根据枚举

时间:2016-04-19 11:45:42

标签: c# wpf xaml enums

我创建了这个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属性绑定到笔划ResourceDictionary吗?

提前致谢

2 个答案:

答案 0 :(得分:1)

如果您在资源字典中定义渐变画笔,请执行以下操作:

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

但是,绑定到依赖项属性有点棘手。如果您将笔划定义为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();
    }
}

现在您可以使用转换器绑定到属性值,如下所示:

<子> 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;
    }
}

或者,你可以放弃任何绑定,只使用触发器来完成大致相同的事情。这是一个示例风格,展示了如何实现这一目标:

<子>主题/ 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>

答案 1 :(得分:0)

这是如何在将在窗口中使用的控件中使用。

在InitializeComponent之前将此行放在UserControl的构造函数中 假设您的ResourcesDictionary文件名是StrokeResources.xaml

 //merge the control resources used to current application 
        Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary(){Source = new Uri("/DLLNameSpace;component/ResourcesDictionaries/StrokeResources.xaml", UriKind.RelativeOrAbsolute)}); 

然后修改xaml代码:

<Ellipse Stroke="{Binding StrokeX, Converter={StaticResource ApplicationResourceConverter}}"/>

以这种方式将资源添加到实际应用程序中......