XAML 将图像添加为扩展器图标WPF

cetgtptt  于 2023-05-27  发布在  其他
关注(0)|答案(1)|浏览(164)

我试图添加一个图像作为扩展器的图标,但当试图设置新图像时,当按钮被切换以显示扩展器中的内容时,我被卡住了。我已经切碎了扩展器模板,使我自己的,因为从我所看到的,这是唯一的方式,使它的自定义图标。这是我目前拥有的,当它未选中时,它会显示初始图像,但我不知道如何在<Trigger Property="IsChecked" Value="true">中更改图像源:

<Style x:Key="ExpanderDownHeaderStyle" TargetType="{x:Type ToggleButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Border Padding="{TemplateBinding Padding}">
                            <Grid SnapsToDevicePixels="False">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="19"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Image Source="Assets\icons\hide.png"/>
                                <ContentPresenter Grid.Column="1" HorizontalAlignment="Left" Margin="4,0,0,0" RecognizesAccessKey="True" SnapsToDevicePixels="True" VerticalAlignment="Center"/>
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="true">

                            #Change image when button is checked want to change to Assets\icons\show.png

                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
etc...
nafvub8i

nafvub8i1#

您可以在触发器中使用Setter更新Image的Source属性:

<Style x:Key="ExpanderDownHeaderStyle"
        TargetType="{x:Type ToggleButton}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToggleButton}">
                <Border Padding="{TemplateBinding Padding}">
                    <Grid SnapsToDevicePixels="False">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="19" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Image Source="Assets\icons\hide.png"
                                Name="icon" />
                        <ContentPresenter Grid.Column="1"
                                            HorizontalAlignment="Left"
                                            Margin="4,0,0,0"
                                            RecognizesAccessKey="True"
                                            SnapsToDevicePixels="True"
                                            VerticalAlignment="Center" />
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked"
                                Value="true">
                        <Setter Property="Source"
                                Value="Assets\icons\show.png"
                                TargetName="icon" />
                    </Trigger>
                    <Trigger Property="IsChecked"
                                Value="False">
                        <Setter Property="Source"
                                Value="Assets\icons\hide.png"
                                TargetName="icon" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

相关问题