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

Time:02-17

I'm using C# WPF with Material Design .

What I need :

I'm trying to set CornerRadius of ButtonAssist for any Button in my project.

Problem:

I can't set this property from Application.Resources.

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

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

Here is my full WPF project link : https://ufile.io/gt5dcjzh

Please help

CodePudding user response:

Create a custom implicit style (without x:Key) for Button that is based on the MaterialDesign styles and only changes the CornerRadius of the ButtonAssist. It will be applied automatically to all Buttons in scope.

Create a separate resource dictionary for your button style, so it can be added after the resource dictionaries of MaterialDesign, otherwise they will override your style instead you overriding their default style. Please also be aware that local values will always override the values from the style.

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

Then add them to the MergedDictionaries after the MaterialDesign resource dictionaries.

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