gpt4 book ai didi

c# - 在 C#、WPF、MVVM 中更改标签的颜色

转载 作者:行者123 更新时间:2023-11-30 17:43:05 27 4
gpt4 key购买 nike

我做了一个有好几页的程序。这是一个简单的程序,我在顶部区域还标记了您当前所在的页面。对于每一页,它都是一个新定义的标签。所有标签都在 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");
}
}

最佳答案

更新后的问题现在变得更糟了。请优化并修复您的命名!

为了让它发挥作用,这是我的建议:

  1. 你的 DockPanelViewModel 看起来没问题
  2. 创建 DockPanelViewModel 的实例并将其分配给 View 的 DataContext
  3. 将标签的前景属性绑定(bind)到 {Binding Path=Color}(这是您的 viewModel 的颜色属性
  4. 删除标签的“名称”(在适当的 MVVM 中不需要它
  5. 每当您想更改标签的颜色时,只要更改 Color 属性,如果您的 View 模型实例(您分配给 View 的 DataContext 的实例)
  6. 我不知道您最近添加到问题中的公共(public)虚拟 DockPanelViewModel Label1 是什么。对我来说这似乎是不必要的,删除它。

这是一个工作示例:查看:

<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;
}

View 模型:

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 是网络上随处可见的著名标准类。

关于c# - 在 C#、WPF、MVVM 中更改标签的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31380496/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com