데이터 WPF에서 웹 브라우저의 원본 속성 바인딩
데이터베이스 바인딩 방법을 아는 사람이 있습니까?WPF(3.5SP1)에 있는 웹 브라우저의 원본 속성?왼쪽에는 작은 웹 브라우저를, 오른쪽에는 콘텐츠를, 목록 항목에 바인딩된 각 개체의 URI로 각 웹 브라우저의 소스를 데이터 바인딩하는 목록 보기가 있습니다.
이것이 지금까지 개념 증명으로 가지고 있는 것이지만, "<WebBrowser Source="{Binding Path=WebAddress}"
컴파일하지 않습니다.
<DataTemplate x:Key="dealerLocatorLayout" DataType="DealerLocatorAddress">
<StackPanel Orientation="Horizontal">
<!--Web Control Here-->
<WebBrowser Source="{Binding Path=WebAddress}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
Width="300"
Height="200"
/>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Path=CompanyName}" FontWeight="Bold" Foreground="Blue" />
<TextBox Text="{Binding Path=DisplayName}" FontWeight="Bold" />
</StackPanel>
<TextBox Text="{Binding Path=Street[0]}" />
<TextBox Text="{Binding Path=Street[1]}" />
<TextBox Text="{Binding Path=PhoneNumber}"/>
<TextBox Text="{Binding Path=FaxNumber}"/>
<TextBox Text="{Binding Path=Email}"/>
<TextBox Text="{Binding Path=WebAddress}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
문제는 그것이 아니라는 것입니다.DependencyProperty
한 가지 해결 방법은 다음과 같습니다.AttachedProperty
이 능력을 가능하게 하는 마법.
public static class WebBrowserUtility
{
public static readonly DependencyProperty BindableSourceProperty =
DependencyProperty.RegisterAttached("BindableSource", typeof(string), typeof(WebBrowserUtility), new UIPropertyMetadata(null, BindableSourcePropertyChanged));
public static string GetBindableSource(DependencyObject obj)
{
return (string) obj.GetValue(BindableSourceProperty);
}
public static void SetBindableSource(DependencyObject obj, string value)
{
obj.SetValue(BindableSourceProperty, value);
}
public static void BindableSourcePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
WebBrowser browser = o as WebBrowser;
if (browser != null)
{
string uri = e.NewValue as string;
browser.Source = !String.IsNullOrEmpty(uri) ? new Uri(uri) : null;
}
}
}
그런 다음 xaml에서 다음을 수행합니다.
<WebBrowser ns:WebBrowserUtility.BindableSource="{Binding WebAddress}"/>
종속성 속성을 사용하는 래퍼 사용자 컨트롤을 작성했습니다.
XAML:
<UserControl x:Class="HtmlBox">
<WebBrowser x:Name="browser" />
</UserControl>
C#:
public static readonly DependencyProperty HtmlTextProperty = DependencyProperty.Register("HtmlText", typeof(string), typeof(HtmlBox));
public string HtmlText {
get { return (string)GetValue(HtmlTextProperty); }
set { SetValue(HtmlTextProperty, value); }
}
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) {
base.OnPropertyChanged(e);
if (e.Property == HtmlTextProperty) {
DoBrowse();
}
}
private void DoBrowse() {
if (!string.IsNullOrEmpty(HtmlText)) {
browser.NavigateToString(HtmlText);
}
}
다음과 같이 사용합니다.
<Controls:HtmlBox HtmlText="{Binding MyHtml}" />
이 문제의 유일한 문제는 웹 브라우저 컨트롤이 "순수한" wpf가 아니라는 것입니다.이것은 실제로 win32 구성 요소의 포장지일 뿐입니다.이는 컨트롤이 z-index를 존중하지 않으며 항상 다른 요소(예: 스크롤 뷰어에서 이것이 약간의 문제를 일으킬 수 있음)를 겹쳐 MSDN의 win32-wpf 문제에 대한 추가 정보를 의미합니다.
바인딩 소스의 문자열이나 Uris 중 하나를 처리하는 버전을 만들기 위해 토드의 훌륭한 답변을 조금 수정했습니다.
public static class WebBrowserBehaviors
{
public static readonly DependencyProperty BindableSourceProperty =
DependencyProperty.RegisterAttached("BindableSource", typeof(object), typeof(WebBrowserBehaviors), new UIPropertyMetadata(null, BindableSourcePropertyChanged));
public static object GetBindableSource(DependencyObject obj)
{
return (string)obj.GetValue(BindableSourceProperty);
}
public static void SetBindableSource(DependencyObject obj, object value)
{
obj.SetValue(BindableSourceProperty, value);
}
public static void BindableSourcePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
WebBrowser browser = o as WebBrowser;
if (browser == null) return;
Uri uri = null;
if (e.NewValue is string )
{
var uriString = e.NewValue as string;
uri = string.IsNullOrWhiteSpace(uriString) ? null : new Uri(uriString);
}
else if (e.NewValue is Uri)
{
uri = e.NewValue as Uri;
}
browser.Source = uri;
}}
좋은 생각이야 토드.
나는 리치 텍스트 상자로 비슷한 일을 했습니다.선택.Silverlight 4에 문자를 보냅니다.게시물 감사합니다.잘 작동합니다.
public class RichTextBoxHelper
{
public static readonly DependencyProperty BindableSelectionTextProperty =
DependencyProperty.RegisterAttached("BindableSelectionText", typeof(string),
typeof(RichTextBoxHelper), new PropertyMetadata(null, BindableSelectionTextPropertyChanged));
public static string GetBindableSelectionText(DependencyObject obj)
{
return (string)obj.GetValue(BindableSelectionTextProperty);
}
public static void SetBindableSelectionText(DependencyObject obj, string value)
{
obj.SetValue(BindableSelectionTextProperty, value);
}
public static void BindableSelectionTextPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
RichTextBox rtb = o as RichTextBox;
if (rtb != null)
{
string text = e.NewValue as string;
if (text != null)
rtb.Selection.Text = text;
}
}
}
여기 Xaml-Code가 있습니다.
<RichTextBox IsReadOnly='False' TextWrapping='Wrap' utilities:RichTextBoxHelper.BindableSelectionText="{Binding Content}"/>
이것은 Null 병합 연산자를 사용하고 일부 기본 논리 전제를 활용하기 위한 Todd와 Samuel의 답변을 개선한 것입니다.
public static void BindableSourcePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
WebBrowser browser = o as WebBrowser;
if ((browser != null) && (e.NewValue != null))
browser.Source = e.NewValue as Uri ?? new Uri((string)e.NewValue);
}
- 브라우저가 null이거나 위치가 null이면 null 페이지를 사용하거나 탐색할 수 없습니다.
- #1의 항목이 null이 아닌 경우 할당할 때 새 값이 URI인 경우 이 값을 사용합니다.그렇지 않고 URI가 null인 경우 #1은 문자열이 null일 수 없도록 적용하므로 URI에 넣을 수 있는 문자열이어야 합니다.
당신은 그것을 처음 몇 줄에 선언해야 합니다.xaml
클래스 파일을 가리키는 파일
xmlns:reportViewer="clr-namespace:CoMS.Modules.Report"
언급URL : https://stackoverflow.com/questions/263551/databind-the-source-property-of-the-webbrowser-in-wpf
'programing' 카테고리의 다른 글
작업 태그를 이클립스의 현재 프로젝트로 제한하려면 어떻게 해야 합니까? (0) | 2023.05.27 |
---|---|
FormControlName과 FormControl의 차이점은 무엇입니까? (0) | 2023.05.27 |
AWS Lambda에서 MongoDB 연결 (0) | 2023.05.22 |
젠킨스에서 작업을 복제하려면 어떻게 해야 합니까? (0) | 2023.05.22 |
리눅스에서 bash 스크립트의 GUI 메시지 상자를 표시하는 방법은 무엇입니까? (0) | 2023.05.22 |