在WPF自定义控件中键入转换器

时间:2016-01-25 20:56:27

标签: wpf xaml

我正在WPF中创建一个自定义控件,并希望将我的转换器放在一个单独的资源字典中,以使事情更清晰。我有一个ControlStyling.xaml资源字典用于控件的样式

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

<Style TargetType="{x:Type local:MyDialIndicator}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:MyDialIndicator}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">

                    <Ellipse Width="{TemplateBinding BackgroundSizeRadius}"                                           

                    </Ellipse>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我希望保留我的转换器的Converters.xaml资源字典

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

<local:RadiusDiameterConverter x:Key="RadiusConvert"/>

Themes / Generic.xaml,我指向这两个词典。

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

<ResourceDictionary.MergedDictionaries>       
    <ResourceDictionary Source="/DialIndicatorControl;component/Themes/Generic/Converters.xaml"/>
    <ResourceDictionary Source="/DialIndicatorControl;component/Themes/Generic/ControlStyling.xaml"/>
</ResourceDictionary.MergedDictionaries>

我遇到的问题是当我将转换器放在一个单独的资源字典中时,我的ControlStyling.xaml字典没有对转换器的引用(现在我认为它很明显)。我认为我可以使用这些转换器,因为我合并了Themes/Generic.xaml字典中的两个字典,但是没有用。有没有一种方法可以将我的所有转换器放在单独的字典中,并且仍然可以在我的ControlStyling.xaml中为这个自定义控件引用它们?

1 个答案:

答案 0 :(得分:0)

从样式字典中引用转换器资源字典:

<ResourceDictionary.MergedDictionaries>       
    <ResourceDictionary Source="/DialIndicatorControl;component/Themes/Generic/Converters.xaml"/>
</ResourceDictionary.MergedDictionaries>