首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在DataTemplate中引用DataTemplate?

如何在DataTemplate中引用DataTemplate?
EN

Stack Overflow用户
提问于 2021-06-23 14:57:05
回答 2查看 346关注 0票数 0

我有这个ResourceDictionary

代码语言:javascript
复制
<DataTemplate DataType="{x:Type vm:MainViewModel}">
    <ListView>
        <ListView.ItemTemplate>
            <DataTemplate />    <----- Here I'd like to load an external DataTemplate from the same folder
        </ListView.ItemTemplate>
    </ListView>
</DataTemplate>

在同一个文件夹中,我有另一个ResourceDictionary

代码语言:javascript
复制
<DataTemplate DataType="{x:Type vm:EditRecordViewModel}">
    <StackPanel>
       <TextBlock Text="Test" />
    </StackPanel>
</DataTemplate>

问题

在我的第一个ResourceDictionary中,如何在第一个ResourceDictionary中显示第二个ResourceDictionary,在哪里显示<DataTemplate />

我像这样在App.xaml中添加了资源,但是如何实际使用它们呢?:

代码语言:javascript
复制
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Views/MainViewModel.xaml" />
            <ResourceDictionary Source="Views/EditRecordViewModel.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-06-24 07:06:14

您可以为数据模板提供一个键。

代码语言:javascript
复制
<DataTemplate x:Key="test" DataType="{x:Type local:EditRecordViewModel}">...

然后引用它

代码语言:javascript
复制
<ListView ItemTemplate="{StaticResource test}">...

票数 1
EN

Stack Overflow用户

发布于 2021-06-24 09:22:37

您已经声明了没有键的数据模板,这意味着默认情况下它们将应用于相应的类型。

你不需要任何链接。

唯一重要的是,ListView获得了类型为EditRecordViewModel的项的集合。

示例.

TwoDataTemplte/ViewModels.cs:

代码语言:javascript
复制
namespace TwoDataTemplte.ViewModel
{
    public class EditRecordViewModel
    {
        public string Text { set; get; }
    }
    public class MainViewModel
    {

        public EditRecordViewModel[] EditRecords { get; } =
        {
            new EditRecordViewModel() {Text = "First"},
            new EditRecordViewModel() {Text = "Second"}
        };
    }
}

TwoDataTemplte\MainDictionary.xaml:

代码语言:javascript
复制
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:vm="clr-namespace:TwoDataTemplte.ViewModel">
    <DataTemplate DataType="{x:Type vm:MainViewModel}">
        <ListView ItemsSource="{Binding EditRecords}"/>
    </DataTemplate>
</ResourceDictionary>

TwoDataTemplte\ItemDictionary.xaml:

代码语言:javascript
复制
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:vm="clr-namespace:TwoDataTemplte.ViewModel">
    <DataTemplate DataType="{x:Type vm:EditRecordViewModel}">
        <StackPanel>
            <TextBlock Text="{Binding Text}" />
        </StackPanel>
    </DataTemplate>
</ResourceDictionary>

App.xaml:

代码语言:javascript
复制
<Application x:Class="Febr20y.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Febr20y"
             StartupUri="TwoDataTemplte/ExampleWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="TwoDataTemplte/MainDictionary.xaml" />
                <ResourceDictionary Source="TwoDataTemplte/ItemDictionary.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

TwoDataTemplte\ExampleWindow.xaml:

代码语言:javascript
复制
<Window x:Class="TwoDataTemplte.ExampleWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TwoDataTemplte"
        xmlns:vm="clr-namespace:TwoDataTemplte.ViewModel"
        mc:Ignorable="d"
        Title="ExampleWindow" Height="450" Width="800">
    <Grid>
        <ContentControl>
            <vm:MainViewModel/>
        </ContentControl>
    </Grid>
</Window>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68102284

复制
相关文章

相似问题

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