我正在用一些customcontrols製作一個WPF-CustomControlLibrary-Project。 其中之一是帶有用於TextWrapping的嵌套TextBlock的標籤。 當我設置DependencyProperty HorizontalContentAlignement p.e.到左邊,我希望Textblock的TextAlignment也設置爲Left。 所以我implememented一個轉換器類喜歡這裏的文章:CustomControlLibrary的Generic.xaml中的ResourceDictionary產生錯誤
Convert HorizontalAlignment to TextAlignment
然後我想用轉換器級的Generic.xaml。 所以我創建了另一個名爲Resources.xaml的ResourceDictionary,它位於我的庫的根目錄中。
<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>
然後,我添加了對Generic.xaml中字典的引用,並綁定了TextBlock的TextAlignment-property。
<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>
但是,當我開始WPF應用程序,它使用CustomControlLibrary我得到一個錯誤信息,雖然庫的初始化時拋出異常。似乎Source-attribute有問題。
我做錯了什麼?
在此先感謝!