Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将标签前景绑定到变量_C#_Wpf_Binding_Wpf Controls - Fatal编程技术网

C# 将标签前景绑定到变量

C# 将标签前景绑定到变量,c#,wpf,binding,wpf-controls,C#,Wpf,Binding,Wpf Controls,我有一个用户界面,我正在更改主网格的后台属性。现在有一些给出了一个非常愉快的外观,但对于一些有困难的阅读文本显示。然而,当我现在有大约20个标签时,就会出现一个问题,让它们每次都改变并指定颜色会让我的代码看起来很难看。我知道一定有更优雅的设计 我试图将标签绑定到一种颜色,但这不起作用。这是代码 XAML: 谢谢看看你发布的C代码,我想你的第一个问题是你已经做到了 SolidColorBrush defColor = new SolidColorBrush(); 而不是这个 public So

我有一个
用户界面
,我正在更改主网格的
后台
属性。现在有一些给出了一个非常愉快的外观,但对于一些有困难的阅读文本显示。然而,当我现在有大约20个标签时,就会出现一个问题,让它们每次都改变并指定颜色会让我的代码看起来很难看。我知道一定有更优雅的设计

我试图将标签绑定到一种颜色,但这不起作用。这是代码

XAML:

谢谢

看看你发布的C代码,我想你的第一个问题是你已经做到了

SolidColorBrush defColor = new SolidColorBrush(); 
而不是这个

public SolidColoRBrush defColor { get; set; }
只能绑定到属性

您的构造函数现在将如下所示

public SettingsWindow()  
{  
    InitializeComponent();  
    defColor = new SolidColorBrush(Colors.Black);
    this.DataContext = this;  // need to tell your window where to look for binding targets
}  
仅从你发布的C代码来看,我认为你的第一个问题是你已经做到了这一点

SolidColorBrush defColor = new SolidColorBrush(); 
而不是这个

public SolidColoRBrush defColor { get; set; }
只能绑定到属性

您的构造函数现在将如下所示

public SettingsWindow()  
{  
    InitializeComponent();  
    defColor = new SolidColorBrush(Colors.Black);
    this.DataContext = this;  // need to tell your window where to look for binding targets
}  

与其将每个标签绑定到同一属性,我认为您应该使用,并将此样式应用于每个标签,例如绑定:

  <Style x:Key="LabelForeGroundStyle" TargetType="{x:Type Label}">
    <Setter Property="Foreground" Value="{Binding defColor}" />
  </Style>

或者更好的方法是:


我认为您不应该将每个标签绑定到同一个属性,而应该为每个标签使用并应用此样式,例如绑定:

  <Style x:Key="LabelForeGroundStyle" TargetType="{x:Type Label}">
    <Setter Property="Foreground" Value="{Binding defColor}" />
  </Style>

或者更好的方法是:


如果您正在设置设置Swindow DataContext=this;然后SettingsWindow类必须实现INotifyPropertyChanged。使{Binding defColor}正常工作。您需要的代码:

public class SettingsWindow : Window, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public SettingsWindow()
    {
        // We are acting as our own 'ViewModel'
        DataContext = this;
        InitializeComponent();
    }

    private Color _defColor;
    public Color defColor
    {
        get { return _defColor; }
        set
        {
            if (_defColor != value)
            {
                _defColor = value;
                if(null != PropertyChanged)
                {
                    PropertyChanged(this, "defColor");
                }
            }
        }
    }
}
正如前面所建议的,针对应用程序中的所有标签,正确的方法是使用样式。 您必须将此样式应用于每个标签。省略x:键,使其成为标签的默认样式

    <Style x:Key="LabelForeGroundStyle" TargetType="{x:Type Label}">
         <Setter Property="Foreground" Value="{Binding defColor}" />
    </Style> 

如果您正在设置设置Swindow DataContext=this;然后SettingsWindow类必须实现INotifyPropertyChanged。使{Binding defColor}正常工作。您需要的代码:

public class SettingsWindow : Window, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public SettingsWindow()
    {
        // We are acting as our own 'ViewModel'
        DataContext = this;
        InitializeComponent();
    }

    private Color _defColor;
    public Color defColor
    {
        get { return _defColor; }
        set
        {
            if (_defColor != value)
            {
                _defColor = value;
                if(null != PropertyChanged)
                {
                    PropertyChanged(this, "defColor");
                }
            }
        }
    }
}
正如前面所建议的,针对应用程序中的所有标签,正确的方法是使用样式。 您必须将此样式应用于每个标签。省略x:键,使其成为标签的默认样式

    <Style x:Key="LabelForeGroundStyle" TargetType="{x:Type Label}">
         <Setter Property="Foreground" Value="{Binding defColor}" />
    </Style> 


是否可能重复?你能详细说明错误消息吗?没有错误消息只是没有更新颜色可能是INotifier:(可能重复?你能详细说明错误消息吗?没有错误消息只是没有更新颜色可能是INotifier:(