2016-11-22 118 views
0

我试图改变实现CornerRadius DependencyProperty的派生按钮的FocusVisualStyle。一切适用于按钮样式,但我无法弄清楚如何将CornerRadius值发送到FocusVisualStyle。FocusVisualStyle绑定自定义DependencyProperty

这里我目前的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))); 
    } 


} 

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

回答

0

为什么不使用元素名称 -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内侧的标签。模板中的边框从外部(从名为的边框的堆叠面板内的边框)获取它的角点半径。

如果elementname-binding不适合你,那么你需要更多的代码来查看你的按钮在哪里以及如何访问。