0

以下の記事を参考にWPFで画像ボタンを作ろうとしています。

WPF のカスタム ボタン テンプレート

これに関する問題は、ボタンを無効にするときに画像をグレースケールにする方法がわからないことです。だから私は2つの画像をロードしていますが、2番目の画像をButtonPropertiesにロードできません。したがって、私の考えは、RGB とグレースケールの画像をロードすることでした。ボタンを無効にすると、1 つの画像が非表示になり、別の画像が表示されます。その逆も同様です。グレーアウトするテキストも。私が思うに、これはトリガーを使用して非常にうまく行うことができます。

これが私のコードです:

a) スタイル

<Setter Property="ContentTemplate">
  <Setter.Value>
    <DataTemplate>
      <StackPanel Orientation="Horizontal">
        <Image Width="20"
               Name="imgEnabled"
               Source="{Binding Path=(ib:ButtonProperties.Image), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"></Image>
        <Image Width="20"
               Name="imgDisabled"
               Source="{Binding Path=(ib:ButtonProperties.Image), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"></Image>
        <ContentPresenter Content="{Binding Path=Content, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"></ContentPresenter>
      </StackPanel>
    </DataTemplate>
  </Setter.Value>
</Setter>  

b) コード:

public class ButtonProperties:DependencyObject
{

    public static readonly DependencyProperty ImageProperty =
        DependencyProperty.Register("Image", 
                                    typeof(ImageSource), 
                                    typeof(ButtonProperties), 
                                    new UIPropertyMetadata((ImageSource)null));



    public static ImageSource GetImage(DependencyObject obj)
    {
        return (ImageSource)obj.GetValue(ImageProperty);
    }

    public static void SetImage(DependencyObject obj, ImageSource value)
    {
        obj.SetValue(ImageProperty, value);
    }
}

3) コントロール:

<ItemsControl Name="icImageButtons"
              ItemsSource="{Binding}">
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <StackPanel Orientation="Horizontal" />
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <Button ToolTip="{Binding Tip}"
              ib:ButtonProperties.Image="{Binding EnabledSource}"
              Content="{Binding Text}"
              Style="{StaticResource ImageButton}" />
    </DataTemplate>

  </ItemsControl.ItemTemplate>
</ItemsControl>

提案やアイデア。ImageButton を無効にすると効果が得られようとしています。画像はテキストと同様に grayedOut になります。

提案?

4

0 に答える 0