[英]TextBox Binding Lost when typing directly using the Keyboard
我有一个用户控件正在由另一个用户控件通过提供的ValueProperty更新(请参见下面的代码Window.XAML绑定)。当我的另一个用户控件的属性正在更新时,文本框也将通过值绑定进行更新。
但是,当我直接在用户控件文本框中键入内容时,请再次使用其他用户控件。 文本框不会再更新。 看来我的TextBox UserControl的ValueProperty已被覆盖。 我假设当我直接在TextBox中键入文本时,Text = {Binding ...}丢失并被覆盖。
//Window.XAML
<veraControls:veraCommandPanel Name="commandCTRL" Value="{Binding ElementName=MyKeyPad, Path=Value}" HorizontalAlignment="Right" Width="360.057" Margin="0,266,-92.834,0" FontSize="42" FontFamily="lcdD" Foreground="LimeGreen" Height="54.901" VerticalAlignment="Top" />
//Here is my userControl
<UserControl x:Name="PART_command" x:Class="Vera.WPFControls.veraCommandPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="Auto" Width="300"
xmlns:local="clr-namespace:Vera.WPFControls">
<StackPanel>
<StackPanel.Resources>
<Style x:Name="PART_veraCommand" TargetType="{x:Type local:veraCommandPanel}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:veraCommandPanel}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</StackPanel.Resources>
<TextBox Height="55" Name="PART_txtCommand" Text="{Binding ElementName=PART_command, Path=Value}" VerticalAlignment="Top" />
</StackPanel>
//TextBox UserControl's Code Behind
namespace Vera.WPFControls
{
/// <summary>
/// Interaction logic for veraCommandPanel.xaml
/// </summary>
public partial class veraCommandPanel : UserControl, INotifyPropertyChanged
{
public veraCommandPanel()
{
InitializeComponent();
this.DataContext = this;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public static DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(string), typeof(veraCommandPanel));
//, new UIPropertyMetadata(false, new PropertyChangedCallback(PropChanged)));
public string Value
{
get
{
return (string)GetValue(ValueProperty);
}
set
{
SetValue(ValueProperty, value);
}
}
}
尝试TwoWay
绑定,如下所示:
Text="{Binding ElementName=PART_command, Path=Value, Mode=TwoWay}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.