不知道我遵循完全,但想必也有可显示字段的固定数量。因此,你可以通过你的视图模型公开这些字段:
public class YourViewModel : ViewModel
{
public string Text
{
//get and set omitted
}
public ImageSource Image1
{
//get and set omitted
}
public ImageSource Image2
{
//get and set omitted
}
}
你有每个模板可以根据不同的密钥存储:
<DataTemplate x:Key="FirstTemplate" DataType="{x:Type local:YourViewModel}">
...
</DataTemplate>
<DataTemplate x:Key="SecondTemplate" DataType="{x:Type local:YourViewModel}">
...
</DataTemplate>
然后在每个模板可以直接绑定到字段您浏览模式:
<TextBlock Content="{Binding Text}"/>
<Image Grid.Row="1" Source="{Binding Image1}"/>
<Image Grid.Row="1" Grid.Column="1" Source="{Binding Image2}"/>
与模板之间进行切换,你可以替代的资源在你的资源层次结构的适当水平:
this.Resources.Clear();
this.Resources.Add(FindResource(templateKey));