Álvaro García
Álvaro García

Reputation: 19356

how to have many wpf custom controls in the same library?

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.

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.

I have seen this post: Custom control Lib with multiple controls and generic.xaml but really I don't understand so much the solution.

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}">

That it is not possible to create a type from text local:CalendarioMes2.

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.

Thanks.

Upvotes: 2

Views: 1310

Answers (1)

Derrick Moeller
Derrick Moeller

Reputation: 4950

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)));
        }
    }
}

Upvotes: 2

Related Questions