我已經制作了幾個頁面的程序。這是一個簡單的程序,我在頂部區域也會標記您目前的頁面。對於每個頁面,它都是一個新的標籤定義。所有標籤都在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