【问题标题】:Usercontrol binding lost on value updated, that doesn't happen with standard textbox control更新值时用户控件绑定丢失,标准文本框控件不会发生这种情况
【发布时间】:2018-07-31 08:20:49
【问题描述】:

我在用户控件中的绑定有问题。

这是我的用户控件:

UserControl1.xaml

<UserControl x:Class="WpfApp1.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-ompatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:local="clr-namespace:WpfApp1"
    mc:Ignorable="d" 
    x:Name="usercontrol"
    d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <TextBox Text="{Binding HmiField, ElementName=usercontrol}"/>
    </Grid>
</UserControl>

UserControl1.xaml.cs

namespace WpfApp1
{
    public partial class UserControl1 : UserControl
    {

        public double HmiField
        {
            get { return (double)GetValue(HmiFieldProperty); }
            set { SetValue(HmiFieldProperty, value); }
        }
        public static readonly DependencyProperty HmiFieldProperty =
            DependencyProperty.Register("HmiField", typeof(double), typeof(UserControl1));

        public UserControl1()
        {
            InitializeComponent();
        }
    }
}

这是主窗口:

MainWindow.xaml

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        DataContext="{Binding Md, RelativeSource={RelativeSource Self}}"
        Title="MainWindow" Height="450" Width="800">
    <UniformGrid>
        <Button Content="{Binding Prop1}" Click="Button_Click"/>
        <Label Content="{Binding Prop1}"/>
        <TextBox Text="{Binding Prop1}"/>
        <local:UserControl1 HmiField="{Binding Prop1}"/>
    </UniformGrid>
</Window>

MainWindow.xaml.cs

namespace WpfApp1
{

    public class tMd: INotifyPropertyChanged
    {
        #region Interfaccia INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string propName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
        }

        #endregion

        private double prop1;
        public double Prop1 { get
            {
                return prop1;
            }
            set
            {
                if (prop1 != value)
                {
                    prop1 = value;
                    NotifyPropertyChanged("Prop1");
                }
            }
        }
    }

    public partial class MainWindow : Window
    {

        public tMd Md
        {
            get { return (tMd)GetValue(MdProperty); }
            set { SetValue(MdProperty, value); }
        }
        public static readonly DependencyProperty MdProperty =
            DependencyProperty.Register("Md", typeof(tMd), typeof(MainWindow), new PropertyMetadata(new tMd()));

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Md.Prop1 = 1234.5678;
        }
    }
}

我发现了一些类似的问题:

How do I change TextBox.Text without losing the binding in WPF?

WPF: Binding is lost when bindings are updated

WPF Textbox TwoWay binding in datatemplate not updating the source even on LostFocus

但我不能完全理解发生了什么:为什么标准文本框按预期工作而我的用户控件没有?

或者更好:有没有办法让我的用户控件与文本框的行为一起工作?

【问题讨论】:

  • “不工作”是什么意思?您的代码似乎对 TextBox 和 UserControl1 的工作方式相同。

标签: c# wpf xaml binding user-controls


【解决方案1】:

绑定必须是双向的,要么显式设置

<local:UserControl1 HmiField="{Binding Prop1, Mode=TwoWay}"/>

或默认隐含:

public static readonly DependencyProperty HmiFieldProperty =
    DependencyProperty.Register(
        nameof(HmiField), typeof(double), typeof(UserControl1),
        new FrameworkPropertyMetadata(
            0d, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

TextBox 的Text 属性如上所示注册,即使用BindsTwoWayByDefault 标志。


在 UserControl 的 XAML 中的 TextBox 绑定处,您可能还希望在用户键入时更新源属性(而不是仅在失去焦点时):

<TextBox Text="{Binding HmiField,
                ElementName=usercontrol,
                UpdateSourceTrigger=PropertyChanged}"/>

或者没有其他无用的生成 usercontrol 字段:

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

【讨论】:

  • 如此简单……如此明显!非常感谢克莱门斯!我在textbox's source code 看不到证据就失明了!!!
【解决方案2】:

您的 Prop1 会在更改时发出通知,但您没有告诉您绑定以触发该通知。 尝试在绑定中包含 UpdateSourceTrigger=PropertyChanged

【讨论】:

    猜你喜欢
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多