0

while writing wpf net core app i decided to use icons from vs2019 icon pack in svg format. I put them in separate xaml file like this

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

merged it in app.xaml

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

yes. it is in a separate project

and then use them this way

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

i decided to get rid of excess typing. first i tried to inherit button but that's different story.

then i decided to go with styles, changed btIcon style and now i have the following

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

yes, i am trying to use button Tag to pass icon to content control and i have now very nice usage

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

but.only last button icon is shown.

only last icon is shown

i tried to change binding (in style) but can't achieve anything better then this. there is no errors in output window.

i don't have lot of experience with wpf, so may be i am missing something very simple or there is better way to do "iconbutton".

so. this behaviour is a bug somewhere in wpf or i am doing something wrong with style/xaml/something else ?

TIA

ish

ish1313
  • 321
  • 3
  • 6
  • Does this answer your question? [Set an image as a Button's content in style](https://stackoverflow.com/questions/7250709/set-an-image-as-a-buttons-content-in-style) – the.Doc Nov 05 '21 at 09:06
  • well, not sure . i'd understand if i have same image for all buttons. i'd understand if i have now image for all buttons. but i don't understand why i have image only for last button :) – ish1313 Nov 05 '21 at 09:17
  • Have you tried the solution? Change your `Content` property in the style to `ContentTemplate` instead, and use a `DataTemplate` with your `ContentControl` as a child? – the.Doc Nov 05 '21 at 10:49
  • = no picture at all :) – ish1313 Nov 05 '21 at 12:20
  • switched to customcontrol based on Button: works like a charm – ish1313 Nov 05 '21 at 12:36

0 Answers0