C# 自定义控件中的依赖项属性绑定和更新

C# 自定义控件中的依赖项属性绑定和更新,c#,wpf,xaml,custom-controls,dependency-properties,C#,Wpf,Xaml,Custom Controls,Dependency Properties,我已经创建了一个简化版本的代码,遇到了同样的问题。问题是,我不确定为什么自定义控件中的依赖项属性在模型中更改时没有更新 型号: public class MainWindowModel : INotifyPropertyChanged { private bool isChecked; public bool IsChecked { get { return isChecked; } set { isChecked = value; OnPropertyChanged("IsCh

我已经创建了一个简化版本的代码,遇到了同样的问题。问题是,我不确定为什么自定义控件中的依赖项属性在模型中更改时没有更新

型号:

public class MainWindowModel : INotifyPropertyChanged
{
    private bool isChecked;
    public bool IsChecked { get { return isChecked; } set { isChecked = value; OnPropertyChanged("IsChecked"); } }

    public event PropertyChangedEventHandler PropertyChanged;
    void OnPropertyChanged(string prop)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(prop));
    }
}
XAML:

自定义控件:

public class CustomTextbox : TextBox
{
    public CustomTextbox()
    {
        CustomTextboxItems = new ObservableCollection<CustomTextboxItem>();
    }

    public ObservableCollection<CustomTextboxItem> CustomTextboxItems { get; set; }
}

public class CustomTextboxItem : FrameworkElement
{
    public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool), typeof(CustomTextboxItem), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

    public bool IsChecked
    {
        get { return (bool)GetValue(IsCheckedProperty); }

        set { SetValue(IsCheckedProperty, value); }
    }
}
公共类CustomTextbox:TextBox
{
公共自定义文本框()
{
CustomTextboxItems=新的ObservableCollection();
}
公共ObservableCollection CustomTextboxItems{get;set;}
}
公共类CustomTextboxItem:FrameworkElement
{
public static readonly dependencProperty IsCheckedProperty=dependencProperty.Register(“IsChecked”、typeof(bool)、typeof(CustomTextboxItem)、new FrameworkPropertyMetadata(false、FrameworkPropertyMetadata options.bindstwaybydefault));
公共场所被检查
{
获取{return(bool)GetValue(IsCheckedProperty);}
set{SetValue(IsCheckedProperty,value);}
}
}

正如您在自定义控件中看到的,我有一个项目集合,其中包含具有要绑定到的依赖项属性的对象。因此,我在xaml中创建了对象并设置了绑定,但是当我在模型中更新bind属性时,它不会在自定义控件中更改它。有什么想法吗?

在Visual Studio输出窗口中查找绑定错误。我想你会发现一些东西告诉你复选框上的绑定失败了

您的
CustomTextBox
控件有一组
CustomTextBoxItem
对象,您正在对这些对象设置绑定。但是,在任何时候都不能将这些项添加到逻辑树中。阅读我的文章,了解如何将这些项添加到逻辑树中

public partial class MainWindow : Window
{
    MainWindowModel model;

    public MainWindow()
    {
        InitializeComponent();

        model = new MainWindowModel();
        this.DataContext = model;
    }

    private void CustomTextbox_TextChanged(object sender, TextChangedEventArgs e)
    {
        model.IsChecked = true;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        if (TextboxName.CustomTextboxItems[0].IsChecked)
        {
            TextboxName.Text = "Property successfully changed";
        }
    }
}
public class CustomTextbox : TextBox
{
    public CustomTextbox()
    {
        CustomTextboxItems = new ObservableCollection<CustomTextboxItem>();
    }

    public ObservableCollection<CustomTextboxItem> CustomTextboxItems { get; set; }
}

public class CustomTextboxItem : FrameworkElement
{
    public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool), typeof(CustomTextboxItem), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

    public bool IsChecked
    {
        get { return (bool)GetValue(IsCheckedProperty); }

        set { SetValue(IsCheckedProperty, value); }
    }
}