UI 在视图模型 (WPF) 中更新命令
UI updating command in View Model (WPF)
我正在 MVVM 模式上制作 WPF 应用程序,用户单击树的项目(由颜色名称组成的超链接,具有相应前景的名称文本)以 更改背景整个 window. 我正在通过中继命令执行此操作,但是 UI 在我正在编写命令的视图模型中是不可接受的。
颜色名称在 XAML 中的树:
<TreeView Name="tree" ItemSource="{Binding colorList, Mode=TwoWay}" Background="Transparent">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemSource={Binding Children}>
<TextBlock><Hyperlink Command={Binding ColorChangerCommand} Foreground={Binding Foreground} TextDecorations="None"><TextBlock Text={Binding Name}/></Hyperlink></TextBlock>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
<TreeView>
我的视图模型中的命令:
public RelayCommand ColorChangerCommand{ get; set;}
public TreeViewModel() //Constructor of the View Model
{
ColorChangerCommand= new RelayCommand(ChangeColor);
}
public void ChangeColor(object sender)
{
this.Background= (sender as TreeViewItem).Foreground;
}
该命令在简单的代码隐藏中运行良好,但现在在视图模型中不起作用。请帮忙?
不要通过 ViewModel 进行。 UI 属于视图。为它使用一种行为。
如果您将 ForwardedColor
绑定到任何其他 UI 控件,那么您将更改此控件的绑定 属性,因此您可以在 XAML 中轻松管理它.
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
<TextBlock Text="Test" Foreground="Aquamarine">
<i:Interaction.Behaviors>
<local:ForwardForegroundOnClick ForwardedColor="{Binding Background, RelativeSource={RelativeSource AncestorType=Window}, Mode=TwoWay}"/>
</i:Interaction.Behaviors>
</TextBlock>
public class ForwardForegroundOnClick : Behavior<TextBlock>
{
public Brush ForwardedColor
{
get { return (Brush)GetValue(ForwardedColorProperty); }
set { SetValue(ForwardedColorProperty, value); }
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ForwardedColorProperty =
DependencyProperty.Register(nameof(ForwardedColor), typeof(Brush), typeof(ForwardForegroundOnClick), new PropertyMetadata(null));
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.MouseLeftButtonDown += AssociatedObject_MouseLeftButtonDown;
}
private void AssociatedObject_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
ForwardedColor = AssociatedObject.Foreground;
}
protected override void OnDetaching()
{
AssociatedObject.MouseLeftButtonDown -= AssociatedObject_MouseLeftButtonDown;
base.OnDetaching();
}
}
this.Background
指的是你的视图模型的Background
属性,前提是ChangeColor
方法属于视图模型class。要更改 window 的背景,您需要将其绑定到视图模型的 Background
属性 并引发事件以告知 UI 进行更新。这需要您的视图模型实现 INotifyPropertyChanged
事件:
public class ViewModel : INotifyPropertyChanged
{
public RelayCommand ColorChangerCommand { get; set; }
public TreeViewModel() //Constructor of the View Model
{
ColorChangerCommand = new RelayCommand(ChangeColor);
}
public void ChangeColor(object sender)
{
this.Background = (sender as TreeViewItem).Foreground;
}
private Brush background= Brushes.White;
public Brush Background
{
get { return background; }
set { Background = value; NotifyPropertyChanged(Background); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
XAML:
<Window .... Background="{Binding Background}" />
您还需要将 window 的 DataContext
设置为视图模型的实例 class 并绑定 Command
属性 Hyperlink
像这样:
<Hyperlink Command="{Binding DataContext.ColorChangerCommand, RelativeSource={RelativeSource AncestorType=Window}}"
Foreground="{Binding Foreground}" TextDecorations="None">
我正在 MVVM 模式上制作 WPF 应用程序,用户单击树的项目(由颜色名称组成的超链接,具有相应前景的名称文本)以 更改背景整个 window. 我正在通过中继命令执行此操作,但是 UI 在我正在编写命令的视图模型中是不可接受的。
颜色名称在 XAML 中的树:
<TreeView Name="tree" ItemSource="{Binding colorList, Mode=TwoWay}" Background="Transparent">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemSource={Binding Children}>
<TextBlock><Hyperlink Command={Binding ColorChangerCommand} Foreground={Binding Foreground} TextDecorations="None"><TextBlock Text={Binding Name}/></Hyperlink></TextBlock>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
<TreeView>
我的视图模型中的命令:
public RelayCommand ColorChangerCommand{ get; set;}
public TreeViewModel() //Constructor of the View Model
{
ColorChangerCommand= new RelayCommand(ChangeColor);
}
public void ChangeColor(object sender)
{
this.Background= (sender as TreeViewItem).Foreground;
}
该命令在简单的代码隐藏中运行良好,但现在在视图模型中不起作用。请帮忙?
不要通过 ViewModel 进行。 UI 属于视图。为它使用一种行为。
如果您将 ForwardedColor
绑定到任何其他 UI 控件,那么您将更改此控件的绑定 属性,因此您可以在 XAML 中轻松管理它.
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
<TextBlock Text="Test" Foreground="Aquamarine">
<i:Interaction.Behaviors>
<local:ForwardForegroundOnClick ForwardedColor="{Binding Background, RelativeSource={RelativeSource AncestorType=Window}, Mode=TwoWay}"/>
</i:Interaction.Behaviors>
</TextBlock>
public class ForwardForegroundOnClick : Behavior<TextBlock>
{
public Brush ForwardedColor
{
get { return (Brush)GetValue(ForwardedColorProperty); }
set { SetValue(ForwardedColorProperty, value); }
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ForwardedColorProperty =
DependencyProperty.Register(nameof(ForwardedColor), typeof(Brush), typeof(ForwardForegroundOnClick), new PropertyMetadata(null));
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.MouseLeftButtonDown += AssociatedObject_MouseLeftButtonDown;
}
private void AssociatedObject_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
ForwardedColor = AssociatedObject.Foreground;
}
protected override void OnDetaching()
{
AssociatedObject.MouseLeftButtonDown -= AssociatedObject_MouseLeftButtonDown;
base.OnDetaching();
}
}
this.Background
指的是你的视图模型的Background
属性,前提是ChangeColor
方法属于视图模型class。要更改 window 的背景,您需要将其绑定到视图模型的 Background
属性 并引发事件以告知 UI 进行更新。这需要您的视图模型实现 INotifyPropertyChanged
事件:
public class ViewModel : INotifyPropertyChanged
{
public RelayCommand ColorChangerCommand { get; set; }
public TreeViewModel() //Constructor of the View Model
{
ColorChangerCommand = new RelayCommand(ChangeColor);
}
public void ChangeColor(object sender)
{
this.Background = (sender as TreeViewItem).Foreground;
}
private Brush background= Brushes.White;
public Brush Background
{
get { return background; }
set { Background = value; NotifyPropertyChanged(Background); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
XAML:
<Window .... Background="{Binding Background}" />
您还需要将 window 的 DataContext
设置为视图模型的实例 class 并绑定 Command
属性 Hyperlink
像这样:
<Hyperlink Command="{Binding DataContext.ColorChangerCommand, RelativeSource={RelativeSource AncestorType=Window}}"
Foreground="{Binding Foreground}" TextDecorations="None">