Button の ControlTemplate を変更するスタイルを作成しています (実際には右側に矢印を追加して、ボタンがドロップダウン ボタンのように見えるようにします (wpf にはありません))。
テンプレート内に実際のボタンを配置します (結果をボタンのようにする必要があるため - ContentTemplate のみを変更できますが、実際のコンテンツを矢印と一緒に表示する必要があり、ContentPresenter を ContentTemplate 内に配置するとスタック オーバーフローが発生します - wpf だけテンプレートをプレゼンターに展開し、何度も続けます)、矢印と共に。
私の問題は、ユーザーがボタンの背景を変更できるようにしたいので、バインドする必要があることBackground="{TemplateBinding Background}"
です。これにより、ユーザーは常に背景を提供する必要があり、そうでない場合は null になります。これを防ぐには、背景にデフォルト値を提供する必要があることを理解しているので、追加しました<Setter Property="Background" Value="default Background goes here"/>
問題は、このセッターの値はどうあるべきかということです。wpf の組み込みスタイルはすべてのアプリケーションで自動的に読み込まれると想定し、スタイルを提供する aero.normalcolor.xaml を調べてButtonNormalBackground
、後でデフォルトのボタン スタイルとして使用します<Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
(aero.normalcolor.xaml、47 行目)。
まさに私が望んでいたことですが、このセッターを使用すると、静的リソースが見つかりません。次に、それを DynamicResource に変更しましたが、その場合、ボタンの背景は透明のままです - ButtonNormalBackground は使用されません!
システムのボタンの外観をデフォルト値として提供するにはどうすればよいですか? ありがとうございました!
PS: BorderBrush、BorderThickness の dtto - うわあ、WPF の作成者が常に使用するトリックは許可されていないようです :/
私のスタイル:
<Style TargetType="{x:Type ToggleButton}" x:Key="DropDownArrowStyle">
<Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
<Setter Property="ToggleButton.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<ToggleButton
x:Name="PART_ToggleButton"
Margin="{TemplateBinding Margin}"
Padding="{TemplateBinding Padding}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
IsEnabled="{TemplateBinding IsEnabled}"
IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
ContextMenu="{TemplateBinding ContextMenu}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Foreground="{TemplateBinding Foreground}"
FocusVisualStyle="{TemplateBinding FocusVisualStyle}"
ClickMode="{TemplateBinding ClickMode}"
>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ContentPresenter
x:Name="ButtonContentPresenter"
HorizontalAlignment="Left"
VerticalAlignment="Stretch"
Margin="{TemplateBinding Padding}"
Content="{TemplateBinding Content}"
Grid.Column="0"
/>
<Border
x:Name="PART_DownArrowBorder"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="Transparent"
Focusable="False"
Grid.Column="1"
>
<Path
Focusable="False"
x:Name="PART_DownArrow"
Data="M 3,0 L 6.5,4 L 10,0"
Width="12"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Fill="Black" SnapsToDevicePixels="True"
>
</Path>
</Border>
</Grid>
</ToggleButton>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>