WPF DataBinding 来控制属性
WPF DataBinding to control properties
我遇到一个问题,即我无法通过我的绑定接收更新。
我有一个标签,它通过 DataContext 绑定到 TextBox 属性 的 ExtentWidth。
我的绑定最初有效并在标签中显示值 0,但此后它不会更新。
ExtentWidth 是只读的 属性,我不确定这是否会以任何方式影响绑定,但我有一个标签,当它被设置时绑定到文本,所以我知道它可以接收更新. (按钮更新文本和标签更新)
下面是一些代码来演示我的问题。
Xaml
<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
{
///
<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";
}
}
}
您的视图未收到有关 ExtentWidth 属性 更改的通知,因为 ExtentWidth 不是 DependencyProperty,TextBox class 也未实现 INotifyPropertyChanged。此外,似乎没有与此 属性.
关联的相应 Changed 事件
如果您想使用最新的 ExtentWidth 自动更新您的视图,那么您将需要监听一个不同的 property/event(可能是 SizeChanged 事件?)它会同时更新。
我遇到一个问题,即我无法通过我的绑定接收更新。
我有一个标签,它通过 DataContext 绑定到 TextBox 属性 的 ExtentWidth。
我的绑定最初有效并在标签中显示值 0,但此后它不会更新。
ExtentWidth 是只读的 属性,我不确定这是否会以任何方式影响绑定,但我有一个标签,当它被设置时绑定到文本,所以我知道它可以接收更新. (按钮更新文本和标签更新)
下面是一些代码来演示我的问题。
Xaml
<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
{
///
<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";
}
}
}
您的视图未收到有关 ExtentWidth 属性 更改的通知,因为 ExtentWidth 不是 DependencyProperty,TextBox class 也未实现 INotifyPropertyChanged。此外,似乎没有与此 属性.
关联的相应 Changed 事件如果您想使用最新的 ExtentWidth 自动更新您的视图,那么您将需要监听一个不同的 property/event(可能是 SizeChanged 事件?)它会同时更新。