您可以通过 WPF 样式将数据绑定到 CornerRadius 吗?

发布于 2024-07-09 17:39:23 字数 1636 浏览 12 评论 0原文

我有一个 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>

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

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>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你对谁都笑 2024-07-16 17:39:23

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

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

∞梦里开花 2024-07-16 17:39:23

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

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.

峩卟喜欢 2024-07-16 17:39:23

这是可能的

<Window.DataContext>
    <local:ButtonCornerRadius/>
</Window.DataContext>
<Window.Resources>
    <Style TargetType="{x:Type Button}" BasedOn="{x:Null}">
        <Setter Property="FocusVisualStyle" Value="{DynamicResource MyButtonFocusVisual}"/>
        <Setter Property="Background" Value="{DynamicResource MyButtonBackgroundBrush}"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="BorderBrush" Value="{DynamicResource MyButtonBorderBrush}"/>
        <Setter Property="BorderThickness" Value="3"/>
        <Setter Property="FontFamily" Value="Segoe UI"/>
        <Setter Property="FontSize" Value="14" />
        <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="{Binding c, UpdateSourceTrigger=PropertyChanged}" 
                                Background="{TemplateBinding Background}" 
                                BorderBrush="{TemplateBinding BorderBrush}" 
                                BorderThickness="{TemplateBinding BorderThickness}" 
                                Padding="{TemplateBinding Padding}">
                            <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</Window.Resources>
<Grid>
    <StackPanel Orientation="Vertical">
        <TextBox 
            Text="{Binding CornerRad,
                    Mode=TwoWay,
                    UpdateSourceTrigger=PropertyChanged}" 
            Width="200" Height="40" 
            HorizontalContentAlignment="Center"
            VerticalContentAlignment="Center"
            FontSize="22" 
            Background="#FFB76464"/>
        <Button Content="{Binding CornerRad}" Background="#FF8A2929" FontSize="22" Width="200" Height="50" Margin="0,100,0,0" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        <Border Background="Red" 
                Margin="0 10 0 0" 
                CornerRadius="{Binding c,UpdateSourceTrigger=PropertyChanged}"
                Width="200" Height="50"/>
    </StackPanel>
</Grid>

并且在后面的代码中:

Imports System.ComponentModel

    Public Class ButtonCornerRadius
Implements INotifyPropertyChanged

Private _CornerRad As String = 10
Private _c As CornerRadius
Public Property CornerRad As Double
    Get
        Return _CornerRad
    End Get
    Set
        _CornerRad = Value
        c = New CornerRadius(Value)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(NameOf(CornerRad)))
    End Set
End Property
Public Property c As CornerRadius
    Get
        Return _c
    End Get
    Set
        _c = Value
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("c"))
    End Set
End Property

Public Sub New()
    Try
        ' This call is required by the designer.
        'InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        CornerRad = New String("2")
        'c = New CornerRadius(50)
        'DataContext = Me
    Catch ex As Exception
        MsgBox("")
    End Try

End Sub


Private Sub ButtonCornerRadius_Closed(sender As Object, e As EventArgs) Handles Me.Closed
    Application.Current.Shutdown()
End Sub



Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

End Class

It is possible

<Window.DataContext>
    <local:ButtonCornerRadius/>
</Window.DataContext>
<Window.Resources>
    <Style TargetType="{x:Type Button}" BasedOn="{x:Null}">
        <Setter Property="FocusVisualStyle" Value="{DynamicResource MyButtonFocusVisual}"/>
        <Setter Property="Background" Value="{DynamicResource MyButtonBackgroundBrush}"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="BorderBrush" Value="{DynamicResource MyButtonBorderBrush}"/>
        <Setter Property="BorderThickness" Value="3"/>
        <Setter Property="FontFamily" Value="Segoe UI"/>
        <Setter Property="FontSize" Value="14" />
        <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="{Binding c, UpdateSourceTrigger=PropertyChanged}" 
                                Background="{TemplateBinding Background}" 
                                BorderBrush="{TemplateBinding BorderBrush}" 
                                BorderThickness="{TemplateBinding BorderThickness}" 
                                Padding="{TemplateBinding Padding}">
                            <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</Window.Resources>
<Grid>
    <StackPanel Orientation="Vertical">
        <TextBox 
            Text="{Binding CornerRad,
                    Mode=TwoWay,
                    UpdateSourceTrigger=PropertyChanged}" 
            Width="200" Height="40" 
            HorizontalContentAlignment="Center"
            VerticalContentAlignment="Center"
            FontSize="22" 
            Background="#FFB76464"/>
        <Button Content="{Binding CornerRad}" Background="#FF8A2929" FontSize="22" Width="200" Height="50" Margin="0,100,0,0" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        <Border Background="Red" 
                Margin="0 10 0 0" 
                CornerRadius="{Binding c,UpdateSourceTrigger=PropertyChanged}"
                Width="200" Height="50"/>
    </StackPanel>
</Grid>

and in the back code:

Imports System.ComponentModel

    Public Class ButtonCornerRadius
Implements INotifyPropertyChanged

Private _CornerRad As String = 10
Private _c As CornerRadius
Public Property CornerRad As Double
    Get
        Return _CornerRad
    End Get
    Set
        _CornerRad = Value
        c = New CornerRadius(Value)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(NameOf(CornerRad)))
    End Set
End Property
Public Property c As CornerRadius
    Get
        Return _c
    End Get
    Set
        _c = Value
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("c"))
    End Set
End Property

Public Sub New()
    Try
        ' This call is required by the designer.
        'InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        CornerRad = New String("2")
        'c = New CornerRadius(50)
        'DataContext = Me
    Catch ex As Exception
        MsgBox("")
    End Try

End Sub


Private Sub ButtonCornerRadius_Closed(sender As Object, e As EventArgs) Handles Me.Closed
    Application.Current.Shutdown()
End Sub



Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

End Class

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文