Hiện tại, tôi đang làm việc trên một nút tùy chỉnh đơn giản sử dụng hình ảnh do người dùng cung cấp làm nền cho trạng thái được nhấn và bình thường. Tôi đã có rất nhiều nút vì vậy tôi quyết định viết một nút tùy chỉnh và thực hiện hai thuộc tính cho hình ảnh của các trạng thái được nhấn và bình thường.TemplateBinding để DependencyProperty trên một điều khiển tùy chỉnh không hoạt động
Đây là mã Tôi đang sử dụng
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);
}
}
}
tôi đã viết kiểu cho nút này như sau
<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>
Tôi đã thử một ví dụ đơn giản
<Controls:ThemeableButton Style="{StaticResource ThemeableButtonTemplate}" x:Name="btnDidntNeedIt" NormalContentBackgroundSource="{Binding Source={StaticResource DefaultTheme}, Path=DidntHaveButtonUnselected}"
PressedContentBackgroundSource="{Binding Source={StaticResource DefaultTheme}, Path=DidntHaveButtonSelected}"
/>
nhưng hình ảnh không hiển thị, tôi đã thử bằng cách xóa TemplateBinding khỏi kiểu và thay thế nó bằng nguồn tương đối thành hình ảnh và nó hoạt động tốt. Tôi chỉ không muốn tạo kiểu tùy chỉnh cho mỗi nút trên ứng dụng. Bất kỳ cách giải quyết nào có thể?
Cảm ơn Colin, đã làm việc tốt và đã lưu lại cho tôi âm điệu công việc – Waleed
Đây là câu trả lời đã hoạt động cho tôi nữa. Tôi ước rằng TemplateBinding chỉ làm việc và rằng một số bí ẩn và arbitrariness đã được xua tan. – TernaryTopiary
"... TemplateBinding không hoạt động cho các thuộc tính phụ thuộc tùy chỉnh trên các điều khiển [.]" Nó hoạt động cho tôi. ' ' trong 'ControlTemplate' của điều khiển tùy chỉnh của tôi điền vào' Rectangle' với thuộc tính phụ thuộc 'FillBrush' của tôi, thậm chí hiển thị thuộc tính đó trong trang thuộc tính của VS và điền nó với bất kỳ thứ gì tôi đã đặt nó trong thời gian thiết kế. Có lẽ đó là một cái gì đó mới từ năm 2011? Dù sao, nó hoạt động tốt ngay bây giờ. (Nhưng hãy cẩn thận với những thứ không phải là 'Freezeable'. Bạn vẫn không thể làm điều đó.) –