为什么我看不到WPF自定义控件的控件?

时间:2016-09-22 16:32:42

标签: wpf wpf-controls

我已按照此问题的解决方案:how to have many wpf custom controls in the same library?

现在的问题是,当我在主视图中添加自定义控件时,我可以看到自定义控件的控件。

但是,如果我在generic.xaml文件中使用了样式而不是MyControl.xaml文件,并且只有用户控件,那么它就可以工作。

MyControl.xaml的代码是:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:CustomControls.Calendario2">


    <Style TargetType="{x:Type local:CalendarioMes2}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CalendarioMes2}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">

                        <Grid>
                            <TextBox Text="Prueba" BorderBrush="Black"/>
                            <Label Content="Prueba" BorderBrush="black"/>
                        </Grid>

                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

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:CustomControls.Calendario2">


        <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/CustomControls.Calendario2;component/ResourceDictionaries/CalendarioMes2.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

为什么我看不到textBox和标签?

感谢。

编辑:我使用了文件的绝对路径:

<ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="F:\Alvaro\ejemplos\Calendario\CustomControls\Themes\CalendarioMes2.xaml"/>
    </ResourceDictionary.MergedDictionaries>

但是知道我在主窗口中有一个错误,说我可以从文本中创建一个类型。错误发生在CalendarioMes2.xaml:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:CustomControls.Calendario2">


    <Style TargetType="{x:Type local:CalendarioMes2}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CalendarioMes2}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">

                        <Grid>
                            <TextBox Text="Prueba" BorderBrush="Black"/>
                            <Label Content="Prueba" BorderBrush="black"/>
                        </Grid>

                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

在第7行:

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

但是将我的代码与另一个帖子中的解决方案的代码进行比较,我看到我有相同的行。

这是CalendarioMes2.cs

namespace CustomControls.Calendario2
{
    public class CalendarioMes2 : Control
    {
        static CalendarioMes2()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CalendarioMes2), new FrameworkPropertyMetadata(typeof(CalendarioMes2)));
        }
    }
}

编辑:源代码在这里:https://1drv.ms/u/s!AiTepLSbavZ0gRDvBPJgrxEIWS1U

0 个答案:

没有答案