任务:实现有史以来最简单的依赖性属性,可以在XAML中使用:
<uc:MyUserControl1 MyTextProperty="{Binding Text}"/>
我认为这个答案非常接近。为了更好的可读性,我在此处复制所有代码(主要来自上面的答案)。
<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>
和
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();
}
}
这是我的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>
到目前为止,一切都起作用。但是,我发现这无用。我需要的是
<uc:MyUserControl1 MyTextProperty="{Binding Text}"/>
并能够通过设置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));
}
}
}
"初始文字"既不是"也不是"点击"显示...永远。因此,我的问题是如何实施一个部门。正确使用
的属性<uc:MyUserControl1 MyTextProperty="{Binding Text}"/>
Text
属性位于 mainwindow的 DataContext
上 不是usercontrol。
因此,将此行<uc:MyUserControl1 MyTextProperty="{Binding Text}"/>
更改为:
<uc:MyUserControl1 MyTextProperty="{Binding Text, ElementName=MyMainWindow}"/>
这将说明您正在谈论MainWindow中的文本元素。当然,由于在此示例中我使用了ElementName
,因此您将要 name 您的窗口mymainwindow ...
因此,将其添加到您的MainWindow:
<Window Name="MyMainWindow" ..... />
如果您不想命名自己的窗口,则可以使用私人findancestor绑定这样的绑定:
<wpfApplication6:MyUserControl1 MyTextProperty="{Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"/>
在这两种方面,您都要求在窗口的dataContext中找到名为"文本"的属性。