自定义窗口控件图标

时间:2017-03-15 22:55:28

标签: c# wpf xaml

我已经制作了一个自定义窗口控件,并且正在添加WPF原始控件的功能;最小化,最大化,关闭和图标放置。

但是我很遗憾如何允许用户使用Icon属性在项目中设置图标,并在我的控件中将其用作图标。

控件基类是Window,可以设置Icon属性。

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>

我是否必须像按钮一样创建新的图像样式? 或者有没有办法使用用户图标定义?

1 个答案:

答案 0 :(得分:0)

你可以在你的Icon setter上使用转换器,它带来一个图标路径(例如从App.config设置)。