【问题标题】:WPF change background of a control with an attached propertyWPF 更改带有附加属性的控件的背景
【发布时间】:2014-12-13 01:57:20
【问题描述】:

当布尔变量为真时,我需要更改标签和按钮的背景(假时返回默认颜色)。所以我写了一个附加属性。到目前为止看起来像这样:

public class BackgroundChanger : DependencyObject
{
    #region dependency properties
    // status
    public static bool GetStatus(DependencyObject obj)
    {
        return (bool)obj.GetValue(StatusProperty);
    }
    public static void SetStatus(DependencyObject obj, bool value)
    {
        obj.SetValue(StatusProperty, value);
    }
    public static readonly DependencyProperty StatusProperty = DependencyProperty.RegisterAttached("Status",
                     typeof(bool), typeof(BackgroundChanger), new UIPropertyMetadata(false, OnStatusChange));

    #endregion

    private static void OnStatusChange(DependencyObject obj,  DependencyPropertyChangedEventArgs e) 
    {
        var element = obj as Control;
        if (element != null)
        {
            if ((bool)e.NewValue)
                element.Background = Brushes.LimeGreen;
            else
                element.Background = default(Brush);
        }
    }
}

我是这样使用它的:

<Label CustomControls:BackgroundChanger.Status="{Binding test}" />

它工作正常。当在视图模型中设置了相应的变量test时,背景色变为LimeGreen

我的问题:

颜色LimeGreen 是硬编码的。我也想在 XAML 中设置该颜色(以及默认颜色)。所以我可以决定背景切换的两种颜色。我该怎么做?

【问题讨论】:

  • Status 为真时,如何使用另外一个附加属性来指定颜色?还是将Status 设为Brush 类型?
  • 我正在努力制作一个(或两个)更多的属性。我如何在OnStatusChanged 中使用它们?

标签: c# wpf attached-properties brushes


【解决方案1】:

您可以有多个附加属性。访问它们很容易,有静态的Get..Set.. 方法,您可以向它们提供DependencyObject 要操作的附加属性值。

public class BackgroundChanger : DependencyObject
{
    // make property Brush Background
    // type "propdp", press "tab" and complete filling

    private static void OnStatusChange(DependencyObject obj,  DependencyPropertyChangedEventArgs e) 
    {
        var element = obj as Control;
        if (element != null)
        {
            if ((bool)e.NewValue)
                element.Background = GetBrush(obj); // getting another attached property value
            else
                element.Background = default(Brush);
        }
    }
}

在 xaml 中它看起来像

<Label CustomControls:BackgroundChanger.Status="{Binding test}"
    CustomControls:BackgroundChanger.Background="Red"/>

【讨论】:

  • 好的,这行得通。但是我现在如何在 xaml 的视图中设置这些属性?
【解决方案2】:

为什么不改用 DataTrigger?

<Style x:Key="FilePathStyle" TargetType="TextBox">
    <Style.Triggers>
        <DataTrigger Binding="{Binding ElementName=AddButton, Path=IsEnabled}" Value="True">
            <Setter Property="Background" Value="#FFEBF7E1" />
        </DataTrigger>

        <DataTrigger Binding="{Binding ElementName=AddButton, Path=IsEnabled}" Value="False">
            <Setter Property="Background" Value="LightYellow" />
        </DataTrigger>
    </Style.Triggers>
</Style>

.
.
.
<TextBox Style="{StaticResource FilePathStyle}" x:Name="filePathControl" Width="300" Height="25" Margin="5" Text="{Binding SelectedFilePath}" />

【讨论】:

  • 目前我正在使用数据触发器。但是在我需要该功能的每个控件上写下所有这些行?
  • 我有点困惑。这些控件正在利用样式,它们都可以在一个地方导出其行为(即样式)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多