在运行时根据枚举值更改边框背景
change border background based on enum value in runtime
我正在设计自定义控件我希望用户能够使用自定义 属性 更改背景颜色。
背景颜色应由名为 Severity 的枚举指定:
代码
public enum Severity
{
Warning,
Information,
Success,
Error
}
Xaml
<Border Background="{DynamicResource InfoBarInformationalSeverityBackgroundBrush}"
CornerRadius="4">
<Grid Margin="10">
<TextBlock Text="{Binding Title,
RelativeSource={RelativeSource AncestorType=ib:InfoBar}}"/>
</Grid>
</Border>
<InfoBar Severity="Error"/>
我尝试了触发器,但它似乎不起作用
更新:
Xaml
<Border>
<Border.Style>
<Style TargetType="Border">
<Style.Triggers>
<DataTrigger Binding="{Binding Severity,
RelativeSource={RelativeSource AncestorType=ib:InfoBar}}"
Value="Error">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding Severity,
RelativeSource={RelativeSource AncestorType=ib:InfoBar}}"
Value="Success">
<Setter Property="Background" Value="Green"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
代码
public Severity Severity
{
get { return (Severity) GetValue(SeverityProperty); }
set { SetValue(SeverityProperty, value); }
}
public static readonly DependencyProperty SeverityProperty =
DependencyProperty.Register("Severity", typeof(Brushes), typeof(InfoBar));
依赖项 属性 应注册为 Severity
而不是 Brushes
:
public static readonly DependencyProperty SeverityProperty =
DependencyProperty.Register(nameof(Severity), typeof(Severity), typeof(InfoBar));
那么假设 Border
是 InfoBar
控件的视觉子项,那么您的示例应该可以工作:
<b:InfoBar Severity="Warning">
<Border>
<Border.Style>
<Style TargetType="Border">
<Style.Triggers>
<DataTrigger Binding="{Binding Severity, RelativeSource={RelativeSource AncestorType=b:InfoBar}}" Value="Warning">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding Severity, RelativeSource={RelativeSource AncestorType=b:InfoBar}}" Value="Success">
<Setter Property="Background" Value="Green"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<TextBlock Text="{Binding Severity, RelativeSource={RelativeSource AncestorType=b:InfoBar}}" />
</Border>
</StackPanel>
</b:InfoBar>
您可以使用转换器。传递您的枚举并转换为颜色:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Color colorDefalut = (Color)ColorConverter.ConvertFromString("#00afef");
switch ((int)value)
{
case (int) Severity. Warning:
return new SolidColorBrush(Colors.Yellow);
case (int) Severity.Information:
return new SolidColorBrush(Colors.bleu);
case (int) Severity. Error:
return new SolidColorBrush(colors.Red);
default:
return new SolidColorBrush(colorDefalut);
}
}
我正在设计自定义控件我希望用户能够使用自定义 属性 更改背景颜色。 背景颜色应由名为 Severity 的枚举指定:
代码
public enum Severity
{
Warning,
Information,
Success,
Error
}
Xaml
<Border Background="{DynamicResource InfoBarInformationalSeverityBackgroundBrush}"
CornerRadius="4">
<Grid Margin="10">
<TextBlock Text="{Binding Title,
RelativeSource={RelativeSource AncestorType=ib:InfoBar}}"/>
</Grid>
</Border>
<InfoBar Severity="Error"/>
我尝试了触发器,但它似乎不起作用
更新:
Xaml
<Border>
<Border.Style>
<Style TargetType="Border">
<Style.Triggers>
<DataTrigger Binding="{Binding Severity,
RelativeSource={RelativeSource AncestorType=ib:InfoBar}}"
Value="Error">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding Severity,
RelativeSource={RelativeSource AncestorType=ib:InfoBar}}"
Value="Success">
<Setter Property="Background" Value="Green"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
代码
public Severity Severity
{
get { return (Severity) GetValue(SeverityProperty); }
set { SetValue(SeverityProperty, value); }
}
public static readonly DependencyProperty SeverityProperty =
DependencyProperty.Register("Severity", typeof(Brushes), typeof(InfoBar));
依赖项 属性 应注册为 Severity
而不是 Brushes
:
public static readonly DependencyProperty SeverityProperty =
DependencyProperty.Register(nameof(Severity), typeof(Severity), typeof(InfoBar));
那么假设 Border
是 InfoBar
控件的视觉子项,那么您的示例应该可以工作:
<b:InfoBar Severity="Warning">
<Border>
<Border.Style>
<Style TargetType="Border">
<Style.Triggers>
<DataTrigger Binding="{Binding Severity, RelativeSource={RelativeSource AncestorType=b:InfoBar}}" Value="Warning">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding Severity, RelativeSource={RelativeSource AncestorType=b:InfoBar}}" Value="Success">
<Setter Property="Background" Value="Green"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<TextBlock Text="{Binding Severity, RelativeSource={RelativeSource AncestorType=b:InfoBar}}" />
</Border>
</StackPanel>
</b:InfoBar>
您可以使用转换器。传递您的枚举并转换为颜色:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Color colorDefalut = (Color)ColorConverter.ConvertFromString("#00afef");
switch ((int)value)
{
case (int) Severity. Warning:
return new SolidColorBrush(Colors.Yellow);
case (int) Severity.Information:
return new SolidColorBrush(Colors.bleu);
case (int) Severity. Error:
return new SolidColorBrush(colors.Red);
default:
return new SolidColorBrush(colorDefalut);
}
}