直接使用键盘输入时文本框绑定丢失

发布于 2024-10-09 18:00:08 字数 2843 浏览 0 评论 0原文

我有一个用户控件正在由另一个用户控件通过提供的 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);

    }

}

}

I Have a usercontrol that is being updated by another usercontrol via provided ValueProperty (see code Window.XAML Binding below) When my other usercontrol's property is being updated, the textbox is updated as well via Value Binding.

But When I directly type to the usercontrols textbox then use my other user control again. The textbox is never updated again. It seems that the ValueProperty of my TextBox UserControl's was overwritten.
I am assuming that the Text={Binding...} was lost and overwritten when I directly typed in the TextBox.

//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);

    }

}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羅雙樹 2024-10-16 18:00:08

尝试 TwoWay 绑定,如下所示:

Text="{Binding ElementName=PART_command, Path=Value, Mode=TwoWay}"

Try TwoWay binding, like this:

Text="{Binding ElementName=PART_command, Path=Value, Mode=TwoWay}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文