WPF styling . Have no idea how to do it

北城余情 提交于 2019-12-13 04:58:54

问题


I am trying to make an image button in WPF based on the following article

Custom button template in WPF

The problem with this is that I have No Idea how to make the image to become grayscale when disabling the button. So I am loading two images, But I can not load a second images into ButtonProperties. So my idea was to load an rgb and greyscale image, and as I disable the button one image is hidden and another shown, and vice versa. and also the text to get greyed out. As I think, this coulde be done very nicely with triggers.

Here is my code:

a) style

<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) code:

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) control:

<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>

Any suggestions or ideas. I am just trying to get the effect when I disable the ImageButton, The image gets greyedOut as well as the text.

Suggestions?

来源:https://stackoverflow.com/questions/16649310/wpf-styling-have-no-idea-how-to-do-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!