Note that there are some explanatory texts on larger screens.

plurals
  1. PODependency Property Binding and Update in Custom Control
    text
    copied!<p>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.</p> <p>Model:</p> <pre><code>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)); } } </code></pre> <p>XAML:</p> <pre><code>&lt;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"&gt; &lt;Grid&gt; &lt;custom:CustomTextbox x:Name="TextboxName" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" TextChanged="CustomTextbox_TextChanged"&gt; &lt;custom:CustomTextbox.CustomTextboxItems&gt; &lt;custom:CustomTextboxItem IsChecked="{Binding IsChecked}" /&gt; &lt;/custom:CustomTextbox.CustomTextboxItems&gt; &lt;/custom:CustomTextbox&gt; &lt;Button Content="Do It" Click="Button_Click" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,20" /&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>Code Behind:</p> <pre><code>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"; } } } </code></pre> <p>Custom Control:</p> <pre><code>public class CustomTextbox : TextBox { public CustomTextbox() { CustomTextboxItems = new ObservableCollection&lt;CustomTextboxItem&gt;(); } public ObservableCollection&lt;CustomTextboxItem&gt; 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); } } } </code></pre> <p>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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload