将自定义类型 属性 绑定到自定义控件

Bind a custom type property to a custom control

我正在尝试将自定义类型的 属性 绑定到用户控件(在我们的示例中,我们称之为 DataContextOne)。此自定义类型由两个字符串组成。

然后我有一个用户控件,我想将此自定义类型绑定到它。

奇怪的是,如果我在我的控件中放入 2 个字符串属性,然后尝试从我的自定义类型绑定每个字符串,它就起作用了。但是当我在我的自定义控件中创建一个 DataContextOne 属性 并尝试绑定到它时,没有任何反应(= 用户控件中的 null)。

这是我的代码

DataContextOne

public class DataContextOne : INotifyPropertyChanged
{
    protected virtual void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

    }

    public event PropertyChangedEventHandler PropertyChanged;

    private string _title;
    public string Title 
    {
        get { return _title; }
        set
        {
            if (_title != value)
            {
                _title = value;
                TitleModified = _title + " modified";
                RaisePropertyChanged("Title");
            }
        }
    }

    private string _titlemodified;
    public string TitleModified
    {
        get { return _titlemodified; }
        set
        {
            if (_titlemodified != value)
            {
                _titlemodified = value;
                RaisePropertyChanged("TitleModified");
            }
        }

    }
}

有效的绑定

用户控件

public partial class MyUserControl : UserControl
{

    public MyUserControl()
    {
        InitializeComponent();
        (Content as FrameworkElement).DataContext = this;
    }

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
            "Text",
            typeof(string),
            typeof(MyUserControl),
            null);

    public string TextModified
    {
        get { return (string)GetValue(TextModifiedProperty); }
        set { SetValue(TextModifiedProperty, value); }
    }

    public static readonly DependencyProperty TextModifiedProperty = DependencyProperty.Register(
            "TextModified",
            typeof(string),
            typeof(MyUserControl),
            null);
}

主要Window

代码隐藏

public partial class MainWindow : Window
{
    Random Rnd = new Random();
    DataContextOne dtone = new DataContextOne();

    public MainWindow()
    {
        InitializeComponent();
        dtone.Title = Rnd.Next().ToString();
        DataContext = dtone;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        dtone.Title = Rnd.Next().ToString();
    }
}

xaml:

<local:MyUserControl Grid.Row="1" Text="{Binding Title, UpdateSourceTrigger=PropertyChanged}" TextModified="{Binding TitleModified, UpdateSourceTrigger=PropertyChanged}"/>

无效的绑定

用户控制

public partial class MyUserControl : UserControl
{

    public MyUserControl()
    {
        InitializeComponent();
        (Content as FrameworkElement).DataContext = this;
    }

    public DataContextOne dtone 
    {
        get { return (DataContextOne)GetValue(dtoneProperty); }
        set { SetValue(dtoneProperty, value); }
    }

    public static readonly DependencyProperty dtoneProperty = DependencyProperty.Register(
            "dtone",
            typeof(DataContextOne),
            typeof(MyUserControl),
            null);

}

主要Window

代码隐藏(注意我绑定this

public partial class MainWindow : Window
{
    Random Rnd = new Random();
    DataContextOne dtone = new DataContextOne();

    public MainWindow()
    {
        InitializeComponent();
        dtone.Title = Rnd.Next().ToString();
        DataContext = this;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        dtone.Title = Rnd.Next().ToString();
    }
}

xaml

<local:MyUserControl Grid.Row="1" dtone="{Binding dtone, UpdateSourceTrigger=PropertyChanged}" Margin="0,31,200,169"/>

我不明白缺少什么。对我来说,仅在第二个版本上封装字符串的自定义类型是一样的,那么为什么它不起作用?

{Binding dtone} 尝试绑定到 DataContextdtone 属性,即 MainWindowMainWindowclass中没有dtone属性,只有私有字段,不能绑定字段

可能的解决方案:

  • 制作dtone一个属性
  • 或将 DataContext = this 更改为 DataContext = dtone 并将 {Binding dtone} 更改为 {Binding}

更新

可能无关,但似乎没有理由在 MyUserControl 中出现 dtone 属性。为什么不简单

<local:MyUserControl Grid.Row="1" DataContext="{Binding dtone}" />

没有尝试在 MyUserControl 构造函数中手动设置 DataContext