数据绑定到WPF中的UserControl-无法获取价值

时间:2018-07-25 08:46:08

标签: c# wpf user-controls dependency-properties inotifypropertychanged

我有一个要进行数据绑定的UserControl。 我只是无法将输入返回到绑定。

UserControl:

super()

UserControl后面的代码:

class Child(Human):
    def __init__(self):
        super().__init__()

在另一个UserControl中的实现:

<UserControl x:Class="WPFLibrary.UserControls.LabeledTextBox"
             x:Name="root" ... >
    <StackPanel  Orientation="Horizontal" Width="270" Height="30">
        <Label Content="LabelNotDef" Width="100" Name="BaseLabel" 
               HorizontalAlignment="Left" HorizontalContentAlignment="Left" VerticalContentAlignment="Top" />
        <TextBox Margin="0,0,20,0" Name="BaseTextBox" Width="150" Height="25" VerticalContentAlignment="Center" 
                 Text="{Binding ElementName=root, Path=Text2, UpdateSourceTrigger=PropertyChanged}" />
    </StackPanel>
</UserControl>

CodeBehind:

public partial class LabeledTextBox : UserControl, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public static DependencyProperty TextProperty = DependencyProperty.Register("Text2", typeof(string), typeof(EffLabeledTextBox),
                                                                                 new PropertyMetadata(string.Empty, OnTextPropertyChanged));

    [Browsable(true)]
    public string Text2
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    private static void OnTextPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
    {
        EffLabeledTextBox myUserControl = dependencyObject as EffLabeledTextBox;
        myUserControl.OnPropertyChanged("Text2");
        myUserControl.OnTextPropertyChanged(e);
    }
    private void OnTextPropertyChanged(DependencyPropertyChangedEventArgs e)
    {
        BaseTextBox.Text = Text2;
    }

    public LabeledTextBox()
    {
        InitializeComponent();
    }
}

当我在文本字段中输入内容时,将触发PropertyChanged。如果我检查Toastbread.Text2我得到的价值。如果我检查Toastbread.BaseTextBox.Text我得到的价值。但是,如果我检查objVM._strNR,我只会得到null。

当我在ArtikelSearchVM的构造函数上的objVM._strNR上手动设置一个值时,它在框中,但我也没有从文本框中获取修改后的值。

1 个答案:

答案 0 :(得分:0)

您无需在UserControl中实现INotifyPropertyChanged。这就是您所需要的:

public partial class LabelTextBox : UserControl
{
    public LabelTextBox()
    {
        InitializeComponent();
    }

    public static DependencyProperty TextProperty = DependencyProperty.Register(
        nameof(Text), typeof(string), typeof(LabelTextBox));

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
}

具有这样的XAML:

<UserControl ...>
    ...
    <TextBox Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl},
                    Path=Text, UpdateSourceTrigger=PropertyChanged}" .../>
    ...
</UserControl>

您还可以考虑将Text属性默认设置为双向绑定:

public static DependencyProperty TextProperty = DependencyProperty.Register(
    nameof(Text), typeof(string), typeof(LabelTextBox),
    new FrameworkPropertyMetadata(string.Empty,
        FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));