Привязка TextBox Потеряна при наборе текста непосредственно с клавиатуры - PullRequest
0 голосов
/ 29 декабря 2010

У меня есть usercontrol, который обновляется другим usercontrol через предоставленный ValueProperty (см. Код Window.XAML Binding ниже) Когда обновляется свойство другого моего usercontrol, текстовое поле также обновляется через Value Binding.

Но когда я непосредственно набираю текстовое поле usercontrols, то снова использую другой пользовательский элемент управления. Текстовое поле никогда не обновляется снова. Кажется, что ValueProperty моего TextBox UserControl был перезаписан. Я предполагаю, что Text = {Binding ...} был потерян и перезаписан, когда я непосредственно набрал в 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);

    }

}

}

1 Ответ

1 голос
/ 29 декабря 2010

Попробуйте TwoWay переплет, как это:

Text="{Binding ElementName=PART_command, Path=Value, Mode=TwoWay}"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...