未显示自定义控件

本文关键字:自定义控件 显示 | 更新日期: 2023-09-27 18:14:34

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

未显示自定义控件

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

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

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

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