参考转换器在另一个xaml中

时间:2015-11-26 14:30:17

标签: wpf xaml windows-10-mobile staticresource windows-10-universal

我在Resource.xaml中获得了一个数据模板:

    <DataTemplate x:Key="SplitViewMenuItemWithCount">
    <RelativePanel>
        <RelativePanel RelativePanel.AlignVerticalCenterWithPanel="True" Margin="0,10,0,10">
            <SymbolIcon Foreground="White" Width="25" RelativePanel.AlignVerticalCenterWithPanel="True" Symbol="{Binding Symbol}" x:Name="SymbolIcon" Margin="10,0,0,0"/>
            <StackPanel Visibility="{Binding Count, Converter={StaticResource BooleanToVisibilityConverter}, TargetNullValue=Collapsed, FallbackValue=Collapsed}" x:Name="StackPanelCount" RelativePanel.RightOf="SymbolIcon" Margin="0,20,0,0" CornerRadius="9" Background="White" Width="15" Height="15">
                <TextBlock Text="{Binding Count, FallbackValue='0'}" TextAlignment="Center" FontSize="10" Foreground="{StaticResource AppDarkBlueColor}"/>
            </StackPanel>
            <TextBlock Margin="10,0,0,0" Foreground="White" x:Name="TextBlockMenuItemText" RelativePanel.RightOf="StackPanelCount" Text="{Binding Text}" FontSize="16" Padding="5,3,0,5"/>
        </RelativePanel>
    </RelativePanel>
</DataTemplate>

我在MainPage.xaml中使用datatemplate作为ListView的数据窗口,但是因为BooleanToVisibilityConverter而崩溃了。它无法在Resource.xaml中找到转换器。

但是,如果我将数据模板放在我的MainPage.Resources中,它会找到转换器(因为它已在那里定义)。

有没有办法让datatemplate保持在Resource.xaml?我宁愿拥有它而不是MainPage.Resources

1 个答案:

答案 0 :(得分:2)

您必须使用 ResourceDictionnary 标记将mainPage.xaml中的引用添加到您的resources.xaml,如下所示:

<Window x:Class="MainPage"  ...  ... >
     <Window.Resources>
         <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/YourProjectDLL;component/Resource.xaml" />
            </ResourceDictionary.MergedDictionaries>
         </ResourceDictionary>
     </Window.Resources>
     <Grid>
          ... Your window body
     </Grid>
</Window>