我制作了一个包含多个页面的程序。这是一个简单的程序,我在顶部区域也标记您当前所在的页面。对于每个页面,它都是定义的新标签。所有标签都在dockpanel.xaml中定义,后者包含在mainwindow.xaml中。
我喜欢用不同颜色制作当前页面标签。
我的代码:
我的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
}
稍后定义ViewModel页面之一:
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");
}
}
答案 0 :(得分:2)
现在随着更新的问题变得更糟。请优化并修复你的命名!
为了实现这一点,这是我的建议:
这是一个有效的例子: 视图:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Foreground="{Binding Path=LabelColor}" Content="welcome" FontSize="20" FontWeight="Light" FontStyle="Italic"/>
<StackPanel Grid.Column="1">
<Button Content="Red" Width="75" Command="{Binding ChangeColorCommand}" CommandParameter="#FF0000"/>
<Button Content="Green" Width="75" Command="{Binding ChangeColorCommand}" CommandParameter="#00FF00" />
</StackPanel>
</Grid>
查看码:
public MainWindow()
{
InitializeComponent();
var vm = new ViewModel();
DataContext = vm;
}
视图模型:
public class ViewModel : INotifyPropertyChanged
{
public ICommand ChangeColorCommand { get; set; }
protected Brush _color;
public Brush LabelColor
{
get { return _color; }
set
{
_color = value;
OnPropertyChange();
}
}
public ViewModel()
{
LabelColor = Brushes.Yellow;
ChangeColorCommand = new RelayCommand((o) =>
{
LabelColor = new BrushConverter().ConvertFromString(o.ToString()) as SolidColorBrush;
});
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChange([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
RelayCommand是网上随处可见的众所周知的标准类。
答案 1 :(得分:-1)
我已经使用了buttons
而不是lable
并完成了我的代码
//Method
private void changeBackgroundOfButtons(string selectedButtons)
{
switch (selectedButtons)
{
case "home":
HomeButton.Background = (SolidColorBrush)new BrushConverter().ConvertFromString("#0199ED"); //HomeButton is my button
TextBlock1.Foreground = Brushes.White; //Also I changed the text of that button
case:
//For all my buttons
}
}
在我的Buttons
点击事件中,我调用了此方法。
private void HomeButton_OnClick(object sender, RoutedEventArgs e)
{
changeBackgroundOfButtons("home");
//Your code after this.
}