0

我创建了一个简单的自定义控件来模仿 Modern UI Tiles。项目构建没有任何问题,但自定义控件根本不显示在窗口上。这很简单,即使我已经看了很长时间,我也看不出问题出在哪里。

以下是文件:

Tile.cs

using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

public class Tile : ButtonBase
{
    public static readonly DependencyProperty ImageProperty =
        DependencyProperty.Register("Image", typeof (Image), typeof (Tile), new PropertyMetadata(default(Image)));

    public Image Image
    {
        get { return (Image) GetValue(ImageProperty); }
        set { SetValue(ImageProperty, value); }
    }

    static Tile()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(Tile), new FrameworkPropertyMetadata(typeof(Tile)));
    }
}

Themes\Generic.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">

    <SolidColorBrush x:Key="TileBackgroundBrush" Color="Black"/>
    <SolidColorBrush x:Key="TileBorderBrush" Color="White"/>
    <SolidColorBrush x:Key="TileForegroundBrush" Color="White"/>

    <Style x:Key="TileStyle" TargetType="{x:Type local:Tile}">
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="Background" Value="{DynamicResource TileBackgroundBrush}"/>
        <Setter Property="BorderBrush" Value="{DynamicResource TileBorderBrush}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:Tile}">
                    <Grid x:Name="Grid">
                        <Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"/>
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/>
                        <ContentPresenter Content="{TemplateBinding Image}" Margin="15,15,15,45" />
                    </Grid>
            </Setter.Value>
        </Setter>
        <Setter Property="BorderThickness" Value="3"/>
        <Setter Property="Foreground" Value="{DynamicResource TileForegroundBrush}"/>
        <Setter Property="Width" Value="150"/>
        <Setter Property="Height" Value="150"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Bottom"/>
        <Setter Property="Padding" Value="15"/>
    </Style>
</ResourceDictionary>

可能是什么问题呢?

编辑:Window是我正在使用它的:

<Window x:Class="ControlsDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:customControls="clr-namespace:MyCustomControls;assembly=MyCustomControls"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <customControls:Tile Content="Demo">
            <customControls:Tile.Image>
                <Image Source="Demo.png"></Image>
            </customControls:Tile.Image>
        </customControls:Tile>
    </Grid>
</Window>
4

1 回答 1

4

那么问题是 Expression Blend 自动定义了一个样式键:

<Style x:Key="TileStyle" TargetType="{x:Type local:Tile}">

我删除它后它现在可以正常工作了:

<Style TargetType="{x:Type local:Tile}">
于 2013-05-20T18:49:39.370 回答