首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ResourceDictionary in Generic.xaml of CustomControlLibrary产生错误

ResourceDictionary in Generic.xaml of CustomControlLibrary产生错误
EN

Stack Overflow用户
提问于 2016-05-09 08:25:17
回答 2查看 576关注 0票数 0

我正在用一些自定义控件制作一个WPF-CustomControlLibrary-Project。其中之一是带有嵌套TextBlock的TextWrapping标签。当我设置DependencyProperty HorizontalContentAlignement体育时。向左,我希望Textblock的TextAlignment也设置为左侧。因此,我在本文中实现了一个转换器类:

Convert HorizontalAlignment to TextAlignment

然后我想在Generic.xaml中使用转换器类。因此,我创建了另一个名为ResourceDictionary的Resources.xaml,它位于库的根目录中。

代码语言:javascript
复制
<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-属性。

代码语言:javascript
复制
<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-属性存在问题。

我做错了什么?

提前感谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-09 09:42:48

我仍然建议使用适当的URI表示法,这样路径就不会那么容易被破坏。

代码语言:javascript
复制
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/WpfDesignerCustomControlLibrary;component/Resources.xaml"/>
</ResourceDictionary.MergedDictionaries>
票数 1
EN

Stack Overflow用户

发布于 2016-05-09 08:45:00

好吧,再一次,我自己找到了答案。这似乎是我的一个新的-不幸的习惯。

我在Generic.xaml中引用Generic.xaml时犯了一个大错误。

这必须是:

代码语言:javascript
复制
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/WpfDesignerCustomControlLibrary;component/Resources.xaml"/>
</ResourceDictionary.MergedDictionaries>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37110919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档