如何在同一个库中拥有多个 wpf 自定义控件? [英] how to have many wpf custom controls in the same library?
问题描述
我有一个 WPF 自定义控件项目,我想在其中包含许多自定义控件.默认情况下,VS2015 cummunity 创建一个 Theme 文件夹,其中包含一个 generic.xaml 文件和一个包含交互逻辑的 .cs 文件.
I have a WPF custom control project in which I want to have many custom controls. By default, VS2015 cummunity creates a Theme folder with a generic.xaml file and a .cs file with the interaction logic.
我想要很多用户控件,所以我尝试创建一个 MyControl1 文件夹,在这个文件夹中,我创建了一个 Theme 文件夹并添加了一个新项目,一个 WPF 自定义控件.但它不会为此控件创建 generic.xaml.我从根文件夹复制了默认的 generic.xaml 并创建了我的样式,但是当我在我的 WPF 应用程序中使用该控件时,我没有看到该控件.
I want to have many user controls, so I have tried to create a MyControl1 folder, inside this folder, I have created a Theme folder and I add a new item, a WPF custom control. But it doesn't create a generic.xaml for this control. I copy from the root folder the default generic.xaml and I create my style, but when I use the control in my WPF application, I don't see the control.
我看过这篇文章:自定义控件库与多个control 和 generic.xaml 但我真的不太了解解决方案.
I have seen this post: Custom control Lib with multiple controls and generic.xaml but really I don't understand so much the solution.
我有一个问题,在 MyControl1.Generic.xaml 中有这个代码:
I have a problem, in MyControl1.Generic.xaml y have this code:
<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"/>
<Label Content="Prueba"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
但我在这一行中遇到错误:
But I get an error in this line:
<Style TargetType="{x:Type local:CalendarioMes2}">
无法从文本 local:CalendarioMes2 创建类型.
That it is not possible to create a type from text local:CalendarioMes2.
如果我在只有一个自定义控件的库中使用此代码并且此代码位于 generic.xaml 文件中,则它可以工作.
If I use this code in a library with only one custom control and this code is in the generic.xaml file, it works.
所以,总而言之,我想知道如何拥有一个包含许多自定义控件的库.
So, in sumary, I would like to know how could I have a library with many custom controls.
谢谢.
推荐答案
我发现最简洁的方法是创建合并字典.CustomControl 倾向于从基本控件继承,因此我将按 ListView、TextBox 等对它们进行分组.
I have found that the cleanest way involves creating a merged dictionary. CustomControl's tend to inherit from a base control, so I will group them by ListView, TextBox, etc.
Generic.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/YourAssembly;component/ResourceDictionaries/ListView.xaml"/>
<ResourceDictionary Source="/YourAssembly;component/ResourceDictionaries/TabControl.xaml"/>
<ResourceDictionary Source="/YourAssembly;component/ResourceDictionaries/TextBox.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
TextBox.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Local="clr-namespace:YourNameSpace">
<!-- Describes how to style a ValidatedTextBox -->
<Style x:Key="{x:Type Local:ValidatedTextBox}" BasedOn="{StaticResource {x:Type TextBox}}" TargetType="{x:Type Local:ValidatedTextBox}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<StackPanel Orientation="Horizontal">
<Border BorderBrush="Red" BorderThickness="1" VerticalAlignment="Top">
<AdornedElementPlaceholder x:Name="adorner" />
</Border>
<Border x:Name="validationErrorsContainer" Background="LightCoral" BorderBrush="Red" BorderThickness="1" Margin="5, 0, 0, 0" VerticalAlignment="Top">
<ListView Background="Transparent" BorderThickness="0" Focusable="False" IsHitTestVisible="False"
ItemsSource="{Binding AdornedElement.(Validation.Errors), ElementName=adorner}">
<ListView.Resources>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Visibility" Value="Collapsed" />
</Style>
</ListView.Resources>
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ErrorContent}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Border>
</StackPanel>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding AdornedElement.IsFocused, ElementName=adorner}" Value="False">
<Setter TargetName="validationErrorsContainer" Property="Visibility" Value="Collapsed" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
ValidatedTextBox.cs
namespace YourNameSpace
{
public class ValidatedTextBox : TextBox
{
static ValidatedTextBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ValidatedTextBox), new FrameworkPropertyMetadata(typeof(ValidatedTextBox)));
}
}
}
这篇关于如何在同一个库中拥有多个 wpf 自定义控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!