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.
<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>
using System.Windows;
using System.Windows.Controls;
namespace TestHarnesses.Views
{
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); }
}
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";
}
}
}