首页 > 解决方案 > 使用样式设置按钮内容时的 WPF 奇怪行为

问题描述

在编写 wpf net core 应用程序时,我决定使用 svg 格式的 vs2019 图标包中的图标。我像这样把它们放在单独的 xaml 文件中

<ControlTemplate x:Key="icSend">
        <Viewbox ToolTip="Send">
            <Rectangle Width="16" Height="16">
                <Rectangle.Fill>
                    <DrawingBrush>
                        <DrawingBrush.Drawing>
                            <DrawingGroup>
                                <DrawingGroup.Children>
                                    <GeometryDrawing Brush="#00FFFFFF" Geometry="F1M16,16L0,16 0,0 16,0z" />
                                    <GeometryDrawing Brush="#FFF6F6F6" Geometry="F1M0,2.1346L1.463,7.5006 0,12.8656 0,14.4666 16,8.1806 16,6.8196 0,0.533600000000001z" />
                                    <GeometryDrawing Brush="#FF424242" Geometry="F1M3.4004,8L10.9924,8 2.4894,11.341z M2.4894,3.659L10.9924,7 3.4004,7z M15.0004,7.5L1.0004,2 2.5004,7.5 1.0004,13z" />
                                </DrawingGroup.Children>
                            </DrawingGroup>
                        </DrawingBrush.Drawing>
                    </DrawingBrush>
                </Rectangle.Fill>
            </Rectangle>
        </Viewbox>
    </ControlTemplate>

将其合并到 app.xaml

 <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/SharedCodeWpf;component/Icons.xaml" x:Name="Icons" />
                <ResourceDictionary Source="/SharedCodeWpf;component/Converters.xaml" x:Name="Converters" />
            </ResourceDictionary.MergedDictionaries>

是的。它在一个单独的项目中

然后以这种方式使用它们

                <Button  Style="{StaticResource btIcon}" ToolTip="Save file" 
                         CommandParameter="{Binding ElementName=GridUploaded,  Path=SelectedItem}"
                         Command="{Binding cmdSaveUploaded}">
                    <ContentControl Template="{StaticResource icSave}" Style="{StaticResource ctlIcon}"/>
                </Button>

我决定摆脱多余的打字。首先我尝试继承按钮,但这是不同的故事。

然后我决定使用样式,更改了 btIcon 样式,现在我有了以下内容

 <Style TargetType="ContentControl" x:Key="ctlIcon">
        <Setter Property="Height" Value="24" />
        <Setter Property="Width" Value="24" />
    </Style>

    <Style TargetType="Button" x:Key="btIcon">
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="Margin" Value="3" />
        <Setter Property="Content">
            <Setter.Value>
                <ContentControl Style="{StaticResource ctlIcon}" Template="{Binding RelativeSource={RelativeSource AncestorLevel=1, AncestorType={x:Type Button}}, Path=Tag}" />
            </Setter.Value>
        </Setter>
    </Style>

是的,我正在尝试使用按钮标签将图标传递给内容控件,现在我的用法非常好

                <StackPanel Orientation="Horizontal" Grid.Column="1">
                    <Label Content="buttons start" />
                    <Button Style="{StaticResource btIcon}" Tag="{StaticResource icSave}" BorderThickness="1"/>
                    <Button Style="{StaticResource btIcon}" Tag="{StaticResource icAdd}" BorderThickness="1"/>
                    <Button Style="{StaticResource btIcon}" Tag="{StaticResource icOpenFile}" BorderThickness="1"/>
                    <Label Content="buttons end" />
                </StackPanel>

但是,仅显示最后一个按钮图标。

只显示最后一个图标

我试图改变绑定(风格),但没有比这更好的了。输出窗口中没有错误。

我对 wpf 没有很多经验,所以可能是我遗漏了一些非常简单的东西,或者有更好的方法来做“iconbutton”。

所以。这种行为是 wpf 中的某个错误,或者我在 style/xaml/其他东西上做错了什么?

TIA

是的

标签: c#wpf-controls

解决方案


推荐阅读