我正在尝试根据以下文章在WPF中创建一个图像按钮
问题在于我无法在禁用按钮时如何使图像变为灰度。所以我正在加载两个图像,但我无法将第二个图像加载到ButtonProperties中。 所以我的想法是加载一个rgb和灰度图像,当我禁用按钮时,一个图像被隐藏而另一个图像被显示,反之亦然。还有要变灰的文字。我认为,这个问题可以用触发器很好地完成。
这是我的代码:
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时获得效果,图像获得greyedOut以及文本。
建议?