前提・実現したいこと
Style を継承して使いまわすために添付プロパティを使用したいと考えています。ベースとなる Style では Trigger を使用して条件に合ったプロパティを使用し、デザインに反映するようにしています。派生スタイルでは添付プロパティを設定するのみです。
なお制限としてコントロールの継承は手段として使用できません。
発生している問題・エラーメッセージ
実行時は設定した値がコントロールに反映されるのですが、デザイン時では反映されません。そのため画面デザインしづらくなっています。デザイン時でも設定した値が反映されるようにしたいと考えています。
該当のソースコード
XAML
1<Window x:Class="WpfApp11.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApp11" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="450" Width="800" Background="#dddddd"> 9 <Window.Resources> 10 <Style x:Key="BaseStyle" TargetType="Button"> 11 <Setter Property="Template"> 12 <Setter.Value> 13 <ControlTemplate TargetType="Button"> 14 <Border Background="{TemplateBinding Background}" BorderBrush="Black" BorderThickness="1"> 15 <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /> 16 </Border> 17 <ControlTemplate.Triggers> 18 <Trigger Property="IsPressed" Value="True"> 19 <Setter Property="Background" Value="{Binding Path=(local:ButtonProperties.PressedBackground), RelativeSource={RelativeSource Self}}"/> 20 </Trigger> 21 <Trigger Property="IsPressed" Value="False"> 22 <Setter Property="Background" Value="{Binding Path=(local:ButtonProperties.NormalBackground), RelativeSource={RelativeSource Self}}"/> 23 </Trigger> 24 <Trigger Property="IsEnabled" Value="False"> 25 <Setter Property="Background" Value="{Binding Path=(local:ButtonProperties.DisabledBackground), RelativeSource={RelativeSource Self}}"/> 26 </Trigger> 27 </ControlTemplate.Triggers> 28 </ControlTemplate> 29 </Setter.Value> 30 </Setter> 31 </Style> 32 <Style x:Key="ButtonStyle" TargetType="Button" BasedOn="{StaticResource BaseStyle}"> 33 <Setter Property="local:ButtonProperties.NormalBackground" Value="Pink" /> 34 <Setter Property="local:ButtonProperties.PressedBackground" Value="Blue" /> 35 <Setter Property="local:ButtonProperties.DisabledBackground" Value="Gray" /> 36 </Style> 37 </Window.Resources> 38 <Grid> 39 <Button Width="100" Height="30" Style="{StaticResource ButtonStyle}" Content="Test" /> 40 </Grid> 41</Window>
C#
1namespace WpfApp11 2{ 3 public static class ButtonProperties 4 { 5 public static readonly DependencyProperty NormalBackgroundProperty = 6 DependencyProperty.RegisterAttached("NormalBackground", typeof(Brush), typeof(ButtonProperties), new PropertyMetadata(null)); 7 8 public static Brush GetNormalBackground(DependencyObject obj) 9 { 10 return (Brush)obj.GetValue(NormalBackgroundProperty); 11 } 12 13 public static void SetNormalBackground(DependencyObject obj, Brush value) 14 { 15 obj.SetValue(NormalBackgroundProperty, value); 16 } 17 18 public static readonly DependencyProperty PressedBackgroundProperty = 19 DependencyProperty.RegisterAttached("PressedBackground", typeof(Brush), typeof(ButtonProperties), new PropertyMetadata(null)); 20 21 public static Brush GetPressedBackground(DependencyObject obj) 22 { 23 return (Brush)obj.GetValue(PressedBackgroundProperty); 24 } 25 26 public static void SetPressedBackground(DependencyObject obj, Brush value) 27 { 28 obj.SetValue(PressedBackgroundProperty, value); 29 } 30 31 public static readonly DependencyProperty DisabledBackgroundProperty = 32 DependencyProperty.RegisterAttached("DisabledBackground", typeof(Brush), typeof(ButtonProperties), new PropertyMetadata(null)); 33 34 public static Brush GetDisabledBackground(DependencyObject obj) 35 { 36 return (Brush)obj.GetValue(DisabledBackgroundProperty); 37 } 38 39 public static void SetDisabledBackground(DependencyObject obj, Brush value) 40 { 41 obj.SetValue(DisabledBackgroundProperty, value); 42 } 43 } 44}
試したこと
Setter の RelativeSource を AncestorType=Button や TemplateParent にしましたが、デザイン時だけでなく実行時にも反映されませんでした。
補足情報(FW/ツールのバージョンなど)
.NET Framework 4.6.1
Visual Studio 2015 / 2017
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/29 07:19