【问题标题】:Data binding to a UserControl in WPF - cant get Value back数据绑定到 WPF 中的 UserControl - 无法取回值
【发布时间】:2019-01-02 00:26:42
【问题描述】:

我有一个 UserControl,我想在其中进行 DataBinding。 我只是无法将输入返回到绑定。

用户控制:

<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>

UserControl 背后的代码:

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();
    }
}

在另一个 UserControl 中的实现:

<uCon:LabeledTextBox x:Name="Toastbread" Tabelle="artikel" Feld="nr" Text2="{Binding _strNR, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

代码隐藏:

public partial class ArtikelSearch : UserControl
{
    ArtikelSearchVM objVM;

    public ArtikelSearch()
    {
        InitializeComponent();
        objVM = new ArtikelSearchVM();
        this.DataContext = objVM;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        string a23 = Toastbread.Text2;
        string a24 = Toastbread.BaseTextBox.Text;
        System.Diagnostics.Debugger.Break();
        objVM.Suche();
    }
}

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

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

【问题讨论】:

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


    【解决方案1】:

    您无需在 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));
    

    【讨论】:

    • 非常感谢,现在可以使用了。问题也是一个错误的变量名。这很好。我有这个问题几个小时.. 谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    相关资源
    最近更新 更多