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