Patrick Pirzer
Patrick Pirzer

Reputation: 1759

ResourceDictionary in Generic.xaml of CustomControlLibrary produces errors

I'm making a WPF-CustomControlLibrary-Project with some customcontrols. One of these is a Label with a nested TextBlock for TextWrapping. When i set the DependencyProperty HorizontalContentAlignement p.e. to Left, i want the TextAlignment of the Textblock set to Left too. So i implememented a converter class like in the article here:

Convert HorizontalAlignment to TextAlignment

Then i wanted to use the converter-class in the Generic.xaml. So i created another ResourceDictionary called Resources.xaml, which lies in the rootdirectory of my library.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WpfDesignerCustomControlLibrary">
    <local:HorizontalToTextAlignmentConverter x:Key="h2tAlignmentConverter"/>
</ResourceDictionary>

Then i added a reference to the dictionary in the Generic.xaml and binded the TextAlignment-property of the TextBlock.

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfDesignerCustomControlLibrary">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Resources.xaml"/>
    </ResourceDictionary.MergedDictionaries>

    <!--Style for the CustomControl CustomTextBox-->
    <Style TargetType="{x:Type local:CustomTextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    </Style>

    <!--Style for the CustomControl CustomLabel-->
    <Style TargetType="{x:Type local:CustomLabel}" BasedOn="{StaticResource {x:Type Label}}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomLabel}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <Label HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                               VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}">
                            <TextBlock Text="{TemplateBinding Text}"
                                       TextWrapping="Wrap"
                                       TextAlignment="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Label}},
                                Path=HorizontalContentAlignment, Converter={StaticResource h2tAlignmentConverter}}"
                                       TextDecorations="{TemplateBinding TextDecorations}"/>
                        </Label>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

But when i start the WPF-application, which uses the CustomControlLibrary i get an error message, that while the initialization of the library an exception was thrown. It seems that there is a problem with the Source-attribute.

What have i done wrong?

Thanks in advance!

Upvotes: 0

Views: 832

Answers (2)

Jai
Jai

Reputation: 8363

I would still recommend to use proper URI notation so that paths do not get broken so easily.

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/WpfDesignerCustomControlLibrary;component/Resources.xaml"/>
</ResourceDictionary.MergedDictionaries>

Upvotes: 1

Patrick Pirzer
Patrick Pirzer

Reputation: 1759

Okay, once again, i found the answer myself. This seems to get a new - unfortunate - custom of mine.

I have made a big mistake when referencing to the ResourceDictionary in the Generic.xaml.

This has to be:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/WpfDesignerCustomControlLibrary;component/Resources.xaml"/>
</ResourceDictionary.MergedDictionaries>

Upvotes: 0

Related Questions