Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an issue whereby I am not receiving updates through my bindings.

I have a label which is bound to the ExtentWidth of the TextBox property via the DataContext.

My binding initially works and displays the value of 0 in the label however it does not update after this.

ExtentWidth is a read only property, I'm not sure if this affects the binding in any way but I have a label the binds to the text when it is set so I know it can receive updates. (button updates text and label is updated)

below is some code to demonstrate my issue.

XML
<Window x:Class="TestHarnesses.Views.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <StackPanel>
            <ContentPresenter x:Name="ContentPresenter" Content="{Binding}"></ContentPresenter>
            <Label x:Name="lblExtentWidth" 
                   Content="{Binding ExtentWidth, Mode=OneWay}"/>
            <Label x:Name="lblText" 
                   Content="{Binding Text, Mode=OneWay}"/>
            <Button Content="Different Jibber Jabber" Click="ButtonBase_OnClick"/>
        </StackPanel>
    </Grid>
</Window>


C#
using System.Windows;
using System.Windows.Controls;

namespace TestHarnesses.Views
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            TextBox tb = new TextBox(){Text = "Jibber Jabber"};
            this.TestTextBox = tb;
        }



        public TextBox TestTextBox
        {
            get { return (TextBox)GetValue(TestTextBoxProperty); }
            set { SetValue(TestTextBoxProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TestTextBox.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TestTextBoxProperty =
            DependencyProperty.Register("TestTextBox", typeof(TextBox), typeof(Window1), new PropertyMetadata(OnTestTextBoxProperty));

        private static void OnTestTextBoxProperty(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((Window1) d).DataContext = (TextBox) e.NewValue;
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            TestTextBox.Text = "Different Jibber Jabber";
        }
    }
}
Posted

1 solution

TextBoxBase.ExtentWidth[^] is not a DependencyProperty[^]. You can bind to it, but there isn't any way for the binding to be notified when the value changes.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900