gpt4 book ai didi

silverlight - 传递更新到 ViewModel 的 DependencyProperty 是一种好习惯吗?

转载 作者:行者123 更新时间:2023-12-03 10:20:00 24 4
gpt4 key购买 nike

我有一个自定义用户控件,它有一个依赖属性。那个自定义用户控件有点复杂,所以我决定为它制作一个 View 模型,但我还没有实现它。我正在考虑使 View 模型具有一些绑定(bind)到自定义用户控件的属性。

这是我的代码示例,

用户控件.xaml

<StackPanel>
<TextBlock Text={Binding Age} />
<TextBlock Text={Binding Name} />
</StackPanel>

用户控件.cs
public Person Person
{
get { return (Person)GetValue(PersonProperty); }
set { SetValue(PersonProperty, value); }
}
public static readonly DependencyProperty PersonProperty = DependencyProperty.Register("Person", typeof(Person), typeof(SampleUserControl), new PropertyMetadata(null, propertyChangedCallback));

private static void propertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// I want to update the view model here
// Something like the following
(this.DataContext as MyViewModel).Person = Person;
}

我的 View 模型
public Person Person
{
get { return _person; }
set
{
_pserson = person;
RaisePorpertyChanged("Age");
RaisePorpertyChanged("Name");
}
}

public int Age{ get; set; }
public string Name{ get; set; }

那么,你认为这是一个好习惯吗?我的意思是在更新依赖属性时更新 View 模型,希望有人教我如何更新 PropertyChangedCallback 中的 View 模型 :) 顺便说一句,我正在使用 MVVM Light 工具包。

最佳答案

根据这个问题,http://forums.silverlight.net/forums/p/133665/298671.aspx ,我不能使用 View 模型作为用户控件的 DataContext 以通常的方式绑定(bind)某些 UI。

关于silverlight - 传递更新到 ViewModel 的 DependencyProperty 是一种好习惯吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3413583/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com