你能通过WPF样式数据绑定到CornerRadius吗?

时间:2022-03-06 21:03:44

I have a Button style and can't seem to property databind the border's CornerRadius property to the template. This is a dependency property, so it should be data bindable. I wonder if I'm missing the right XAML syntax to use?

我有一个Button样式,似乎无法将边框的CornerRadius属性数据绑定到模板。这是一个依赖属性,因此它应该是数据可绑定的。我想知道我是否错过了正确使用的XAML语法?

<Style TargetType="{x:Type Button}" BasedOn="{x:Null}">         
      <Setter Property="FocusVisualStyle" Value="{DynamicResource MyButtonFocusVisual}"/>       
      <Setter Property="Background" Value="{DynamicResource MyButtonBackgroundBrush}"/>       
      <Setter Property="Foreground" Value="{DynamicResource MyButtonForegroundBrush}"/>
      <Setter Property="BorderBrush" Value="{DynamicResource MyButtonBorderBrush}"/>
      <Setter Property="BorderThickness" Value="3"/>
      <Setter Property="FontFamily" Value="Segoe UI"/>      
      <Setter Property="FontSize" Value="14" />
      <Setter Property="CornerRadius" Value="2" />
      <Setter Property="Template">          
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type Button}">
           <!-- We use Grid as a root because it is easy to add more elements to customize the button -->
           <Grid x:Name="Grid">
           <Border x:Name="Border" CornerRadius="{TemplateBinding CornerRadius}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"/> 
           </Grid> 
         </ControlTemplate>             
       </Setter.Value>      
     </Setter>                  
   </Style>

Both and CornerRadius="{TemplateBinding CornerRadius}" give me the error "CornerRadius is not recognized or is not accessible".

两者和CornerRadius =“{TemplateBinding CornerRadius}”给我错误“CornerRadius无法识别或无法访问”。

2 个解决方案

#1


5  

You're trying to set/bind a CornerRadius property on class Button, but there is no such property. So the error is expected.

您正尝试在类Button上设置/绑定CornerRadius属性,但没有此类属性。所以错误是预料之中的。

#2


1  

Kent is right. A way around this is to create your own custom control that inherits from the button class. Then inside this derived class, create a dependency property and register it to the window for the CornerRadius property. Then you may use the above code, but instead of setting the style property on a Button control, set the style property on the derived class.

肯特是对的。解决这个问题的方法是创建自己的自定义控件,该控件继承自按钮类。然后在此派生类中,创建一个依赖项属性并将其注册到CornerRadius属性的窗口。然后您可以使用上面的代码,但不是在Button控件上设置style属性,而是在派生类上设置style属性。

#1


5  

You're trying to set/bind a CornerRadius property on class Button, but there is no such property. So the error is expected.

您正尝试在类Button上设置/绑定CornerRadius属性,但没有此类属性。所以错误是预料之中的。

#2


1  

Kent is right. A way around this is to create your own custom control that inherits from the button class. Then inside this derived class, create a dependency property and register it to the window for the CornerRadius property. Then you may use the above code, but instead of setting the style property on a Button control, set the style property on the derived class.

肯特是对的。解决这个问题的方法是创建自己的自定义控件,该控件继承自按钮类。然后在此派生类中,创建一个依赖项属性并将其注册到CornerRadius属性的窗口。然后您可以使用上面的代码,但不是在Button控件上设置style属性,而是在派生类上设置style属性。