简体   繁体   English

FocusVisualStyle 与自定义 DependencyProperty 绑定

[英]FocusVisualStyle binding with custom DependencyProperty

I'm trying to change the FocusVisualStyle of a derived button that implement a CornerRadius DependencyProperty.我正在尝试更改实现 CornerRadius DependencyProperty 的派生按钮的 FocusVisualStyle。 Everything works for the button style, but I can't figure out how to send the CornerRadius value to the FocusVisualStyle.一切都适用于按钮样式,但我不知道如何将 CornerRadius 值发送到 FocusVisualStyle。

Here my current code for the 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>

I have also try this form of binding:我也尝试过这种形式的绑定:

CornerRadius="{Binding CornerRadius, RelativeSource={RelativeSource TemplatedParent}}"

Any help would be nice :)任何帮助都会很好:)

EDIT : As requested, here would be all my code:编辑:根据要求,这里是我所有的代码:

MyButton.cs:我的按钮.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:主题\\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>

EDIT : Considering I never found a nice solution, here's how I solved it:编辑:考虑到我从来没有找到一个好的解决方案,这是我解决它的方法:

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

Why not to use an elementname -binding?为什么不使用elementname绑定?

I've simplified your code to give an illustration about my suggestion.我已经简化了您的代码,以说明我的建议。

   <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>

View查看

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

As you can see the style is applied to the label inside the stackpanel.如您所见,样式已应用于堆栈面板内的标签。 The border within the template is getting it's cornerradius from outside (from the border inside the stackpanel named border ).模板内的边框从外部(从名为border的堆栈面板内的边框)获取它的角半径。

If elementname-binding isn't the right approach for you plaese post some more code to see where your button is lying and how to get access.如果 elementname-binding 不是您的正确方法,请发布更多代码以查看您的按钮所在的位置以及如何访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自定义 UserControl DependencyProperty 绑定 - Custom UserControl DependencyProperty Binding WPF:不可能绑定到自定义DependencyProperty - WPF: Binding to custom DependencyProperty not possible 自定义 UserControl 的自定义 DependencyProperty 未正确绑定 - Custom DependencyProperty of custom UserControl not binding properly 将 ToolTip 绑定到自定义控件内的 DependencyProperty - Binding ToolTip to DependencyProperty inside custom control 在自定义用户控件的DependencyProperty上绑定不更新更新 - Binding on DependencyProperty of custom User Control not updating on change 绑定到 TreeView SelectedItem 用作 ItemsSource 但不适用于自定义 DependencyProperty - Binding to TreeView SelectedItem works as ItemsSource but not for custom DependencyProperty 将自定义控件中的DependencyProperty绑定到ViewModel属性 - Binding DependencyProperty in custom control to ViewModel property 将 ControlTemplate 子项绑定到自定义控件上的 DependencyProperty 不起作用 - Binding ControlTemplate child to DependencyProperty on a custom control not working 公开一个ViewModel事件以绑定到自定义DependencyProperty - Expose a ViewModel event for binding to a custom DependencyProperty 将自定义 DependencyProperty 绑定到 ListView.SelectedItem 的问题 - Issue with binding custom DependencyProperty to ListView.SelectedItem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM