使用 NotificationBase 的数据绑定问题
Data Binding issue using NotificationBase
Custom_View.xaml
<UserControl>
<local:Custom_Text_Field
Custom_Text_Field_Color="{x:Bind ViewModel.Color1 , Mode=TwoWay}">
</local:Custom_Text_Field>
<local:Custom_Text_Field
Custom_Text_Field_Color="{x:Bind ViewModel.Color2 , Mode=TwoWay}">
</local:Custom_Text_Field>
<Button Click="{x:Bind ViewModel.ChangeColor"/>
</UserControl>
Custom_View.cs
public sealed partial class Custom_View : UserControl
{
public Custom_View_VM ViewModel { get; set; }
public Custom_View()
{
ViewModel = new Custom_View_VM();
this.InitializeComponent();
}
}
Custom_View_VM.cs
public class Custom_View_VM : NotificationBase
{
public Brush Color1 { get; set; }
public Brush Color2 { get; set; }
public void ChangeColor{//change color1 or color2};
}
我使用了这个例子中的 NotificationBase class:https://blogs.msdn.microsoft.com/johnshews_blog/2015/09/09/a-minimal-mvvm-uwp-app/
如果我在构造函数中影响 Color1 或 Color2 的值,它会起作用(更改视图),但在调用 ChangeColor 后,视图模型中的值会更改,但不会影响视图。
您需要注册属性。
public static readonly DependencyProperty Custom_Text_Field_Color_Property =
DependencyProperty.Register("Custom_Text_Field_Color", typeof(Brush),
typeof(Class_Name), new UIPropertyMetadata(null));
public Brush Custom_Text_Field_Color
{
get { return (Brush)GetValue(Custom_Text_Field_Color_Property); }
set { SetValue(Custom_Text_Field_Color_Property, value); }
}
使用 typeof(Class_Name)
.
的控件名称(即 Class 名称)
对于 UI 更新,它应该收到一个 PropertyChanged
事件。您应该使用 NotificationBase 的机制来设置属性,这也会引发 PropertyChanged
事件:
public class Custom_View_VM : NotificationBase
{
private Brush color1;
public Brush Color1
{
get { return color1; }
set { SetProperty(color1, value, () => color1 = value); }
}
// TODO: same here
public Brush Color2 { get; set; }
public void ChangeColor{//change color1 or color2};
}
此外,颜色 通常 不会进入 ViewModels
。 ViewModel
应该有一些业务逻辑 属性,您可以将 TextBox
的颜色作为 XAML
的基础,例如 IsNameAvailable
.
在你的情况下 class NotificationBase 是自定义的 class,你可以使用 is 或 not。
我基本上只讲解MVVM设计模式。
在ViewModel中,应该实现Interface INotifyPropertyChanged,当设置属性时,触发事件PropertyChanged。
public sealed class MainPageViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _productName;
public string ProductName
{
get { return _productName; }
set
{
_productName = value;
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(nameof(ProductName)));
}
}
}
}
示例将演示此 MVVM 设计模式。
https://code.msdn.microsoft.com/How-to-achieve-MVVM-design-2bb5a580
Custom_View.xaml
<UserControl>
<local:Custom_Text_Field
Custom_Text_Field_Color="{x:Bind ViewModel.Color1 , Mode=TwoWay}">
</local:Custom_Text_Field>
<local:Custom_Text_Field
Custom_Text_Field_Color="{x:Bind ViewModel.Color2 , Mode=TwoWay}">
</local:Custom_Text_Field>
<Button Click="{x:Bind ViewModel.ChangeColor"/>
</UserControl>
Custom_View.cs
public sealed partial class Custom_View : UserControl
{
public Custom_View_VM ViewModel { get; set; }
public Custom_View()
{
ViewModel = new Custom_View_VM();
this.InitializeComponent();
}
}
Custom_View_VM.cs
public class Custom_View_VM : NotificationBase
{
public Brush Color1 { get; set; }
public Brush Color2 { get; set; }
public void ChangeColor{//change color1 or color2};
}
我使用了这个例子中的 NotificationBase class:https://blogs.msdn.microsoft.com/johnshews_blog/2015/09/09/a-minimal-mvvm-uwp-app/
如果我在构造函数中影响 Color1 或 Color2 的值,它会起作用(更改视图),但在调用 ChangeColor 后,视图模型中的值会更改,但不会影响视图。
您需要注册属性。
public static readonly DependencyProperty Custom_Text_Field_Color_Property =
DependencyProperty.Register("Custom_Text_Field_Color", typeof(Brush),
typeof(Class_Name), new UIPropertyMetadata(null));
public Brush Custom_Text_Field_Color
{
get { return (Brush)GetValue(Custom_Text_Field_Color_Property); }
set { SetValue(Custom_Text_Field_Color_Property, value); }
}
使用 typeof(Class_Name)
.
对于 UI 更新,它应该收到一个 PropertyChanged
事件。您应该使用 NotificationBase 的机制来设置属性,这也会引发 PropertyChanged
事件:
public class Custom_View_VM : NotificationBase
{
private Brush color1;
public Brush Color1
{
get { return color1; }
set { SetProperty(color1, value, () => color1 = value); }
}
// TODO: same here
public Brush Color2 { get; set; }
public void ChangeColor{//change color1 or color2};
}
此外,颜色 通常 不会进入 ViewModels
。 ViewModel
应该有一些业务逻辑 属性,您可以将 TextBox
的颜色作为 XAML
的基础,例如 IsNameAvailable
.
在你的情况下 class NotificationBase 是自定义的 class,你可以使用 is 或 not。
我基本上只讲解MVVM设计模式。 在ViewModel中,应该实现Interface INotifyPropertyChanged,当设置属性时,触发事件PropertyChanged。
public sealed class MainPageViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _productName;
public string ProductName
{
get { return _productName; }
set
{
_productName = value;
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(nameof(ProductName)));
}
}
}
}
示例将演示此 MVVM 设计模式。 https://code.msdn.microsoft.com/How-to-achieve-MVVM-design-2bb5a580