27
目前,我正在處理一個簡單的自定義按鈕,它使用用戶提供的圖像作爲按下和正常狀態的背景。我有很多按鈕,所以我決定編寫一個自定義按鈕併爲按下的和正常狀態的圖片實現兩個屬性。TemplateBinding對DependencyProperty的自定義控件不起作用
這裏是我使用
public partial class ThemeableButton : Button
{
public ThemeableButton()
{
InitializeComponent();
}
public static readonly DependencyProperty PressedContentBackgroundSourceProperty = DependencyProperty.Register(
"PressedContentBackgroundSource", typeof(ImageSource), typeof(ThemeableButton), null);
public ImageSource PressedContentBackgroundSource
{
get { return (ImageSource)GetValue(PressedContentBackgroundSourceProperty); }
set
{
(value as BitmapImage).CreateOptions = BitmapCreateOptions.BackgroundCreation;
SetValue(PressedContentBackgroundSourceProperty, value);
}
}
public static readonly DependencyProperty NormalContentBackgroundSourceProperty =
DependencyProperty.Register("NormalContentBackgroundSource", typeof(ImageSource), typeof(ThemeableButton), null);
public ImageSource NormalContentBackgroundSource
{
get { return (ImageSource)GetValue(NormalContentBackgroundSourceProperty); }
set
{
(value as BitmapImage).CreateOptions = BitmapCreateOptions.BackgroundCreation;
SetValue(NormalContentBackgroundSourceProperty, value);
}
}
}
我寫此按鈕的樣式如下
<Style x:Key="ThemeableButtonTemplate" TargetType="MJbox_UIComponents_Controls:ThemeableButton">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/>
<Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
<Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
<Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}"/>
<Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="MJbox_UIComponents_Controls:ThemeableButton">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Source" Storyboard.TargetName="ButtonBackground">
<DiscreteObjectKeyFrame KeyTime="0" Value="{TemplateBinding NormalContentBackgroundSource}">
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Source" Storyboard.TargetName="ButtonBackground">
<DiscreteObjectKeyFrame KeyTime="0" Value="{TemplateBinding PressedContentBackgroundSource}">
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0">
<Image x:Name="ButtonBackground" Stretch="None" Source="{TemplateBinding NormalContentBackgroundSource}"/>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我想一個簡單的例子
<Controls:ThemeableButton Style="{StaticResource ThemeableButtonTemplate}" x:Name="btnDidntNeedIt" NormalContentBackgroundSource="{Binding Source={StaticResource DefaultTheme}, Path=DidntHaveButtonUnselected}"
PressedContentBackgroundSource="{Binding Source={StaticResource DefaultTheme}, Path=DidntHaveButtonSelected}"
/>
但圖像的代碼沒有顯示,我嘗試從樣式中刪除TemplateBinding,並將其替換爲圖像的相對源文件,它工作正常。我只是不想爲應用上的每個按鈕創建一個自定義樣式。任何可能的解決方法?
感謝科林,工作得很好,並保存了我的工作音色 – Waleed
這是工作的答案對我也是。我希望TemplateBinding起作用,並且一些神祕和隨意性被消除了。 – TernaryTopiary
「... TemplateBinding不適用於控件[。]上的自定義依賴項屬性」它適用於我。在我的自定義控件的ControlTemplate中使用我的'FillBrush'自定義依賴項屬性填充'Rectangle',甚至在VS的屬性表中顯示該屬性,並用我設置的任何屬性填充該屬性來填充'