我在MVVM模式上创建一个WPF应用程序,用户单击树的项目(由颜色名称组成的超链接,具有相应前景的名称文本)以更改整个窗口的背景。我通过relay命令执行此操作,但在View Model中我不能接受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;
}
该命令在简单的代码隐藏中运行良好,但现在不在View Model中。请帮忙?
this.Background
指的是视图模型的Background
属性,前提是ChangeColor
方法属于视图模型类。要更改窗口的背景,需要将其绑定到视图模型的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}" />
您还需要将窗口的DataContext
设置为视图模型类的实例,并将Command
的Hyperlink
属性绑定为:
<Hyperlink Command="{Binding DataContext.ColorChangerCommand, RelativeSource={RelativeSource AncestorType=Window}}"
Foreground="{Binding Foreground}" TextDecorations="None">
不要通过ViewModel来做。 UI属于View。使用它的行为。
如果将ForwardedColor
绑定到任何其他UI控件,则将更改此控件的bound属性,以便您可以在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();
}
}