UWP:绑定到视图模型属性

优瑟比

在我的视图模型中:

public string MyProperty{ get; set; }

public MyViewModel()
{
     MyProperty = "hello";
}

我已经定义了一个字符串属性。

现在,从我的页面,我想绑定到这个属性:

Text="{Binding MyProperty}"

但这不起作用 - 没有显示文本。我错过了什么?

编辑:我的视图模型继承自:

public class Observable : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void Set<T>(ref T storage, T value, [CallerMemberName]string propertyName = null)
        {
            if (Equals(storage, value))
            {
                return;
            }

            storage = value;
            OnPropertyChanged(propertyName);
        }

        protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

编辑2:

我修改了我的视图模型:

private string _myProperty;

        public string MyProperty
        {
            get => _myProperty;
            set => Set(ref _myProperty, value);
        }

        public MyViewModel()
        {
            _myProperty = "hello";
        }

和xaml:

Text="{Binding MyProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

但它仍然无法正常工作。

编辑 3:我认为问题在于该Text属性是自定义控件的注册依赖属性:

public sealed partial class MyControl : UserControl
    {
        public MyControl()
        {
            InitializeComponent();
            DataContext = this;
        }

        public string Text
        {
            get => (string)GetValue(s_textProperty);
            set => SetValue(s_textProperty, value);
        }

        public static readonly DependencyProperty s_textProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(MyControl), new PropertyMetadata(null));
    }

在控件的 xaml 中,我有:

<TextBlock Text="{Binding Text}" />

这:

<MyControl Text="{Binding MyProperty}"/>

在我使用自定义控件的页面中。

大卫·赫鲁斯卡

你的类应该实现INotifyPropertyChanged并拥有这样的属性访问器:

public event PropertyChangedEventHandler PropertyChanged;

private string _myProperty;

public string MyProperty
{
    get { return _myProperty; }
    set
    {
        _myProperty = value;
        OnPropertyChanged();
    }
}

private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

在 XAML 中:

Text="{Binding MyProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

评论:

Mode = TwoWay - 如果被任一更改,属性将在 UI 和代码中更改。UpdateSourceTrigger - 对 PropertyChanged 事件做出反应。另外,请阅读 DataContext :)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将VisualStateGroup的当前状态绑定到UWP MVVM应用程序的视图模型中的属性

Xamarin UWP似乎绑定到错误的视图模型

将视图模型的属性绑定到依赖属性

绑定到模型或视图模型

UWP绑定到属性

从视图模型绑定到ListView项目的点击属性

wpf 向上绑定:绑定到嵌套 uiElement 内的视图模型属性

如何将视图的BackgroundColor属性绑定到Xamarin Forms中的视图模型?

如何更新其他项目视图模型 (UWP) 中的绑定属性?

将媒体元素绑定到视图模型

将模型数据绑定到视图-Spring

绑定到其他控件的视图模型

骨干将模型绑定到视图

不同视图模型之间的绑定属性

从 UI 绑定到视图模型属性需要从 TextBox 中移除焦点

Universal Apps:如何将ListViewItem(容器)的属性绑定到实际项目(视图模型)?

WPF控件(绑定到视图模型上的列表的属性)想要在Xaml中定义列表

如何将模型中的 Float 值绑定到 SwiftUI 视图中的 @Binding:Float 属性

如何从视图模型将Func <T,T,T>绑定到XAML中的依赖项属性?

如何在WPF应用程序中绑定到子视图模型属性?

如何将自定义Dependecy属性绑定到控件的视图模型?

创建从视图模型集合类到数据上下文中的属性的绑定

为什么 DepartmentID 值没有正确绑定到视图模型的属性?

为什么 DepartmentID 值没有正确绑定到视图模型的属性?

UWP组合框绑定到SelectedItem属性

绑定到UWP中的多个属性

如何绑定到UWP中的附加属性?

UWP MVVMCross绑定属性到方法

视图模型绑定:当绑定到的类是抽象的时,是否有一种方法可以访问具体实现的属性?