用户控件绑定在值更新时丢失,标准文本框控件不会发生这种情况

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


这是我的用户控件:


用户控件1.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>

用户控件1.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();

        }

    }

}

这是主窗口:


主窗口.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>


慕森王
浏览 175回答 1
1回答

慕虎7371278

Binding 必须是 TwoWay,或者显式设置<local:UserControl1 HmiField="{Binding Prop1, Mode=TwoWay}"/>或默认情况下隐式:public static readonly DependencyProperty HmiFieldProperty =&nbsp; &nbsp; DependencyProperty.Register(&nbsp; &nbsp; &nbsp; &nbsp; nameof(HmiField), typeof(double), typeof(UserControl1),&nbsp; &nbsp; &nbsp; &nbsp; new FrameworkPropertyMetadata(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0d, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));TextBox 的Text属性注册如上所示,即带有BindsTwoWayByDefault标志。在TextBoxUserControl 的 XAML 中的Binding 中,您可能还想在用户键入时更新源属性(而不是仅在失去焦点时):<TextBox Text="{Binding HmiField,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ElementName=usercontrol,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UpdateSourceTrigger=PropertyChanged}"/>或者没有其他无用的生成usercontrol字段:<TextBox Text="{Binding HmiField,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RelativeSource={RelativeSource AncestorType=UserControl}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UpdateSourceTrigger=PropertyChanged}"/>
打开App,查看更多内容
随时随地看视频慕课网APP