【问题标题】:Custom window control Icon自定义窗口控件图标
【发布时间】:2017-03-15 22:55:28
【问题描述】:

我已经制作了一个自定义窗口控件,并正在从 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>

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

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      • 2014-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多