如何从源代码绑定 Material 设计的 ButtonAssist CornerRadius 值

How to bind a ButtonAssist CornerRadius Value of Material Design from a source

我在 Material 设计中使用 C# WPF。

我需要什么:

我正在尝试为我的项目中的任何 Button 设置 CornerRadiusButtonAssist

问题:

我无法从 Application.Resources 设置这个 属性。

我的XAML:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:MaterialDesignation"
    xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
    x:Class="MaterialDesignation.MainWindow"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Window.Resources>
    
</Window.Resources>
<Grid>
    <!--I Want to Set Radius For All Buttons in My Projects like blow line ↓-->
    <Button Content="Button" materialDesign:ButtonAssist.CornerRadius="30" HorizontalAlignment="Left" Margin="71,260,0,0" VerticalAlignment="Top" Width="237" Height="85" Background="#FF038BEA" BorderBrush="{x:Null}"/>

    
    <Button Content="Button" Style="{StaticResource ResourceKey=Gerdy}"  HorizontalAlignment="Left" Margin="410,129,0,0" VerticalAlignment="Top" Width="237" Height="85" Background="#FF038BEA" BorderBrush="{x:Null}"/>
    <Button Content="Button" materialDesign:ButtonAssist.CornerRadius="{Binding Gerdy}" HorizontalAlignment="Left" Margin="71,129,0,0" VerticalAlignment="Top" Width="237" Height="85" Background="#FF038BEA" BorderBrush="{x:Null}"/>

</Grid>

我的App.xaml:

<Application x:Class="MaterialDesignation.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:clr="clr-namespace:System;assembly=mscorlib"
         xmlns:local="clr-namespace:MaterialDesignation"
         xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>
        
        <Style x:Key="Gerdy" TargetType="{x:Type Button}">
            <Style.Resources>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="materialDesign:ButtonAssist.CornerRadius" Value="30" />
                </Style>
            </Style.Resources>
        </Style>

        <clr:Byte x:Key="GerdyAsByte">30</clr:Byte>

        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

这是我的完整 WPF 项目 link: https://ufile.io/gt5dcjzh

请帮忙

Button 创建自定义 implicit 样式(没有 x:Key),该样式基于 MaterialDesign 样式并且仅更改 CornerRadiusButtonAssist。它将自动应用于范围内的所有 Button

为你的按钮样式创建一个单独的资源字典,这样它就可以添加在MaterialDesign的资源字典之后,否则它们会覆盖你的样式而不是你覆盖它们的默认样式。另请注意,本地值将始终覆盖样式中的值。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes">
   <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
      <Setter Property="materialDesign:ButtonAssist.CornerRadius" Value="30" />
   </Style>
</ResourceDictionary>

然后将它们添加到MergedDictionaries 之后 MaterialDesign 资源字典。

<Application x:Class="MaterialDesignation.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:clr="clr-namespace:System;assembly=mscorlib"
             xmlns:local="clr-namespace:MaterialDesignation"
             xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>

           <clr:Byte x:Key="GerdyAsByte">30</clr:Byte>

            <ResourceDictionary.MergedDictionaries>
               <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
               <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
               <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
               <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
               <ResourceDictionary Source="CustomButtonStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>