【问题标题】:Can I have a custom parameter linked to a style in my control?我可以将自定义参数链接到我的控件中的样式吗?
【发布时间】:2018-01-06 02:18:33
【问题描述】:

我有一系列图像,我希望它们都有 MouseOver 替代品。我可以为它们中的每一个实现一种样式,例如:

<Image Source="C:\Image1.png" Style="{StaticResource Image1Style}"/>

...

<Image>
  <Image.Style>
    <Style TargetType="{x:Type Image}" x:Key="Image1Style">
      <Setter Property="Source" Value="C:\Image1.jpg"/>
      <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
          <Setter Property="Source" Value="C:\Image2.jpg"/>
        </Trigger>
      </Style.Triggers>
    </Style>
  </Image.Style>
</Image>

但是这样的实现需要为每张图片设置一个完整的样式块。我宁愿想要一种统一的样式,然后我可以在实际控件的标签中指定 IsMouseOver 属性,如下所示:

<Image Source="C:\Image1.png" Style="{StaticResource Image1Style}" IsMouseOver="C:\Image1MouseOver.png" />

我以为我对模板有所了解,但我无法将这些部分组合在一起来完成这项工作。

【问题讨论】:

  • 你为什么不把你的风格放在一个资源中,给它一个名字 x:name = "MyImageStyle" 然后引用这个风格作为一个静态资源
  • 如果有一些一致的方法可以将默认图像路径/名称转换为鼠标悬停图像路径/名称,您可以编写一个值转换器并在样式中使用它。不过,我个人更喜欢 Ash 的附加属性解决方案。

标签: c# wpf xaml


【解决方案1】:

为所有图像编写样式并将图像源绑定到 Tag 属性

       <Style TargetType="{x:Type Image}" x:Key="Image1Style">
            <Setter Property="Source" Value="{Binding Path=Tag,RelativeSource={RelativeSource Self}}"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Source" Value="{Binding Path=(local:AttachedProperty.AltSource),RelativeSource={RelativeSource Self}}"/>
                </Trigger>
            </Style.Triggers>
        </Style>

      <Image Tag="C:\Image1.jpg" local:AttachedProperty.AltSource="C:\Image2.jpg" Style="{StaticResource Image1Style}"/>

并编写附加属性以将替代图像源绑定到它

 public class AttachedProperty
{
    public static readonly DependencyProperty AltSourceProperty =
        DependencyProperty.RegisterAttached("AltSource",
        typeof(string), typeof(AttachedProperty),
        new PropertyMetadata());

    public static string GetAltSource(DependencyObject obj)
    {
        return (string)obj.GetValue(AltSourceProperty);
    }
    public static void SetAltSource(DependencyObject obj, string value)
    {
        obj.SetValue(AltSourceProperty, value);
    }
}

【讨论】:

    【解决方案2】:

    为您的项目添加一个新的资源字典,并将其命名为 ImageStyles.xaml

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:projectName">
    <Style TargetType="{x:Type Image}" x:Key="Image1Style">
        <Setter Property="Source" Value="C:\Image1.jpg"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Source" Value="C:\Image2.jpg"/>
            </Trigger>
        </Style.Triggers>
    </Style>
    

    然后您可以从您的 xaml 中将其作为静态资源引用 enter code here

     <image style={static resource="Image1Style"} />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-20
      • 2011-06-01
      • 2019-07-15
      • 1970-01-01
      • 2017-11-02
      • 1970-01-01
      • 2016-10-24
      • 1970-01-01
      相关资源
      最近更新 更多