C# 以样式更改按钮的内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1050737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change a button's content in a style?
提问by Thrash505
I'm trying to do something similar to this:
我正在尝试做类似的事情:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Button>
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Content"
Value="No mouse over" />
<Style.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="Content">
<Setter.Value>
<CheckBox Content="Mouse is over" />
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</Grid>
</Window>
However, I get a run-time XamlParseException with a message of:
但是,我收到了一个运行时 XamlParseException 的消息:
Cannot add content of type 'System.Windows.Controls.CheckBox' to an object of type 'System.Object'. Error at object 'System.Windows.Controls.CheckBox
无法将“System.Windows.Controls.CheckBox”类型的内容添加到“System.Object”类型的对象。对象“System.Windows.Controls.CheckBox”处出错
I'm actually trying to draw different icons for the button's content depending on external conditions. So I'm actually trying to use a DataTrigger, but the example above simplifies the problem. Any ideas?
我实际上是在尝试根据外部条件为按钮的内容绘制不同的图标。所以我实际上是在尝试使用 DataTrigger,但上面的示例简化了问题。有任何想法吗?
回答by apandit
WARNING:This may not be the best or correct way to do it. Make sure you read the other answers on this page as well.
警告:这可能不是最好或正确的方法。请确保您也阅读此页面上的其他答案。
Pretty sure you'd want to use a control template in this sort of situation. Something like:
在这种情况下,您肯定希望使用控件模板。就像是:
<style>
<Setter Property="Content">
<Setter.Value>
<ControlTemplate>
<Image Img="something.jpg" />
</ControlTemplate>
</Setter.Value>
</Setter>
</style>
And add a control template in the trigger for the on-hover.
并在触发器中添加一个用于悬停的控件模板。
Here's a good link
这是一个很好的链接
回答by rmoore
The actual error is occurring because Visuals can not be directly set as a Setter value. You can get the behavior you are looking for though, by setting the ContentTemplate using a DataTemplate, or by creating your content as a resource, either specific to the button or located elsewhere.
发生实际错误是因为 Visuals 不能直接设置为 Setter 值。但是,您可以通过使用 DataTemplate 设置 ContentTemplate 或通过将内容创建为资源(特定于按钮或位于其他位置)来获得您正在寻找的行为。
<Button>
<Button.Resources>
<CheckBox x:Key="Local_MouseOverContent" Content="Mouse is over" />
</Button.Resources>
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Content" Value="No mouse over" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Content"
Value="{StaticResource Local_MouseOverContent}" />
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
<Button>
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Content" Value="No mouse over" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate DataType="Button">
<CheckBox Content="Mouse is over" />
</DataTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
回答by Steve
If you are making a generic style to be used by buttons all around your app, you will get visual tree conflicts using the approach where the image is a resource. So the template is your only choice in that case.
如果您正在制作一个通用样式供您应用程序周围的按钮使用,则使用图像是资源的方法将导致视觉树冲突。因此,在这种情况下,模板是您唯一的选择。
回答by Rudolf
REMARK! Exactly your example works in .NET Framework 4without any Changes !!!!
评论!正是您的示例在.NET Framework 4 中工作,没有任何更改!!!!
<Button>
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Content"
Value="No mouse over" />
<Style.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="Content">
<Setter.Value>
<CheckBox Content="Mouse is over" />
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>