WFP 带图片CheckBox(使用同一样式,设置Tag更改图片源)

实例下载:WFP带图片CheckBox(使用同一样式,设置Tag更改图片源)-C#文档类资源-CSDN下载

前台xaml代码:

<Window x:Class="Test.Window1"
        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:Test"
        mc:Ignorable="d"
        Title="Window1" Height="450" Width="800">
    <Grid>
        <CheckBox x:Name="cbxPrimaryPeriod" Width="260" Height="350" Tag="pack://application:,,,/Images/ic_bird.png"
                   Style="{StaticResource selectImgCheckBoxStyle }" VerticalAlignment="Center" HorizontalAlignment="Center" />
    </Grid>
</Window>

字典:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:Test">
    <!--值转换器-->
    <local:StringToImageSourceConverter x:Key="strToImageSource"></local:StringToImageSourceConverter>
    <!--选择框样式 照片选择-->
    <Style x:Key="selectImgCheckBoxStyle" TargetType="CheckBox">
        <Setter Property="IsChecked" Value="False"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Foreground" Value="#555"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="CheckBox">
                    <Grid  ToolTip="{TemplateBinding Content}" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
                        <!--<Border Name="imgBorder"   CornerRadius="6" Background="#000000" />-->

                        <Image Stretch="Fill" x:Name="_img" Source="{TemplateBinding Tag, Converter={StaticResource strToImageSource}}" >
                            <!--<Image.OpacityMask>
                                <VisualBrush Visual="{Binding ElementName=imgBorder, Mode=OneTime}" />
                            </Image.OpacityMask>-->
                        </Image>
                        <Border Name="maskBorder" CornerRadius="6" Background="#000000" Opacity="0.7" Visibility="Collapsed"/>
                        <Image x:Name="selectIcon" Width="50" Height="50" VerticalAlignment="Center" HorizontalAlignment="Center" Source="pack://application:,,,/Images/ic_sel.png" Visibility="Collapsed"/>
                    </Grid>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="true">
                            <Setter TargetName="maskBorder" Property="Visibility" Value="Visible"/>
                            <Setter TargetName="selectIcon" Property="Visibility" Value="Visible"/>
                        </Trigger>
                        <Trigger Property="IsChecked" Value="false">
                            <Setter TargetName="maskBorder" Property="Visibility" Value="Collapsed"/>
                            <Setter TargetName="selectIcon" Property="Visibility" Value="Collapsed"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

转换器:

    public sealed class StringToImageSourceConverter : IValueConverter
    {
        #region Converter

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return null;
            string path = (string)value;
            if (!string.IsNullOrEmpty(path))
            {
                return new BitmapImage(new Uri(path, UriKind.Absolute));
            }
            else
            {
                return null;
            }

        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
        #endregion

    }

猜你喜欢

转载自blog.csdn.net/lvxingzhe3/article/details/121470161
今日推荐