我正在MVVM模式上创建WPF应用程序,其中用户单击树中的项目(由颜色名称组成的超链接,名称文本具有相应的前景)以更改整个窗口的背景。 我是通过中继命令执行此操作的,但是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中无法正常运行。请帮忙吗?
答案 0 :(得分:1)
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
的{{1}}属性:
Hyperlink
答案 1 :(得分:0)
请勿通过ViewModel进行操作。 UI属于视图。为此使用行为。
如果将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();
}
}