亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用戶控件綁定在值更新時丟失,標準文本框控件不會發生這種情況

用戶控件綁定在值更新時丟失,標準文本框控件不會發生這種情況

C#
慕森王 2021-11-07 20:08:50
我在用戶控件中綁定有問題。這是我的用戶控件:用戶控件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.csnamespace 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>
查看完整描述

1 回答

?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

Binding 必須是 TwoWay,或者顯式設置


<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標志。


在TextBoxUserControl 的 XAML 中的Binding 中,您可能還想在用戶鍵入時更新源屬性(而不是僅在失去焦點時):


<TextBox Text="{Binding HmiField,

                ElementName=usercontrol,

                UpdateSourceTrigger=PropertyChanged}"/>

或者沒有其他無用的生成usercontrol字段:


<TextBox Text="{Binding HmiField,

                RelativeSource={RelativeSource AncestorType=UserControl}

                UpdateSourceTrigger=PropertyChanged}"/>


查看完整回答
反對 回復 2021-11-07
  • 1 回答
  • 0 關注
  • 211 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號