使用App.xaml中的DataTemplate

时间:2014-03-10 19:21:12

标签: c# wpf datatemplate

在我的App.xaml中,我为我的DataType Entry定义了DataTemplate。在此DataTemplate我创建了一个包含多行和多列的Grid

DataTemplate的xaml看起来像是:

<DataTemplate x:Key="EntryTemplate" DataType="entity:Entry" x:Name="EntryTemplate">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.Resources>
            <Style TargetType="hCBase:HControlBase" x:Key="TopRowMargin">
                <Setter Property="Margin" Value="2,2,50,2"/>
            </Style>
        </Grid.Resources>
        <hC:HComboBox Grid.Row="0" Grid.Column="0" Header="Entry-Type:" Style="{StaticResource TopRowMargin}" SelectedIndex="0"
                      ItemsSource="{Binding Source={StaticResource EntryTypesDataProvider}}"
                      SelectedItem="{Binding EntryType, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
    </Grid>
</DataTemplate>
  

模板尚未完成。

现在我想在一个Window中使用这个DataTemplate,我在ViewModel中有一个Entry-Object。但目前我没有胶水如何使用它。

使用我的DataTemplate在View中从ViewModel显示Entry-Object需要做什么?

1 个答案:

答案 0 :(得分:2)

我个人创建ContentControl,例如在MainWindow中创建ContentTemplate,如下所示:

<ContentControl Name="MyContent"
                ContentTemplate="{StaticResource EntryTemplate}"> 

    <entity:Entry /> <!-- Your ViewModel here -->
</ContentControl>