我正在用一些自定义控件制作一个WPF-CustomControlLibrary-Project。其中之一是带有嵌套TextBlock的TextWrapping标签。当我设置DependencyProperty HorizontalContentAlignement体育时。向左,我希望Textblock的TextAlignment也设置为左侧。因此,我在本文中实现了一个转换器类:
Convert HorizontalAlignment to TextAlignment
然后我想在Generic.xaml中使用转换器类。因此,我创建了另一个名为ResourceDictionary的Resources.xaml,它位于库的根目录中。
<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-属性。
<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>但是,当我启动使用CustomControlLibrary的WPF应用程序时,我会收到一条错误消息,即在初始化库时抛出一个异常。看来,Source-属性存在问题。
我做错了什么?
提前感谢!
发布于 2016-05-09 09:42:48
我仍然建议使用适当的URI表示法,这样路径就不会那么容易被破坏。
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/WpfDesignerCustomControlLibrary;component/Resources.xaml"/>
</ResourceDictionary.MergedDictionaries>发布于 2016-05-09 08:45:00
好吧,再一次,我自己找到了答案。这似乎是我的一个新的-不幸的习惯。
我在Generic.xaml中引用Generic.xaml时犯了一个大错误。
这必须是:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/WpfDesignerCustomControlLibrary;component/Resources.xaml"/>
</ResourceDictionary.MergedDictionaries>https://stackoverflow.com/questions/37110919
复制相似问题