Mar*_*ser 22 wpf xaml binding dependency-properties
好的,我知道.这已经被问了一百万次,但我仍然没有得到它.对不起.
任务:实现最简单的依赖属性,可以在xaml中使用:
<uc:MyUserControl1 MyTextProperty="{Binding Text}"/>
Run Code Online (Sandbox Code Playgroud)
我认为这个答案非常接近.为了更好的可读性,我在这里复制了所有代码(主要来自上面的答案).
<UserControl x:Class="Test.UserControls.MyUserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<!-- Text is being bound to outward representative property;
Note the DataContext of the UserControl -->
<TextBox Text="{Binding MyTextProperty}"/>
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
和
public partial class MyUserControl1 : UserControl
{
// The dependency property which will be accessible on the UserControl
public static readonly DependencyProperty MyTextPropertyProperty =
DependencyProperty.Register("MyTextProperty", typeof(string), typeof(MyUserControl1), new UIPropertyMetadata(String.Empty));
public string MyTextProperty
{
get { return (string)GetValue(MyTextPropertyProperty); }
set { SetValue(MyTextPropertyProperty, value); }
}
public MyUserControl1()
{
InitializeComponent();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:uc="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<StackPanel Orientation="Vertical">
<uc:MyUserControl1 MyTextProperty="my text goes here"/>
<Button Click="ButtonBase_OnClick" Content="click"/>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
到目前为止,一切正常.但是,我觉得这没有用.我需要的是
<uc:MyUserControl1 MyTextProperty="{Binding Text}"/>
Run Code Online (Sandbox Code Playgroud)
并且能够通过设置DataContext(通常在MVVM中)来改变它
所以我替换上面的行,并添加我的代码如下:
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
Text = "Initial Text";
DataContext = this;
}
private string _Text;
public string Text
{
get { return _Text; }
set
{
if (value != _Text)
{
_Text = value;
NotifyPropertyChanged("Text");
}
}
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
Text = "clicked";
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
Run Code Online (Sandbox Code Playgroud)
无论是"初始文本"还是"点击"都不会显示...... 所以我的问题是如何实施部门.属性正确使用
<uc:MyUserControl1 MyTextProperty="{Binding Text}"/>
Run Code Online (Sandbox Code Playgroud)
?谢谢你的协助.
Bla*_*hma 26
该Text物业位于DataContext的主窗口不是用户控件的.
所以将这一行<uc:MyUserControl1 MyTextProperty="{Binding Text}"/>改为:
<uc:MyUserControl1 MyTextProperty="{Binding Text, ElementName=MyMainWindow}"/>
Run Code Online (Sandbox Code Playgroud)
这将告诉Binding您正在谈论位于MainWindow中的Text元素.当然,因为在我使用的这个例子中ElementName,你将要命名你的窗口MyMainWindow ...
所以将它添加到您的MainWindow:
<Window Name="MyMainWindow" ..... />
Run Code Online (Sandbox Code Playgroud)
如果您不想命名窗口,可以使用RelativeSource FindAncestor绑定,如下所示:
<wpfApplication6:MyUserControl1 MyTextProperty="{Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"/>
Run Code Online (Sandbox Code Playgroud)
在这两种方式中,您都要求在窗口的DataContext中找到名为"Text"的属性.