C# 如何在属性上实现双向绑定?
我知道有很多关于依赖属性的问题,我已经研究了很多,但是没有一个能解决我的问题 我有一扇这样的窗户:C# 如何在属性上实现双向绑定?,c#,.net,wpf,data-binding,wpf-controls,C#,.net,Wpf,Data Binding,Wpf Controls,我知道有很多关于依赖属性的问题,我已经研究了很多,但是没有一个能解决我的问题 我有一扇这样的窗户: <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=
<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>
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>
现在据我所知,我的窗口现在应该有两个文本框控件,它们彼此绑定。就像我改变其中一个的值一样,另一个应该更新
然而,我最终得到了两个独立的文本框,其中第一个文本框以“Empty”开头,下一个文本框以“完全不同的值”开头。这样地:
而改变其中任何一个文本都不会在另一个文本中重现
我希望它们都以文本“完全不同的值”开始,并与它们的值同步(通过在MainWindow上传播对MyTextValue属性的更改,并在那里通知更改,更改将传播到另一个文本框)。要在控件中正确实现数据绑定,我缺少什么?请尝试更改此行:
public static readonly DependencyProperty MyTextProperty = DependencyProperty.Register("MyText",
typeof(string), typeof(TextInputWrapper), new PropertyMetadata("Empty"));
为此:
public static readonly DependencyProperty MyTextProperty = DependencyProperty.Register("MyText",
typeof(string), typeof(TextInputWrapper), new FrameworkPropertyMetadata("Empty", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
尝试更改此行:
public static readonly DependencyProperty MyTextProperty = DependencyProperty.Register("MyText",
typeof(string), typeof(TextInputWrapper), new PropertyMetadata("Empty"));
为此:
public static readonly DependencyProperty MyTextProperty = DependencyProperty.Register("MyText",
typeof(string), typeof(TextInputWrapper), new FrameworkPropertyMetadata("Empty", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
删除分配
DataContext="{Binding RelativeSource={RelativeSource Self}}"
来自用户控件的XAML。相反,将“内部”绑定的相对源设置为控件实例:
<UserControl x:Class="WpfBindingTest.TextInputWrapper" ...>
<TextBox Text="{Binding MyText,
RelativeSource={RelativeSource AncestorType=UserControl}}"/>
</UserControl>
显式设置UserControl的DataContext可防止从其父控件继承DataContext,如绑定
<local:TextInputWrapper MyText="{Binding MyTextValue, Mode=TwoWay}" />
将使用UserControl作为源对象,而不是当前DataContext
一般来说,永远不要显式设置UserControl的DataContext。删除分配
DataContext="{Binding RelativeSource={RelativeSource Self}}"
来自用户控件的XAML。相反,将“内部”绑定的相对源设置为控件实例:
<UserControl x:Class="WpfBindingTest.TextInputWrapper" ...>
<TextBox Text="{Binding MyText,
RelativeSource={RelativeSource AncestorType=UserControl}}"/>
</UserControl>
显式设置UserControl的DataContext可防止从其父控件继承DataContext,如绑定
<local:TextInputWrapper MyText="{Binding MyTextValue, Mode=TwoWay}" />
将使用UserControl作为源对象,而不是当前DataContext
作为一般规则,永远不要显式设置UserControl的DataContext。在UserControl中,{Binding MyText,RelativeSource={RelativeSource AncestoyType=UserControl}}
,并且不要将UserControl的DataContext绑定到Self。在UserControl中,{Binding MyText,RelativeSource={RelativeSource AncestoyType=UserControl}}
,并且不将UserControl的DataContext绑定到Self。正如您所看到的,它无法绑定到viewmodel。您是否看到了MyText=“{Binding MyTextValue,Mode=TwoWay}”
?不,我实际上没有看到。尽管如此,无论如何重写默认绑定行为可能还是有用的(尽管这不是问题)。您是否看到了MyText=“{binding MyTextValue,Mode=TwoWay}”
?不,我实际上没有看到。不过,不管怎样,重写默认绑定行为可能还是有用的(尽管这不是问题),这似乎确实起到了作用。非常感谢。有什么地方我能读到吗?特别是相对资源部分。关于MSDN的文章应该给你一个好的开始。这似乎确实起到了作用。非常感谢。有什么地方我能读到吗?特别是相对资源部分。关于MSDN的文章应该给你一个好的开始。