簡體   English   中英

自定義控件中的依賴項屬性綁定和更新

[英]Dependency Property Binding and Update in Custom Control

我創建了遇到相同問題的代碼的簡化版本。 問題是,我不確定為什么自定義控件中的依賴項屬性在模型中更改后沒有更新。

模型:

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中創建對象並設置綁定,但是當我更新模型中的binded屬性時,它不會在自定義控件中更改它。 有任何想法嗎?

在Visual Studio輸出窗口中查找綁定錯誤。 我認為您會發現一些告訴您復選框綁定失敗的信息。

您的CustomTextBox控件具有一個CustomTextBoxItem對象的集合,您正在針對這些對象設置綁定。 但是,您絕不會將這些項目添加到邏輯樹中。 在這里閱讀我的文章以了解如何將這些項目添加到邏輯樹中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM