[英]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.