XAML 到UserControl属性的双向绑定- WinUI 3

tv6aics1  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(178)

我有以下UserControl,它是一个带有标签的预格式化TextBox:
XAML文件:

<UserControl
        x:Class="Aerloc.Components.LabeledTextBox"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:Aerloc.Components"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">

        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="75"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBox Text="{x:Bind Caption, Mode=TwoWay}" Style="{StaticResource LabelTextBox}" Grid.Column="0"/>
            <TextBox Text="{x:Bind Value, Mode=TwoWay}" IsEnabled="{x:Bind IsEditable, Mode=OneWay}" Style="{StaticResource FieldTextBox}" Grid.Column=1/>
        </Grid>
    </UserControl>

代码隐藏:

public sealed partial class LabeledTextBox : UserControl
    {
        public DependencyProperty CaptionProperty = DependencyProperty.Register(nameof(Value), typeof(string), typeof(LabeledTextBox), new PropertyMetadata(null));
        public DependencyProperty ValueProperty = DependencyProperty.Register(nameof(Caption), typeof(string), typeof(LabeledTextBox), new PropertyMetadata(null));
        public DependencyProperty IsEditableProperty = DependencyProperty.Register(nameof(IsEditable), typeof(bool), typeof(LabeledTextBox), new PropertyMetadata(null));

        public string Caption
        {
            get => (string)GetValue(CaptionProperty);
            set => SetValue(CaptionProperty, value);
        }
        public string Value
        {
            get => (string)GetValue(ValueProperty);
            set => SetValue(ValueProperty, value);
        }
        public bool IsEditable
        {
            get => (bool)GetValue(IsEditableProperty);
            set => SetValue(IsEditableProperty, value);
        }

        public LabeledTextBox()
        {
            this.InitializeComponent();
        }
    }

我在双向绑定传递到UserControl的值时遇到问题。当我创建绑定(下面的代码)时,我收到一条错误消息,指出“双向绑定目标'Value'必须是依赖项属性”。如何将UserControl中的值双向绑定到ViewModel?

<components:LabeledTextBox 
                Caption="First Name:"
                Value="{x:Bind ViewModel.SelectedUser.FirstName, Mode=TwoWay}" 
                IsEditable="{x:Bind ViewModel.IsAddEditModeEnabled}"/>
ni65a41a

ni65a41a1#

DependencyProperty必须是static readonly。您的DependencyProperties名称也不正确。
解决这个问题,

public DependencyProperty CaptionProperty = 
    DependencyProperty.Register(
        nameof(Value),
        typeof(string),
        typeof(LabeledTextBox),
        new PropertyMetadata(null));

public DependencyProperty ValueProperty = 
    DependencyProperty.Register(
        nameof(Caption),
        typeof(string),
        typeof(LabeledTextBox),
        new PropertyMetadata(null));

对此,

public static readonly DependencyProperty CaptionProperty = 
    DependencyProperty.Register(
        nameof(Caption),
        typeof(string),
        typeof(LabeledTextBox),
        new PropertyMetadata(null));

public static readonly DependencyProperty ValueProperty = 
    DependencyProperty.Register(
        nameof(Value),
        typeof(string),
        typeof(LabeledTextBox),
        new PropertyMetadata(null));

相关问题