我已经制作了几个页面的程序。这是一个简单的程序,我在顶部区域也会标记您目前的页面。对于每个页面,它都是一个新的标签定义。所有标签都在dockpanel.xaml中定义,稍后将其包含在mainwindow.xaml中。改变C#,WPF,MVVM标签的颜色
我喜欢用不同的颜色制作当前页面标签。
我的代码:
我DockPanel.xaml的第一个标签(其它都一样只数的变化)视图模型中的一个
<Label Name="Label1" Foreground="{Binding Path=Label1.Color}" Content="welcome" Grid.Column="0" HorizontalAlignment="Left" FontSize="20" FontWeight="Light" FontStyle="Italic"/>
我DockPanelViewModel
public class DockPanelViewModel : ViewModelBase
{
#region Member fields
#endregion
#region Constructors
/// <summary>
/// The default constructor
/// </summary>
public DockPanelViewModel()
{
}
#endregion
#region Properties
protected Brush _color;
public Brush Color
{
get { return _color; }
set
{
_color = value;
NotifyPropertyChanged("Color");
}
}
#endregion
}
后来定义页码:
Label1.Color = System.Windows.Media.Brushes.Yellow;
重点是我的代码不想改变颜色,我不知道什么是错的:)
请帮忙。谢谢!
新增.. PageViewModelBase
public virtual DockPanelViewModel Label1
{
get
{
if (_Label1 == null)
{
_Label1 = new DockPanelViewModel()
{
//Text = "Back",
Color = System.Windows.Media.Brushes.Yellow,
};
}
return _Label1;
}
set
{
_Label1 = value;
NotifyPropertyChanged("Label1");
}
}
您是否已将视图的DataContext设置为您的ViewModel? – Measuring
无法重现它,必须是绑定错误。但通常考虑使用样式设置前景等。 –
检查您的绑定。它应该是{绑定路径=颜色}。 另外,在更改值时,应该在视图模型上设置值,而不是在标签上。 “ – Roemer