更容易一个。
Resources.xaml包含:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DataTemplate DataType="TestInstanceViewModel" x:Name="TestInstanceViewModelTemplate">
<StackPanel Orientation="Vertical">
<Button Command="{Binding Path=StartCommand}" Content="Start"/>
<Button Command="{Binding Path=StopCommand}" Content="Stop"/>
<TextBlock Text="{Binding Path=Status}"/>
</StackPanel>
</DataTemplate>
窗口包含:
<Window x:Class="TestClientMainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Testing client" Height="350" Width="525"
DataContext="{StaticResource ResourceKey=TheViewModel}" Background="#FFD4BFBF">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<StackPanel HorizontalAlignment="Stretch" Name="stackPanel1" VerticalAlignment="Stretch">
<ToolBar Height="26" Name="toolBar1">
<ItemsControl>
<Button Command="{Binding Path=CreateNewTestCommand}">Add new Test</Button>
</ItemsControl>
</ToolBar>
<ListBox ItemsSource="{Binding Path=TestInstances}" ItemTemplate="{StaticResource TestInstanceViewModelTemplate}" Name="listBox1" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" MinHeight="{Binding ElementName=stackPanel1, Path=Height}" Height="274" />
</StackPanel>
</Grid>
然后我会尝试一个列表框:
ItemTemplate="{StaticResource TestInstanceViewModelTemplate}"
这不起作用。访问我已添加到合并词典的资源背后的逻辑是什么?
由于
编辑:
答案 0 :(得分:3)
尝试<DataTemplate DataType="TestInstanceViewModel" x:Key="TestInstanceViewModelTemplate">