1

我有几个按钮,我设置如下:

<Button Template="{DynamicResource btnTmplt}" Command="{Binding ImgChgeCmd}" >
  <Button.Resources>
  <ControlTemplate x:Key="btnTmplt" TargetType="{x:Type Button}">
    <StackPanel x:Name="stPanel" HorizontalAlignment="Left" Width="Auto" 
                Height="Auto" Orientation="Horizontal">
    <Image x:Name="img" Source=""></Image>
    </StackPanel>
      <ControlTemplate.Triggers>
        <Trigger Property="IsEnabled" Value="False">
          <Trigger.Setters>
            <Setter TargetName="img" Property="Source" 
Here--------->      Value="{Binding FirstImage, Converter={StaticResource GrayScaleConverter}}" />
            <Setter TargetName="stPanel" Property="BitmapEffect">
              <Setter.Value>
                <BlurBitmapEffect Radius="1" />
              </Setter.Value>
            </Setter>
          </Trigger.Setters>
        </Trigger>
        <Trigger Property="IsEnabled" Value="True">
          <Trigger.Setters>
And Here--> <Setter TargetName="img" Property="Source" Value="{Binding FirstImage}" />
            <Setter TargetName="stPanel" Property="BitmapEffect" Value="{x:Null}" />
          </Trigger.Setters>
        </Trigger>
      </ControlTemplate.Triggers>
    </ControlTemplate>
  </Button.Resources>
</Button>

Button.Resources 部分在所有这些按钮中几乎相同。不同的两行是上面“Here--->”指示的值绑定。

这些差异允许我为每个按钮加载不同的图像。

有没有办法进行设置,这样我就不必为每个按钮复制 Button.Resources 了? (但仍然在每个按钮中加载不同的图像)。

4

1 回答 1

0

您可以创建自定义控件。我已经做了一个简单的例子,但你可以自定义控件以随心所欲地表现(它非常强大)。另外请注意 BitMapEffects 已被弃用,如果您尝试在 .net 4 中使用它们,您将遇到异常。无论如何...创建一个自定义控件,例如:

public class VaccanoButton : Button
{        
    static VaccanoButton()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(VaccanoButton), new FrameworkPropertyMetadata(typeof(VaccanoButton)));
    }

    public ImageSource FirstImage
    {
        get { return (ImageSource)GetValue(FirstImageProperty); }   
        set { SetValue(FirstImageProperty, value); }
    }

    // Using a DependencyProperty as the backing store for FirstImage.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty FirstImageProperty =
        DependencyProperty.Register("FirstImage", typeof(ImageSource), typeof(VaccanoButton), new UIPropertyMetadata(null));  
}

并在 Generic.xaml 中设置 xaml,如下所示:(注意:我取出了您的 GrayScaleConverter,因为我没有它的代码,只需将其作为资源放回原处,它应该可以工作)

<Style TargetType="{x:Type local:VaccanoButton}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:VaccanoButton}">
                <Button>
                    <StackPanel x:Name="stPanel" HorizontalAlignment="Left" Width="Auto"  
                Height="Auto" Orientation="Horizontal">
                        <Image x:Name="img"></Image>
                    </StackPanel>
                </Button>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                            <Setter TargetName="img" Property="Source" Value="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=FirstImage}" />
                            <Setter TargetName="stPanel" Property="BitmapEffect">
                                <Setter.Value>
                                    <BlurBitmapEffect Radius="1"  />
                                </Setter.Value>
                            </Setter>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="True">
                        <Trigger.Setters>
                            <Setter TargetName="img" Property="Source" Value="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=FirstImage}" />
                            <Setter TargetName="stPanel" Property="BitmapEffect" Value="{x:Null}" />
                        </Trigger.Setters>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

然后像这样使用它:

        <local:VaccanoButton FirstImage="\Untitled.png"/>
于 2012-07-04T02:22:34.623 回答