Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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# WPF更改带有附加属性的控件的背景_C#_Wpf_Attached Properties_Brushes - Fatal编程技术网

C# WPF更改带有附加属性的控件的背景

C# WPF更改带有附加属性的控件的背景,c#,wpf,attached-properties,brushes,C#,Wpf,Attached Properties,Brushes,当布尔变量为true(返回默认颜色为false)时,我需要更改标签和按钮的背景。所以我写了一个附加属性。目前看来是这样的: public class BackgroundChanger : DependencyObject { #region dependency properties // status public static bool GetStatus(DependencyObject obj) { return (bool)obj.Ge

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

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}" />

它很好用。当在viewmodel中设置相应的变量
test
时,背景颜色将更改为
LimeGreen

我的问题:


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

为什么不改用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}" />

.
.
.

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

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"/>


Status为true时,再使用一个附加属性来指定颜色如何?或者制作
Status
a
Brush
type?我正在努力制作一(或两)个以上的属性。如何在
OnStatusChanged
中使用它们?目前我正在使用datatrigger。但是在我需要的每个控件上写这些行呢?我有点困惑。这些控件正在利用样式,它们都可以在一个地方派生它们的行为(即样式)。好的,这很有效。但是现在如何在xaml的视图中设置这些属性呢?