Binding non content properties

If I bind properties like IsReadOnly and Visibility on a TextBox (or any other control), the controls are not refreshed when I change the values on the dataobject. Meaning that the TextBox is still visible and so on.

How can I tell the controls to update

Best regards,

Thomas Andersen



Answer this question

Binding non content properties

  • Marc_S.

    Ah, the problem is simple. You're binding to CLR object's properties, but you have not implemented the INotifyPropertyChanged interface on that class. The databinding engine has no other way to know that the property is changed for a CLR object. Here's all you'd need to do to your MyDataObject class (pretty formatting aside):

    public class MyDataObject : INotifyPropertyChanged
    {
    private Visibility visibility;
    private string text;

    public MyDataObject() { }

    public Visibility Visibility
    {
    get { return visibility; }
    set { visibility = value; if(this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs("Visibility")); } }
    }

    public string Text
    {
    get { return text; }
    set { text = value; if(this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs("Text")); } }
    }

    #region INotifyPropertyChanged implementation

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    }


  • tomer70

    Ahh, of cause. Thanks :)
  • Saleem Hakani - MSFT

    Here is some code to demonstrate. I use a normal CLR property as the dataobject. But I have also tried to use a DP.

    Note that when you click the “cheat” button the values are correct. Actually the problem is also with the Text property, so I might be missing the whole point.

    If I duplicate the TextBox, the Text is updateted correctly in the other TextBox when changed in one TextBox (via input not code).

    Window1.xaml:

    <Window x:Class="WindowsApplication4.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="WindowsApplication4"

    Name="window1"

    >

    <StackPanel DataContext="{Binding ElementName=window1}" Name="panel1">

    <TextBox Visibility="{Binding Path=MyDataObject.Visibility}" Text="{Binding Path=MyDataObject.Text}" />

    <Button Click="ShowHide">Show / Hide</Button>

    <Button Click="Cheat">Cheat refresh</Button>

    </StackPanel>

    </Window>

    Window1.xaml.cs:

    using System;

    using System.Windows;

    using System.Windows.Controls;

    using System.Windows.Data;

    using System.Windows.Documents;

    using System.Windows.Media;

    using System.Windows.Media.Imaging;

    using System.Windows.Shapes;

    namespace WindowsApplication4

    {

    public partial class Window1 : Window

    {

    private MyDataObject myDataObject;

    public Window1()

    {

    this.MyDataObject = new MyDataObject();

    this.MyDataObject.Text = "Test";

    this.MyDataObject.Visibility = Visibility.Visible;

    InitializeComponent();

    }

    public MyDataObject MyDataObject

    {

    get { return myDataObject; }

    set { myDataObject = value; }

    }

    private void ShowHide(object sender, RoutedEventArgs e)

    {

    if (this.MyDataObject.Visibility == Visibility.Visible)

    this.MyDataObject.Visibility = Visibility.Hidden;

    else

    this.MyDataObject.Visibility = Visibility.Visible;

    this.MyDataObject.Text = this.MyDataObject.Visibility.ToString();

    }

    private void Cheat(object sender, RoutedEventArgs e)

    {

    UIElement[] temp = new UIElement[this.panel1.Children.Count];

    this.panel1.Children.CopyTo(temp, 0);

    this.panel1.Children.Clear();

    foreach (UIElement uiElement in temp)

    {

    this.panel1.Children.Add(uiElement);

    }

    }

    }

    public class MyDataObject

    {

    private Visibility visibility;

    private string text;

    public MyDataObject() {}

    public Visibility Visibility

    {

    get { return visibility; }

    set { visibility = value; }

    }

    public string Text

    {

    get { return text; }

    set { text = value; }

    }

    }

    }


  • Parker Whittle

    Hmm... I think we're going to need to see some code since what you're saying is a perfectly supported scenario. So, if you can strip down an example of how you're setting up the binding one of the properites and post it here that would help in analyzing the problem.

    Cheers,
    Drew


  • Binding non content properties