M
M
Mikhail Petrovich2019-05-22 17:53:22
WPF
Mikhail Petrovich, 2019-05-22 17:53:22

How to solve the problem of displaying an image when using RelativeSource TemplatedParent?

Button style

<Style TargetType="Button" x:Key="Style_Navigation_Btn">
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button" >
                        <Border  
                            x:Name="border"
                            BorderBrush="#EFEFEF" 
                            BorderThickness ="0 0 0 1" 
                            Background="White"
                            Cursor="Hand">

                            <Grid >
                                
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="250"/>
                                </Grid.ColumnDefinitions>

                                <Image 
                                    x:Name="image"
                                    Width="30" 
                                    Height="50"
                                    Source ="{Binding Path=Tag, RelativeSource="{RelativeSource TemplatedParent}}" / >
             

                                <TextBlock 
                                    x:Name="text" 
                                    Foreground="#3094e6" 
                                    FontSize="20" 
                                    Margin="10 0 0 0" 
                                    VerticalAlignment="Center" 
                                    Text="{TemplateBinding Content}" 
                                    Grid.Column="1" />

                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="border" Property="Background" Value="#3094e6" />
                                <Setter TargetName="text" Property="Foreground" Value="white"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

Usage
<Button 
Command="{Binding bStorage}" 
Tag="C:\Users\akautdinov_ai\Desktop\StorageApp\StorageApp\Images\package.png" Style="{StaticResource Style_Navigation_Btn}"  
Content="Склад" />

The bottom line is that you have to write the entire path of the picture (Variants with multiBinding and StringFormat do not work). So here's how you can do it so that you enter the path from the current directory. Proven method please.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question