我有这个ResourceDictionary
<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
<DataTemplate DataType="{x:Type vm:EditRecordViewModel}">
<StackPanel>
<TextBlock Text="Test" />
</StackPanel>
</DataTemplate>问题
在我的第一个ResourceDictionary中,如何在第一个ResourceDictionary中显示第二个ResourceDictionary,在哪里显示<DataTemplate />?
我像这样在App.xaml中添加了资源,但是如何实际使用它们呢?:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Views/MainViewModel.xaml" />
<ResourceDictionary Source="Views/EditRecordViewModel.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>发布于 2021-06-24 07:06:14
您可以为数据模板提供一个键。
<DataTemplate x:Key="test" DataType="{x:Type local:EditRecordViewModel}">...然后引用它
<ListView ItemTemplate="{StaticResource test}">...

发布于 2021-06-24 09:22:37
您已经声明了没有键的数据模板,这意味着默认情况下它们将应用于相应的类型。
你不需要任何链接。
唯一重要的是,ListView获得了类型为EditRecordViewModel的项的集合。
示例.
TwoDataTemplte/ViewModels.cs:
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:
<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:
<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:
<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:
<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>https://stackoverflow.com/questions/68102284
复制相似问题