我用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