我用this MSDN tutorial为我的窗口的所有Button
控件创建了一个眼睛糖果外观,并且工作正常。为了使它更具可重用性,我尝试将所有内容放在UserControl中:我创建了一个ImageButton UC,然后将所有<Style>
从<Window.Resources>
封装到<UserControl.Resources>
。UserControl中的参数化风格?
然后,我改变了我的Button实例在XAML,从:
<Button Tag="Face.jpg" Content="Foo" />
要:
<uc:ImageButton Tag="Face.jpg" Content="Foo" />
而且款式停止被应用。
这里是UC代码:
<UserControl x:Class="GDTI.UI.Main.View.UserControls.ImageButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<Style TargetType="Button">
<Setter Property="MaxWidth" Value="250" />
<Setter Property="Margin" Value="5" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" >
<Setter.Value>
<SolidColorBrush Color="Orange" Opacity="0.4" />
</Setter.Value>
</Setter>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate >
<StackPanel>
<Image Source="{Binding Tag,
RelativeSource={RelativeSource
FindAncestor,
AncestorType='Button'}}" />
<TextBlock Margin="10"
HorizontalAlignment="Center"
Text="{Binding Content,
RelativeSource={RelativeSource
FindAncestor,
AncestorType='Button'}}" />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Button/>
我缺少什么?
谢谢!
Gaaahhh!你有自定义的用户控件,你仍然使用'Tag'属性? (也很难说明问题出在你没有发布的代码中) – 2012-02-13 17:19:46
下一步是避免使用'Tag'属性,这就是为什么我要使用UCs ...现在发布代码 – 2012-02-13 17:35:07