CornerRadius DependencyProperty を実装する派生ボタンの FocusVisualStyle を変更しようとしています。ボタン スタイルではすべてが機能しますが、CornerRadius 値を FocusVisualStyle に送信する方法がわかりません。
ここで、FocusVisualStyle の現在のコード:
<Style x:Key="FocusVisualStyle">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Border BorderBrush="{StaticResource MyFocusBorderBrush}"
BorderThickness="1"
CornerRadius="{Binding CornerRadius, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MyButton}}}"
SnapsToDevicePixels="True"
UseLayoutRounding="True"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
私はこの形式のバインディングも試しました:
CornerRadius="{Binding CornerRadius, RelativeSource={RelativeSource TemplatedParent}}"
どんな助けでもいいでしょう:)
編集:要求に応じて、ここにすべてのコードがあります:
MyButton.cs:
public class MyButton : Button
{
public int CornerRadius
{
get { return (int)GetValue(CornerRadiusProperty); }
set { SetValue(CornerRadiusProperty, value); }
}
// DependencyProperty as the backing store for CornerRadius
public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register(
"CornerRadius",
typeof(int),
typeof(MyButton),
new PropertyMetadata(3)
);
static MyButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyButton), new FrameworkPropertyMetadata(typeof(MyButton)));
}
}
Themes\Generic.xaml:
<Style x:Key="FocusVisualStyle">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Border BorderBrush="Red"
BorderThickness="1"
CornerRadius="{Binding CornerRadius, ElementName=background}"
SnapsToDevicePixels="True"
UseLayoutRounding="True" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type local:MyButton}">
<Setter Property="Content" Value="MyButton"/>
<Setter Property="Background" Value="DarkGray"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Height" Value="20"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisualStyle}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MyButton}">
<Border x:Name="background"
Background="{TemplateBinding Background}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{Binding CornerRadius, RelativeSource={RelativeSource TemplatedParent}}">
<ContentPresenter VerticalAlignment="Center"
HorizontalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter TargetName="background"
Property="Background"
Value="Gray" />
</Trigger>
<Trigger Property="IsPressed"
Value="True">
<Setter TargetName="background"
Property="Background"
Value="Black" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
編集:私は良い解決策を見つけたことがないことを考慮して、これが私がそれを解決した方法です:
public MyButton()
{
Loaded += (s, e) =>
{
string styleStr = "<Style xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>" +
"<Setter Property = \"Control.Template\"> " +
"<Setter.Value> " +
"<ControlTemplate> " +
"<Rectangle Margin = \"-2\" " +
"Stroke = \"" + Resource<SolidColorBrush>.GetColor("MaxFocusBorder") + "\" " +
"StrokeThickness = \"1\" " +
"StrokeDashArray = \"1 2\" " +
"RadiusX = \"" + CornerRadius + "\" " +
"RadiusY = \"" + CornerRadius + "\" " +
"SnapsToDevicePixels = \"True\" " +
"UseLayoutRounding = \"True\" /> " +
"</ControlTemplate> " +
" </Setter.Value> " +
"</Setter> " +
"</Style>";
FocusVisualStyle = (Style)XamlReader.Parse(styleStr);
};
}