focusVisualStyle与自定义依赖关系绑定



我正在尝试更改实现cornerradius depentencyProperty的派生按钮的焦点风格。一切都适合按钮样式,但是我不知道如何将Cornerradius值发送到焦点风格。

在这里我当前用于焦点的代码:

<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)));
    }

}

主题 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);
        };
    }

为什么不使用 elementName - binding?

我已经简化了您的代码,以提供有关我的建议的插图。

   <Style x:Key="FocusVisualStyle">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Border BorderBrush="Red" BorderThickness="1" CornerRadius="{Binding ElementName=border, Path=CornerRadius}">
                        <Label Foreground="{Binding ElementName=rectangle, Path=Fill}">Template</Label>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

查看

<StackPanel>
    <Label Style="{StaticResource FocusVisualStyle}" Height="30"/>
    <Rectangle x:Name="rectangle" Height="30" Fill="Green"/>
    <Border x:Name="border" CornerRadius="15"/>
</StackPanel>

您可以看到样式应用于Stackpanel内部的标签。模板内的边框从外部得到它的Cornerradius(从stackpanel内部的边界 border>)。

)。

如果元素名称不是正确的方法,请发布更多代码以查看您的按钮在哪里以及如何访问。

相关内容

  • 没有找到相关文章

最新更新