ResourceDictionary中的控件内容

时间:2013-12-10 16:57:08

标签: c# wpf xaml

我想在ResourceDictionary中设置一个自定义WPF控件。

ResourceDictinary(InformationContainer.xaml):

<ResourceDictionary 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:dxdb="http://schemas.devexpress.com/winfx/2008/xaml/demobase"
             xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
             xmlns:l="clr-namespace:LISA"
             xmlns:vm="clr-namespace:LISA.ViewModels"
             xmlns:local="clr-namespace:LISA.ContentPages.Notification.SubModels"
             mc:Ignorable="d">

    <DataTemplate x:Key="notificationTemplate">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <local:BasicInformation Grid.Row="0" Width="Auto" Height="Auto" HorizontalAlignment="Left" VerticalAlignment="Top" />
            <local:PatientInformation Grid.Row="1"  Width="Auto" Height="Auto" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,10,0,0"  />
            <Button Grid.Row="2" Content="{x:Static l:Resources.Ok}" Width="Auto" Height="Auto" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,10,0,0" />
        </Grid>
    </DataTemplate>

</ResourceDictionary>

My MainWindow.xaml:

<UserControl
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" 
             xmlns:cp="clr-namespace:LISA.ContentPages"
             xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
             xmlns:l="clr-namespace:LISA"
             xmlns:vm="clr-namespace:LISA.Models"
             x:Class="LISA.ContentPages.Notification.NotificationControl" 
             mc:Ignorable="d" FontSize="{DynamicResource fontNormal}">

    <UserControl.DataContext>
        <vm:NotificationControlModel x:Name="Model" />
    </UserControl.DataContext>
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/LISA;component/ContentPages/Notification/SubModels/InformationContainer.xaml" />
                <ResourceDictionary Source="/LISA;component/ContentPages/Notification/SubModels/InformationHeader.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <vm:NotificationControlModel x:Key="viewModel" />
        </ResourceDictionary>
    </UserControl.Resources>

    <dxg:GridControl Name="notificationGrid" ItemsSource="{Binding Path=InformationContainers}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ShowLoadingPanel="True" Loaded="view_Loaded">
        <dxg:GridControl.View>
            <dxg:CardView ShowGroupPanel="False" ShowColumnHeaders="False" Name="view" NavigationStyle="Row" CardTemplate="{DynamicResource notificationTemplate}" CardHeaderTemplate="{DynamicResource notificationTemplateHeader}" Width="Auto" Height="Auto" AllowEditing="True" CardLayout="Rows"/>
        </dxg:GridControl.View>
    </dxg:GridControl>

</UserControl>

现在,我想创建一个“InformationContainer.xaml”的实例,并设置自定义UI元素(模型中的数据)。但是,如果我创建一个新的实例,如:InformationContrainer ic = new InformationContrainer();我无法访问自定义UI元素“

1 个答案:

答案 0 :(得分:0)

确保您实际上是将字典添加到其资源下的app.xaml

    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="YourDictionaryName.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </RosourceDictionary>