【发布时间】:2016-10-24 14:45:31
【问题描述】:
我知道有很多关于依赖属性的问题,我也看过很多,但似乎没有一个能解决我的问题。
我有一个这样的窗口:
<Window x:Class="WpfBindingTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfBindingTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<StackPanel>
<local:TextInputWrapper MyText="{Binding MyTextValue, Mode=TwoWay}" />
<TextBox Text="{Binding MyTextValue, Mode=TwoWay}"/>
</StackPanel>
</Window>
其中 MyTextValue 只是一个在更改时通知的字符串属性:
private string _myTextValue = "Totally different value";
public string MyTextValue { get { return _myTextValue; } set { _myTextValue = value; OnPropertyChanged(); } }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
TextInputWrapper 也比较简单:
<UserControl x:Class="WpfBindingTest.TextInputWrapper"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfBindingTest"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<TextBox Text="{Binding MyText}"></TextBox>
</UserControl>
代码隐藏:
public partial class TextInputWrapper : UserControl
{
public static readonly DependencyProperty MyTextProperty = DependencyProperty.Register("MyText",
typeof(string), typeof(TextInputWrapper), new PropertyMetadata("Empty"));
public TextInputWrapper()
{
InitializeComponent();
}
public string MyText
{
get { return (string)GetValue(MyTextProperty); }
set { SetValue(MyTextProperty, value); }
}
}
现在据我了解,我的 Window 现在应该有 2 个彼此绑定的 TextBox 控件。就像我改变一个值一样,另一个应该更新。
但是,我最终得到了 2 个单独的文本框,其中第一个以文本“空”开头,下一个以文本“完全不同的值”开头。像这样:
并且更改其中任何一个的文本都不会在另一个中重现。
我希望它们都从文本“完全不同的值”开始,并与它们的值同步(通过传播对 MainWindow 上的 MyTextValue 属性的更改,并通知那里的更改,然后更改会向上传播到另一个文本框)。在我的控件中正确实现数据绑定缺少什么?
【问题讨论】:
-
在UserControl中,
{Binding MyText, RelativeSource={RelativeSource AncestoyType=UserControl}},并且不要将UserControl的DataContext绑定到Self。正如你所拥有的,它不能绑定到视图模型。
标签: c# .net wpf data-binding wpf-controls