我创建了这个 ResourcesDictionary:
<LinearGradientBrush x:Key="OffStroke" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF007226" Offset="0"/>
<GradientStop Color="#FF003C15" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="OnStroke" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF6BBF8A" Offset="0"/>
<GradientStop Color="#FF007A27" Offset="0.306"/>
</LinearGradientBrush>
然后我在 codeBehind 中创建了以下枚举:
/// <summary>
/// Enumerator
/// </summary>
public enum Strokes
{
/// <summary>
/// off (value 0)
/// </summary>
Off = 0x00,
/// <summary>
/// on (value 1)
/// </summary>
On = 0x01,
}
我在 UserControl xaml 端放置了两个椭圆。
<Viewbox x:Name="ViewBoxRoot">
<Grid x:Name="GridRoot" Width="256" Height="256">
<Ellipse x:Name="OnStroke" Stroke={StaticResource OnStroke}/>
<Ellipse x:Name="OffStroke" Stroke="{StaticResource OffStroke}"/>
</Grid>
</Viewbox>
最后我插入了以下属性:
public static readonly DependencyProperty StrokeXProperty =
DependencyProperty.Register("StrokeX", typeof(Enums.Strokes), typeof(MyUserControl), new PropertyMetadata(Enums.Strokes.Off));
public Enums.Strokes StrokeX
{
get { return (Enums.Strokes)GetValue(myUserControl.StrokeXProperty); }
set
{
SetValue(myUserControl.StrokeXProperty, value);
OnPropertyChanged("StrokeX");
}
}
是否可以根据枚举值将 Ellipse Stroke 属性绑定(bind)到 stroke ResourceDictionary?
提前致谢
最佳答案
如果您像这样在资源字典中定义渐变画笔:
xmlns:enums="clr-namespace:MyNamespace.Enums" ...
<LinearGradientBrush x:Key="{x:Static enums:Strokes.Off}" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF007226" Offset="0"/>
<GradientStop Color="#FF003C15" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="{x:Static enums:Strokes.On}" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF6BBF8A" Offset="0"/>
<GradientStop Color="#FF007A27" Offset="0.306"/>
</LinearGradientBrush>
现在您可以直接使用这些资源键:
<Ellipse x:Name="OnStroke" Stroke="{StaticResource {x:Static enums:Strokes.On}}"/>
<Ellipse x:Name="OffStroke" Stroke="{StaticResource {x:Static enums:Strokes.On}}"/>
然而,绑定(bind)到依赖属性有点棘手。如果您将笔画定义为 App.xaml
资源字典,那么您可以按照以下行创建一个 IValueConverter
:
public class ApplicationResourceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Application.Current.Resources[value];
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
现在您可以使用转换器绑定(bind)到属性值,如下所示:
MyControl.xaml
<UserControl x:Class="MyNamespace.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyNamespace"
xmlns:converters="clr-namespace:MyNamespace.Converters">
<UserControl.Resources>
<converters:ApplicationResourceConverter x:Key="ApplicationResourceConverter"/>
</UserControl.Resources>
<StackPanel Grid.Row="1" Grid.Column="2">
<Ellipse Stroke="{Binding StrokeX, RelativeSource={RelativeSource AncestorType=local:MyControl}, Converter={StaticResource ApplicationResourceConverter}}"
StrokeThickness="5"
Fill="AliceBlue"
Width="100"
Height="100"
Margin="0 0 0 20"/>
<StackPanel Orientation="Horizontal">
<Button Click="OnButton_Click">On</Button>
<Button Click="OffButton_Click">Off</Button>
</StackPanel>
</StackPanel>
</UserControl>
MyControl.xaml.cs
public partial class MyControl : UserControl
{
public MyControl()
{
InitializeComponent();
}
public static readonly DependencyProperty StrokeXProperty =
DependencyProperty.Register("StrokeX", typeof(Enums.Strokes), typeof(MyControl), new PropertyMetadata(Enums.Strokes.Off));
public Enums.Strokes StrokeX
{
get { return (Enums.Strokes)GetValue(StrokeXProperty); }
set
{
SetValue(StrokeXProperty, value);
}
}
private void OnButton_Click(object sender, RoutedEventArgs e)
{
this.StrokeX = Enums.Strokes.On;
}
private void OffButton_Click(object sender, RoutedEventArgs e)
{
this.StrokeX = Enums.Strokes.Off;
}
}
或者,您可以放弃任何绑定(bind),只使用触发器来完成大致相同的事情。下面是一个示例样式,展示了您可以如何实现这一点:
Themes/Generic.xaml
xmlns:enums="clr-namespace:MyNamespace.Enums" ...
<LinearGradientBrush x:Key="{x:Static enums:Strokes.Off}" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF007226" Offset="0"/>
<GradientStop Color="#FF003C15" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="{x:Static enums:Strokes.On}" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF6BBF8A" Offset="0"/>
<GradientStop Color="#FF007A27" Offset="0.306"/>
</LinearGradientBrush>
<Style x:Key="{x:Type local:MyControl}" TargetType="local:MyControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:MyControl">
<StackPanel Grid.Row="1" Grid.Column="2">
<Ellipse Name="_ellipse"
StrokeThickness="5"
Fill="AliceBlue"
Width="100"
Height="100"
Margin="0 0 0 20"/>
<StackPanel Orientation="Horizontal">
<Button Click="OnButton_Click">On</Button>
<Button Click="OffButton_Click">Off</Button>
</StackPanel>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="StrokeX" Value="{x:Static enums:Strokes.On}">
<Setter TargetName="_ellipse" Property="Stroke" Value="{DynamicResource {x:Static enums:Strokes.On}}"/>
</Trigger>
<Trigger Property="StrokeX" Value="{x:Static enums:Strokes.Off}">
<Setter TargetName="_ellipse" Property="Stroke" Value="{DynamicResource {x:Static enums:Strokes.Off}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
关于c# - 根据枚举将 Shape.Stroke 绑定(bind)到 ResourceDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36717426/