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

发布于 2024-10-13 06:36:21 字数 2869 浏览 2 评论 0原文

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

模型:

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:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:custom="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <custom:CustomTextbox x:Name="TextboxName" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" TextChanged="CustomTextbox_TextChanged">
        <custom:CustomTextbox.CustomTextboxItems>
            <custom:CustomTextboxItem IsChecked="{Binding IsChecked}" />
        </custom:CustomTextbox.CustomTextboxItems>
    </custom:CustomTextbox>

    <Button Content="Do It" Click="Button_Click" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,20" />
</Grid>
</Window>

代码隐藏:

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); }
    }
}

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

I have created a simplified version of my code that experiences the same issue. The issue is that I'm not sure why the dependency property in my custom control is not updating when it gets changed in the model.

Model:

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:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:custom="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <custom:CustomTextbox x:Name="TextboxName" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" TextChanged="CustomTextbox_TextChanged">
        <custom:CustomTextbox.CustomTextboxItems>
            <custom:CustomTextboxItem IsChecked="{Binding IsChecked}" />
        </custom:CustomTextbox.CustomTextboxItems>
    </custom:CustomTextbox>

    <Button Content="Do It" Click="Button_Click" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,20" />
</Grid>
</Window>

Code Behind:

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

Custom Control:

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); }
    }
}

As you can see in the custom control, I have a collection of items that contain objects with dependency properties that I want to bind to. So I create the objects in the xaml and setup the binding, but when I update the binded property in the model, it doesn't change it in the custom control. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初与友歌 2024-10-20 06:36:21

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

您的 CustomTextBox 控件具有一组 CustomTextBoxItem 对象,您将针对这些对象设置绑定。但是,您决不会将这些项目添加到逻辑树中。请阅读我的文章此处,了解如何将这些项目添加到逻辑树。

Look for binding errors in the Visual Studio output window. I think you'll find something telling you the binding on the check box has failed.

Your CustomTextBox control has a collection of CustomTextBoxItem objects, against which you're setting a binding. However, at no point are you adding those items to the logical tree. Read my post here to see how to add those items to the logical tree.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文