2016-11-22 117 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不適合你,那麼你需要更多的代碼來查看你的按鈕在哪裏以及如何訪問。