【问题标题】:Bind label Foreground to a variable将标签 Foreground 绑定到变量
【发布时间】:2011-12-28 17:39:14
【问题描述】:

我有一个User Interface,我正在更改主网格的background 属性。现在有些人看起来非常愉快,但对于某些人来说,阅读所显示的文本有困难。但是,当我现在有大约 20 个标签时,就会出现问题,并且每次更改它们并为其分配颜色会使我的代码看起来很难看。我知道一定有更优雅的设计。

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

XAML:

<Label Foreground="{Binding defColor}" Content="Settings" Height="44" HorizontalAlignment="Left" Margin="12,53,0,0" Name="label1" VerticalAlignment="Top" FontWeight="Normal" FontSize="26" />

后面的代码:

  SolidColorBrush defColor = new SolidColorBrush();

  public SettingsWindow()
  {
    InitializeComponent();
    defColor.Color = Colors.Black;
    //defColor.Color = Colors.Black;  label1.Foreground = defColor;
  }

  private void button4_Click(object sender, RoutedEventArgs e)
  {
   defColor.Color = Colors.Black;
  }

谢谢

【问题讨论】:

标签: c# wpf binding wpf-controls


【解决方案1】:

仅从您发布的 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
}  

【讨论】:

    【解决方案2】:

    如果您正在设置 SettingsWindow 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");
                    }
                }
            }
        }
    }
    

    对于应用程序中的所有标签,正确的方法是使用 Style,如前所述。 您必须将此样式应用于每个标签。省略 x:Key 使其成为标签的默认样式

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

    【讨论】:

      【解决方案3】:

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

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

      甚至更好,trigger:

      <Style.Triggers>
       <Trigger>
         <Trigger Property="Background" Value="Blue">
           <Setter Property="Foreground" Value="Green"/>
         </Trigger>
       </Trigger>
      </Style.Triggers>
      

      【讨论】:

        猜你喜欢
        • 2014-07-06
        • 2011-03-09
        • 2012-01-12
        • 2023-03-22
        • 1970-01-01
        • 2015-06-30
        • 2011-08-01
        • 1970-01-01
        • 2020-02-23
        相关资源
        最近更新 更多