我是一个使用C#/WPF/Telerik控件的项目的新手。
有这样的风格:
<Style x:Key="MyButtonStyle" Target="{x:Type Button">
<Setter Property="Width" Value="28"/>
<Setter Property="Height" Value="28"/>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Image Source="/MyPrj;component/Images/mybutton.png"
x:Name="image"
Width="24"
Height="24"
Margin="-2,-2-2,-1"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>在XAML中,我可以使用如下样式:
<RadButton Style="{StaticResource MyButtonStyle}"/>,这个工作很好,。该按钮的大小为28x28像素,显示已定义的图像。
现在,我想以编程方式分配样式:
RadButton button = new RadButton();
button.Style = FindResource("MyButtonStyle") as Style;程序似乎找到了样式,因为按钮的大小是28x28像素。
但是它没有显示图像!按钮显示文本" image“而不是。
我做错了什么?
蒂娅!
编辑:
发布于 2013-11-15 09:26:56
Images在Styles中有一个不为人所知的怪癖。通过像这样定义图像,只有一个图像将被创建,永远,因此只能出现在您的一个按钮。因此,在不真正了解其余代码的情况下,我假设您有两个带有该样式的按钮。
要解决这个问题,您可以做的是使用属性x:Shared="false"分别创建图像,然后设置内容。然后,每次引用它时都会创建一个新的图像。
<Image x:Key="buttonImage" x:Shared="false" Source="/MyPrj;component/Images/mybutton.png"
Width="24" Height="24" Margin="-2,-2-2,-1"/>
<Style x:Key="MyButtonStyle" Target="{x:Type Button">
<Setter Property="Width" Value="28"/>
<Setter Property="Height" Value="28"/>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Border Content="{StaticResource buttonImage}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>https://stackoverflow.com/questions/19996346
复制相似问题