我是 MVVM 模式的真正初学者。我试图在单击按钮时更改网格的背景。我有一个带有包含按钮的网格的 xaml,以及我想在单击按钮时更改网格背景的 ViewModel .cs。直到我点击时才成功显示一个 MessageBox ...
.xaml代码:
<Window x:Class="WpfSimple.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfSimple"
Title="MainWindow" Height="150" Width="370">
<Window.DataContext>
<local:MainWindowViewModel/>
</Window.DataContext>
<Grid>
<Button Content="Click"
Height="23"
HorizontalAlignment="Left"
Background="Gray"
Margin="75.944,47.465,0,0"
Name="btnClick"
VerticalAlignment="Top"
Width="203"
Command="{Binding ButtonCommand}"/>
<!--What is necessary to add for changing grid color ? Commandparameter ?-->
</Grid>
MainWindowViewModel.cs代码:
namespace WpfSimple
{
class MainWindowViewModel
{
private ICommand m_ButtonCommand;
public ICommand ButtonCommand
{
get
{
return m_ButtonCommand;
}
set
{
m_ButtonCommand = value;
}
}
public MainWindowViewModel()
{
ButtonCommand=new RelayCommand(new Action<object>(ChangeBgColor));
}
public void ChangeBgColor(object obj)
{
/*HERE I WANT TO CHANGE GRID COLOR*/
}
}
}
抱歉我的英语不好。
最好的问候。
最佳答案
最适合你应该实现 INotifyPropertyChanged在你的 ViewModel 中:
public class MainWindowViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
然后,将 NotifyPropertyChanged() 添加到您的属性 setter 中。
好的。接下来,将带有网格背景颜色的新属性添加到 ViewModel:
private Brush _gridBackground;
public Brush GridBackground
{
get { return _gridBackground; }
set
{
_gridBackground = value;
NotifyPropertyChanged();
}
}
并将您的网格背景绑定(bind)到您的属性:
<Grid Background="{Binding GridBackground}">
最后,您可以在命令处理程序中更改 GridBackground:
public void ChangeBgColor(object obj)
{
GridBackground = Brushes.Blue;
}
您应该记住,将 WPF 类(如 Brush)添加到您的代码中是一种不好的做法。更好的方法是使用 IValueConverter在您的 ViewModel 中的 XAML 代码和 BCL 类中。例如,您可以在 ViewModel 中使用枚举,然后在 ValueConverter 中将其转换为画笔。
为 ViewModel 的属性添加新枚举:
public enum GridState { Valid, Invalid }
更改属性类型:
private GridState _gridBackground; public GridState GridBackground { get { return _gridBackground; } set { _gridBackground = value; NotifyPropertyChanged(); } }
添加带有值转换器的新类
public class GridStateToBackgroundColorConverter : IValueConverter { #region IValueConverter Members public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { GridState val = (GridState) value; if(val == GridState.Valid) return Brushes.Green; return Brushes.Red; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotSupportedException(); } #endregion }
向您的控件添加新的静态资源
<UserControl.Resources> <converters:GridStateToBackgroundColorConverter x:Key="gridStateToBackgroundColorConverter" /> </UserControl.Resources>
更新绑定(bind)到您的属性
<Grid Background="{Binding GridBackground, Converter={StaticResource gridStateToBackgroundColorConverter}">
关于c# - MVVM 在单击时更改网格的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34331654/