0
votes

I have made a custom window control and am adding back in functionality from WPF's original control; minimize, maximize, close and Icon placement.

However I am lost as to how to allow the user to set the icon in there project using the Icon property, and use it in my control as an icon.

The controls base class is Window and the Icon property can be set.

XAML:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyCustomControls">
<!--  Button style -->
<Style TargetType="{x:Type Button}" x:Key="WindowButtonStyle">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ButtonBase}">
                <Border
                        x:Name="Chrome"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        Margin="0"
                        Background="{TemplateBinding Background}"
                        SnapsToDevicePixels="True">
                    <ContentPresenter
                            ContentTemplate="{TemplateBinding ContentTemplate}"
                            Content="{TemplateBinding Content}"
                            ContentStringFormat="{TemplateBinding ContentStringFormat}"
                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                            Margin="{TemplateBinding Padding}"
                            RecognizesAccessKey="True"
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="FontFamily" Value="Webdings"/>
    <Setter Property="FontSize" Value="13.333" />
    <Setter Property="Foreground" Value="Black" />
    <Setter Property="Margin" Value="0,2,3,0"/>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="#F0F8FF"/>
            <Setter Property="BorderBrush" Value="AntiqueWhite"/>
        </Trigger>
    </Style.Triggers>
</Style>

<!-- Window style -->
<Style TargetType="{x:Type local:CustomWindow}">
    <Setter Property="WindowStyle" Value="None"/>
    <Setter Property="ResizeMode" Value="NoResize"/>
    <Setter Property="Background" Value="White"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="BorderBrush" Value="Black"/>
    <Setter Property="Icon" Value="{Binding BindsDirectlyToSource=True}"/>
    <Setter Property="Template">

        <Setter.Value>
            <!-- window border area -->
            <ControlTemplate TargetType="{x:Type local:CustomWindow}">
                <Border BorderThickness="{TemplateBinding BorderThickness}"
                        BorderBrush="Black" Background="#E9ECFA">
                    <Grid>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition />
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <Rectangle x:Name="moveRectangle" Fill="Transparent"
                                       Grid.Row="0" Grid.Column="1"/>
                            <StackPanel Grid.Row="0" Grid.Column="2" Orientation="Horizontal">
                                <Button x:Name="minimizeButton" Style="{StaticResource WindowButtonStyle}"
                                        Content="0" />
                                <Button x:Name="restoreButton" Style="{StaticResource WindowButtonStyle}"
                                        Content="1" />
                                <Button x:Name="closeButton"  BorderBrush="Beige" Style="{StaticResource WindowButtonStyle}"
                                        Content="r" />
                            </StackPanel>

                            <Grid Background="{TemplateBinding Background}"
                                       Grid.Row="1" Grid.ColumnSpan="2" Margin="5,5,5,5">
                                <AdornerDecorator>
                                    <ContentPresenter/>
                                </AdornerDecorator>
                            </Grid>
                        </Grid>
                        <!-- Resize grid  -->
                        <Grid x:Name="resizeGrid" ShowGridLines="True">
                            <Rectangle
        Stroke="{x:Null}"
        Fill="Transparent"
        VerticalAlignment="Top"
        Height="5"
        x:Name="top"
        Margin="5,0,5,0" />
                            <Rectangle
        Stroke="{x:Null}"
        Fill="Transparent"
        x:Name="bottom"
        Height="5"
        VerticalAlignment="Bottom"
        Margin="5,0,5,0" />
                            <Rectangle
        Stroke="{x:Null}"
        Fill="Transparent"
        HorizontalAlignment="Left"
        Margin="0,5,0,5"
        Width="5"
        x:Name="left"/>
                            <Rectangle
        Stroke="{x:Null}"
        Fill="Transparent"
        Margin="0,5,0,5"
        Width="5"
        HorizontalAlignment="Right"
        x:Name="right" />
                            <Rectangle
        Stroke="{x:Null}"
        Fill="Transparent"
        HorizontalAlignment="Left"
        VerticalAlignment="Bottom"
        Width="5"
        Height="5"
        x:Name="bottomLeft" />
                            <Rectangle
        Stroke="{x:Null}"
        Fill="Transparent"
        VerticalAlignment="Bottom"
        Height="5"
        Width="5"
        HorizontalAlignment="Right"
        x:Name="bottomRight" />
                            <Rectangle
        Stroke="{x:Null}"
        Fill="Transparent"
        HorizontalAlignment="Right"
        Width="5"
        Height="5"
        VerticalAlignment="Top"
        x:Name="topRight" />
                            <Rectangle
        Stroke="{x:Null}"
        Fill="Transparent"
        HorizontalAlignment="Left"
        Width="6"
        VerticalAlignment="Top"
        Height="5"
        x:Name="topLeft" />
                        </Grid>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Do I have to create a new image style like done with the buttons? Or is there a way to use the users icon definition?

1

1 Answers

0
votes

you can use converter on your Icon setter, that bring a path to the icon(set from App.config for example).