自定义控件未显示

时间:2013-05-20 18:26:13

标签: c# wpf xaml button custom-controls

我创建了一个简单的自定义控件来模仿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>

1 个答案:

答案 0 :(得分:4)

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

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

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

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