wpf – 绑定到自定义依赖属性 – 再次

好的,我知道.这已经被问了一百万次,但我还是不明白.对不起,

任务:实现最简单的依赖属性,可以在xaml中使用:

<uc:MyUserControl1 MyTextProperty="{Binding Text}"/>

我认为this的回答是相当接近的.为了更好的可读性,我将所有的代码复制到这里(主要是从上面的答案).

<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}"/>

这将告诉Binding你在谈论MainWindow中的Text元素.当然,因为在这个例子中我使用了ElementName,你将要命名你的窗口MyMainWindow …

所以添加到你的MainWindow:

<Window  Name="MyMainWindow" ..... />

如果你不是命名你的窗口,你可以使用RelativeSource FindAncestor绑定如下:

<wpfApplication6:MyUserControl1 MyTextProperty="{Binding Text,RelativeSource={RelativeSource FindAncestor,AncestorType=Window}}"/>

在这两种方式中,您都要求在窗口的DataContext中找到名为“Text”的属性.

相关文章

迭代器模式(Iterator)迭代器模式(Iterator)[Cursor]意图...
高性能IO模型浅析服务器端编程经常需要构造高性能的IO模型,...
策略模式(Strategy)策略模式(Strategy)[Policy]意图:定...
访问者模式(Visitor)访问者模式(Visitor)意图:表示一个...
命令模式(Command)命令模式(Command)[Action/Transactio...
生成器模式(Builder)生成器模式(Builder)意图:将一个对...