[WPF] MaterialDesignConverter

MaterialDesignConverter

    [ValueConversion(typeof(string),typeof(MaterialDesignThemes.Wpf.PackIconKind))]
    public class MaterialDesignConverter : IValueConverter
    {

        public static MaterialDesignConverter Instance = new MaterialDesignConverter();

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            switch ((string)value)
            {
                case "Home":
                    return MaterialDesignThemes.Wpf.PackIconKind.Home;
                case "Report":
                    return MaterialDesignThemes.Wpf.PackIconKind.ReportBar;
                case "User":
                    return MaterialDesignThemes.Wpf.PackIconKind.TicketUser;
                case "Library":
                    return MaterialDesignThemes.Wpf.PackIconKind.Library;
                case "Music":
                    return MaterialDesignThemes.Wpf.PackIconKind.Music;
                default:
                    return null;
            }
                
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

RootView.xaml

<RadioButton Content="Home" BorderThickness="0" Height="50" Foreground="White"  FontSize="14" IsChecked="True" Grid.Row="0" Style="{StaticResource RadioButtonTheme}" />
<RadioButton Content="Report" BorderThickness="0" Height="50" Foreground="White"  FontSize="14"  Grid.Row="1" IsChecked="False" Style="{StaticResource RadioButtonTheme}"/>
<RadioButton Content="User" BorderThickness="0" Height="50" Foreground="White"  FontSize="14"  Grid.Row="2" IsChecked="False" Style="{StaticResource RadioButtonTheme}"/>
<RadioButton Content="Library" BorderThickness="0" Height="50" Foreground="White"  FontSize="14"  Grid.Row="3" IsChecked="False" Style="{StaticResource RadioButtonTheme}"/>
<RadioButton Content="Music" BorderThickness="0" Height="50" Foreground="White"  FontSize="14"  Grid.Row="4" IsChecked="False" Style="{StaticResource RadioButtonTheme}"/>

style.xaml

    <Style TargetType="{x:Type RadioButton}" x:Key="RadioButtonTheme" >
        <Style.Setters>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="RadioButton">
                        <DockPanel LastChildFill="True">
                            <TextBlock x:Name="txtBlock" Width="15" HorizontalAlignment="Left"></TextBlock>
                            <StackPanel Orientation="Horizontal" Background="{TemplateBinding Background}">
                                <materialDesign:PackIcon Kind="{TemplateBinding Property=Content, Converter={x:Static local:MaterialDesignConverter.Instance}}" Width="25" Height="25" VerticalAlignment="Center" />
                                <TextBlock Text="{TemplateBinding Property=Content}" VerticalAlignment="Center" Margin="25,0,0,0"></TextBlock>
                            </StackPanel>
                        </DockPanel>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter TargetName="txtBlock" Property="Background" Value="#BF1B3B"/>
                                <Setter Property="Background" Value="#322f30"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

            <Setter Property="Background" Value="Transparent"/>

        </Style.Setters>
    </Style>

 

posted @ 2022-06-02 14:06  WikiChen  阅读(84)  评论(0编辑  收藏  举报