Это в основном продолжение моего предыдущего вопроса.
Мне удалось заставить эту работу работать с шаблонами, однако я хочу сделать ее немного общей, чтобы мне не приходилось повторять код повсюду
Рабочая версия (жестко закодированная) выглядит так:
<UserControl.Resources>
<ControlTemplate x:Key="TrebleCheckboxImageTemplate" TargetType="CheckBox">
<Image x:Name="imgTreble" MinWidth="100" Source="Images/treble_checked.png">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CheckStates">
<VisualState x:Name="Checked">
<Storyboard>
<ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="imgTreble" Storyboard.TargetProperty="(Image.Source)">
<DiscreteObjectKeyFrame KeyTime="00:00:00">
<DiscreteObjectKeyFrame.Value>
<BitmapImage UriSource="Images/treble_checked.png" />
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Unchecked">
<Storyboard>
<ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="imgTreble" Storyboard.TargetProperty="(Image.Source)">
<DiscreteObjectKeyFrame KeyTime="00:00:00">
<DiscreteObjectKeyFrame.Value>
<BitmapImage UriSource="Images/treble_unchecked.png" />
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Indeterminate"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Image>
</ControlTemplate>
</UserControl.Resources>
<StackPanel x:Name="LayoutRoot" Background="{StaticResource PhoneForegroundBrush}" Orientation="Horizontal">
<CheckBox Height="72" HorizontalAlignment="Left" Background="White" VerticalAlignment="Top" Template="{StaticResource TrebleCheckboxImageTemplate}" Margin="0,0,10,0" >
<Custom:Interaction.Triggers>
<Custom:EventTrigger EventName="Click">
<GalaSoft_MvvmLight_Command:EventToCommand CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=IsChecked}" Command="{Binding TreblePressedCommand}"/>
</Custom:EventTrigger>
</Custom:Interaction.Triggers>
</CheckBox>
</StackPanel>
Конечно, пути изображения жестко закодированы. Итак, я хотел сделать его общим, чтобы вы могли просто установить флажок, сказать ему, что это изображение, и шаблон будет одинаковым для всех.
Я создал класс управления ImageCheckbox:
public class ImageCheckbox : CheckBox
{
/// <summary>
/// The <see cref="CheckedImagePath" /> dependency property's name.
/// </summary>
public const string CheckedImagePathPropertyName = "CheckedImagePath";
/// <summary>
/// Gets or sets the value of the <see cref="CheckedImagePath" />
/// property. This is a dependency property.
/// </summary>
public string CheckedImagePath
{
get
{
return (string)GetValue(CheckedImagePathProperty);
}
set
{
SetValue(CheckedImagePathProperty, value);
}
}
/// <summary>
/// Identifies the <see cref="CheckedImagePath" /> dependency property.
/// </summary>
public static readonly DependencyProperty CheckedImagePathProperty = DependencyProperty.Register(
CheckedImagePathPropertyName,
typeof(string),
typeof(ImageCheckbox),
new PropertyMetadata(null));
/// <summary>
/// The <see cref="UnCheckedImagePath" /> dependency property's name.
/// </summary>
public const string UnCheckedImagePathPropertyName = "UnCheckedImagePath";
/// <summary>
/// Gets or sets the value of the <see cref="UnCheckedImagePath" />
/// property. This is a dependency property.
/// </summary>
public string UnCheckedImagePath
{
get
{
return (string)GetValue(UnCheckedImagePathProperty);
}
set
{
SetValue(UnCheckedImagePathProperty, value);
}
}
/// <summary>
/// Identifies the <see cref="UnCheckedImagePath" /> dependency property.
/// </summary>
public static readonly DependencyProperty UnCheckedImagePathProperty = DependencyProperty.Register(
UnCheckedImagePathPropertyName,
typeof(string),
typeof(ImageCheckbox),
new PropertyMetadata(null));
}
Я создал конвертер (потому что столкнулся с проблемой, что я должен преобразовать строку в Uri для источника изображения)
public class StringToImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
{
return null;
}
if (!UriParser.IsKnownScheme("pack"))
{
UriParser.Register(new GenericUriParser
(GenericUriParserOptions.GenericAuthority), "pack", -1);
}
if (value is string)
{
var image = new BitmapImage();
image.UriSource = new Uri(String.Format(@"pack://application:,,/Images/{0}", value as string));
//image.UriSource = new Uri(String.Format(@"pack://application:,,,/Adagio.Presentation;component/Images/{0}", value as string),UriKind.Absolute);
image.ImageFailed += new EventHandler<System.Windows.ExceptionRoutedEventArgs>(image_ImageFailed);
image.ImageOpened += new EventHandler<System.Windows.RoutedEventArgs>(image_ImageOpened);
return image;
}
if (value is Uri)
{
var bi = new BitmapImage {UriSource = (Uri) value};
return bi;
}
return null;
}
void image_ImageOpened(object sender, System.Windows.RoutedEventArgs e)
{
throw new NotImplementedException();
}
void image_ImageFailed(object sender, System.Windows.ExceptionRoutedEventArgs e)
{
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
вы видите, что конвертер был опробован с тысячами различных комбинаций, ни одна из них не работает
Затем новый xaml:
<ControlTemplate x:Key="CheckboxImageTemplate" TargetType="Controls:ImageCheckbox">
<Image x:Name="imgForTemplate" MinWidth="100" Source="{Binding RelativeSource={RelativeSource TemplatedParent},Path=CheckedImagePath, Converter={StaticResource stringToImageConverter}}">
<!--
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CheckStates">
<VisualState x:Name="Checked">
<Storyboard>
<ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="imgForTemplate" Storyboard.TargetProperty="(Image.Source)">
<DiscreteObjectKeyFrame KeyTime="00:00:00">
<DiscreteObjectKeyFrame.Value>
<BitmapImage UriSource="{Binding RelativeSource={RelativeSource TemplatedParent},Path=CheckedImagePath, Converter={StaticResource stringToImageConverter}}" />
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Unchecked">
<Storyboard>
<ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="imgForTemplate" Storyboard.TargetProperty="(Image.Source)">
<DiscreteObjectKeyFrame KeyTime="00:00:00">
<DiscreteObjectKeyFrame.Value>
<BitmapImage UriSource="{Binding RelativeSource={RelativeSource TemplatedParent},Path=UnCheckedImagePath, Converter={StaticResource stringToImageConverter}}" />
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Indeterminate"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>-->
</Image>
</ControlTemplate>
<StackPanel x:Name="LayoutRoot" Background="{StaticResource PhoneForegroundBrush}" Orientation="Horizontal">
<Controls:ImageCheckbox CheckedImagePath="treble_checked.png" UnCheckedImagePath="treble_unchecked.png" Height="72" HorizontalAlignment="Left" VerticalAlignment="Top" Template="{StaticResource CheckboxImageTemplate}" Margin="0,0,10,0" >
<Custom:Interaction.Triggers>
<Custom:EventTrigger EventName="Click">
<GalaSoft_MvvmLight_Command:EventToCommand CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=IsChecked}" Command="{Binding TreblePressedCommand}"/>
</Custom:EventTrigger>
</Custom:Interaction.Triggers>
</Controls:ImageCheckbox>
</StackPanel>
Сначала я пытался заставить все это работать, но мне, кажется, даже не удалось загрузить первое свойство Source изображения. Комментируемая часть (состояния VisualStateManager) тоже не работает, но я думаю, что это должна быть та же проблема ... в этом случае мне понадобится конвертер для возврата Uri вместо BitmapImage, потому что UriSource имеет тип Uri, и Источник имеет тип изображения.
Я получаю ошибки в конвертере, где я не могу загрузить изображения (всегда попадая в событие image_imageFailed). Я установил изображения как ресурсы в сборке ... что я делаю не так ?? это сводит меня с ума !!!!
[РЕДАКТИРОВАТЬ]: я попытался сделать, как предложено, и изменил свойство зависимости на Uri, но я не могу заставить его работать
Если я скажу
<Controls:ImageCheckbox CheckedImagePath="Images/treble_checked.png" UnCheckedImagePath="Images/treble_unchecked.png" Height="72" HorizontalAlignment="Left" VerticalAlignment="Top" Template="{StaticResource CheckboxImageTemplate}" Margin="0,0,10,0" >
и в VisualState шаблона:
<BitmapImage UriSource="{Binding Path=UnCheckedImagePath, RelativeSource={RelativeSource TemplatedParent}}" />
он говорит мне, что xaml недействителен и выдает ошибку. Если я использую TemplateBinding, как это:
<BitmapImage UriSource="{TemplateBinding UnCheckedImagePath}" />
не жалуется, но изображение не загружается (отображается пустым). Я думаю, что приближаюсь, но все еще не нашел решения ...
[ПРАВКА 2]: последняя попытка ...
использование:
<StackPanel x:Name="LayoutRoot" Background="{StaticResource PhoneForegroundBrush}" Orientation="Horizontal">
<Controls:ImageCheckbox Style="{StaticResource TheImageCheckboxStyle}" CheckedImagePath="Images/treble_checked.png" UnCheckedImagePath="Images/treble_unchecked.png" Height="72" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,10,0" >
<Custom:Interaction.Triggers>
<Custom:EventTrigger EventName="Click">
<GalaSoft_MvvmLight_Command:EventToCommand CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=IsChecked}" Command="{Binding TreblePressedCommand}"/>
</Custom:EventTrigger>
</Custom:Interaction.Triggers>
</Controls:ImageCheckbox>
</StackPanel>
стиль (попытка скопировать то, что вы вставили, и удалить то, что я считал ненужными частями)
<UserControl.Resources>
<Style x:Key="TheImageCheckboxStyle" TargetType="Controls:ImageCheckbox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="CheckBox">
<Grid Background="Transparent">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CheckStates">
<VisualState x:Name="Checked">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="CheckMark"
Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="CheckMark"
Storyboard.TargetProperty="Source">
<!-- Magic! -->
<DiscreteObjectKeyFrame KeyTime="0"
Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=CheckedImagePath}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Unchecked">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="CheckMark"
Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="CheckMark"
Storyboard.TargetProperty="Source">
<!-- Magic! -->
<DiscreteObjectKeyFrame KeyTime="0"
Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=UnCheckedImagePath}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid Margin="{StaticResource PhoneTouchTargetLargeOverhang}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="32" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Border x:Name="CheckBackground"
Width="32"
Height="32"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding Background}"
BorderThickness="{StaticResource PhoneBorderThickness}"
IsHitTestVisible="False" />
<Rectangle x:Name="IndeterminateMark"
Grid.Row="0"
Width="16"
Height="16"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Fill="{StaticResource PhoneRadioCheckBoxCheckBrush}"
IsHitTestVisible="False"
Visibility="Collapsed" />
<!-- Magic! Default to UnCheckedImagePath -->
<Image x:Name="CheckMark"
Width="24"
Height="18"
HorizontalAlignment="Center"
VerticalAlignment="Center"
IsHitTestVisible="False"
Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=UnCheckedImagePath}"
Stretch="Fill"
Visibility="Collapsed" />
<ContentControl x:Name="ContentContainer"
Grid.Column="1"
Margin="12,0,0,0"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Foreground="{TemplateBinding Foreground}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
Padding="{TemplateBinding Padding}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" />
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>