【问题标题】:WPF custom ContentControl with Background Color based on Enum value基于枚举值的具有背景颜色的 WPF 自定义 ContentControl
【发布时间】:2018-01-24 09:45:26
【问题描述】:

我正在尝试构建一个自定义 ContentControl,其状态应该会导致背景颜色发生变化。

因此我定义了以下枚举:

public enum OrderSourceState
{
    Idle,
    Busy,
}

我的 customControl 类中还有一个 DependencyProperty:

public class BorderWithState : ContentControl
{
    public static readonly DependencyProperty OrderStateProperty =
 DependencyProperty.Register("OrderState", typeof(OrderSourceState),
 typeof(BorderWithState), new FrameworkPropertyMetadata(OrderSourceState.Idle));

    // .NET Property wrapper
    public OrderSourceState OrderState
    {
        get { return (OrderSourceState)GetValue(OrderStateProperty); }
        set { SetValue(OrderStateProperty, value); }
    }

    static BorderWithState()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(BorderWithState), new FrameworkPropertyMetadata(typeof(BorderWithState)));
    }
}

最后我定义了以下 XAML 模板:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MasterEKanBan"
xmlns:customControls="clr-namespace:MasterEKanBan.WPF">

<Style TargetType="{x:Type customControls:BorderWithState}">
    <Setter Property="Background">
        <Setter.Value>
            <SolidColorBrush Color="LightGray"/>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type customControls:BorderWithState}">
                <Border x:Name="border" Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <ContentPresenter Content="{TemplateBinding Content}"/>
                </Border>
                <ControlTemplate.Triggers>
                    <DataTrigger  Binding="{Binding OrderState}" Value="{x:Static local:OrderSourceState.Idle}">
                        <Setter TargetName="border" Property="Background">
                            <Setter.Value>
                                <SolidColorBrush Color="LightGray"/>
                            </Setter.Value>
                        </Setter>

                    </DataTrigger>
                    <DataTrigger Binding="{Binding OrderState}" Value="{x:Static local:OrderSourceState.Busy}">
                        <Setter TargetName="border" Property="Background">
                            <Setter.Value>
                                <SolidColorBrush Color="LightGreen"/>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
</ResourceDictionary>

最后我通过以下方式嵌入了自定义控件:

<customControls:BorderWithState Grid.Column="0" Grid.Row="0" BorderThickness="5" BorderBrush="Black" Margin="20"  OrderState="{x:Static local:OrderSourceState.Busy}" >
<Label Content="Mobile-RFID" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="30"/>
</customControls:BorderWithState>

但是颜色仍然是灰色的。任何想法我做错了什么?

【问题讨论】:

    标签: c# wpf enums custom-controls


    【解决方案1】:

    您的触发器是错误的,即您将DataTriggerBinding="{Binding OrderState}" 一起使用,它试图在当前DataContext 上找到OrderState 属性并绑定到它。您想要的是将触发器基于模板化控件上的属性值。为此,您应该使用常规的 Trigger 和相应的 Property 值:

    <Trigger Property="OrderState" Value="(...)">
        (...)
    </Trigger>
    

    或者,至少为DataTrigger 绑定指定正确的来源:

    <DataTrigger Binding="{Binding OrderState, RelativeSource={RelativeSource Self}}" Value="(...)">
        (...)
    </DataTrigger>
    

    【讨论】:

      猜你喜欢
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-31
      • 1970-01-01
      • 2017-04-14
      • 2011-01-30
      相关资源
      最近更新 更多