I have this 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>
In the same folder I have another ResourceDictionary
:
<DataTemplate DataType="{x:Type vm:EditRecordViewModel}">
<StackPanel>
<TextBlock Text="Test" />
</StackPanel>
</DataTemplate>
Question
In my first ResourceDictionary
how do I get the 2nd ResourceDictionary
to display in the first, where I have the <DataTemplate />
displayed?
I have added resources in the App.xaml like so, but how do actually use them?:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Views/MainViewModel.xaml" />
<ResourceDictionary Source="Views/EditRecordViewModel.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
You have declared Data Templates without a key, which means that they will be applied to the corresponding types by default.
You don't need any links for this.
The only thing that matters is that the ListView gets a collection of items of type EditRecordViewModel.
Example.
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.