具有基于枚举值的背景颜色的 WPF 自定义 ContentControl
WPF custom ContentControl with Background Color based on Enum value
我正在尝试构建一个自定义 ContentControl,其状态应导致背景颜色发生变化。
因此我定义了以下枚举:
public enum OrderSourceState
{
Idle,
Busy,
}
我的自定义控件中有一个 DependencyProperty class:
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-Template:
<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>
但颜色仍然存在 gray.Any 想知道我做错了什么吗?
您的触发器是错误的,即您将 DataTrigger
与 Binding="{Binding OrderState}"
一起使用,它试图在当前 DataContext
上找到 OrderState
属性 并绑定给它。您想要的是将触发器基于模板化控件的 属性 值。为此,您应该使用具有相应 Property
值的常规 Trigger
:
<Trigger Property="OrderState" Value="(...)">
(...)
</Trigger>
或者,至少为 DataTrigger
绑定指定正确的来源:
<DataTrigger Binding="{Binding OrderState, RelativeSource={RelativeSource Self}}" Value="(...)">
(...)
</DataTrigger>
我正在尝试构建一个自定义 ContentControl,其状态应导致背景颜色发生变化。
因此我定义了以下枚举:
public enum OrderSourceState
{
Idle,
Busy,
}
我的自定义控件中有一个 DependencyProperty class:
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-Template:
<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>
但颜色仍然存在 gray.Any 想知道我做错了什么吗?
您的触发器是错误的,即您将 DataTrigger
与 Binding="{Binding OrderState}"
一起使用,它试图在当前 DataContext
上找到 OrderState
属性 并绑定给它。您想要的是将触发器基于模板化控件的 属性 值。为此,您应该使用具有相应 Property
值的常规 Trigger
:
<Trigger Property="OrderState" Value="(...)">
(...)
</Trigger>
或者,至少为 DataTrigger
绑定指定正确的来源:
<DataTrigger Binding="{Binding OrderState, RelativeSource={RelativeSource Self}}" Value="(...)">
(...)
</DataTrigger>