本文介绍了您可以通过WPF样式将数据绑定到CornerRadius吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

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

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

推荐答案

您正试图在其上设置/绑定 CornerRadius 属性class Button ,但没有此类属性。因此,错误是预期的。

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

这篇关于您可以通过WPF样式将数据绑定到CornerRadius吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 21:03