为什么要在 ListView 中使用 DataTemplate?
Why would you use DataTemplate in a ListView?
有人可以向我解释一下如果我使用有什么区别
DataTemplate
在 ListView
里面 XAML?
我已经使用 ListView
来显示我的 ObservableCollection
的内容,而没有使用 DataTemplate
并且使用 DataTemplate
看起来完全一样?
那我为什么要使用数据模板?我想看一个简单的 explanation/example.
请在这里查看#HighCore 的回答WPF MVVM Why use ContentControl + DataTemplate Views rather than straight XAML Window Views?。
<Window x:Class="MyViews.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml
xmlns:viewmodel="clr-namespace:Project.ViewModel"
DataContext="{Binding Main, Source={StaticResource Locator}}"
Title="{Binding Title, Mode=TwoWay}" Height="{Binding Height, Mode=TwoWay}" Width="{Binding Width, Mode=TwoWay}" Left="{Binding Left, Mode=TwoWay}" Top="{Binding Top, Mode=TwoWay}" WindowStartupLocation="CenterScreen">
<Window.Resources>
<HierarchicalDataTemplate DataType="{x:Type viewmodel:OtherViewModel1}">
<ContentPresenter Content="{Binding Path=Text}" />
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type viewmodel:OtherViewModel2}">
<view:ConnectivityLED />
</DataTemplate>
</Window.Resources>
因此您可以看到 Mainwindow 正在与不止 1 个视图模型对话,Main 是它自己的视图模型,它是它的数据上下文,它还引用了 otherviewmodel1 和 otherviewmodel2。希望这对您有所帮助,干杯!
DataTemplate 用于以超出简单文本块的方式显示数据。当然这样做:
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListView.ItemTemplate>
不买你很多。但它允许你做这样的事情:
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Source="{Binding ImagePath}"/>
<TextBlock Text="{Caption}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
太酷了!您可以将 任何内容 放入模板中,因此它使 ItemsControl
(及其派生词)成为 WPF 中最强大的 类。
有人可以向我解释一下如果我使用有什么区别
DataTemplate
在 ListView
里面 XAML?
我已经使用 ListView
来显示我的 ObservableCollection
的内容,而没有使用 DataTemplate
并且使用 DataTemplate
看起来完全一样?
那我为什么要使用数据模板?我想看一个简单的 explanation/example.
请在这里查看#HighCore 的回答WPF MVVM Why use ContentControl + DataTemplate Views rather than straight XAML Window Views?。
<Window x:Class="MyViews.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml
xmlns:viewmodel="clr-namespace:Project.ViewModel"
DataContext="{Binding Main, Source={StaticResource Locator}}"
Title="{Binding Title, Mode=TwoWay}" Height="{Binding Height, Mode=TwoWay}" Width="{Binding Width, Mode=TwoWay}" Left="{Binding Left, Mode=TwoWay}" Top="{Binding Top, Mode=TwoWay}" WindowStartupLocation="CenterScreen">
<Window.Resources>
<HierarchicalDataTemplate DataType="{x:Type viewmodel:OtherViewModel1}">
<ContentPresenter Content="{Binding Path=Text}" />
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type viewmodel:OtherViewModel2}">
<view:ConnectivityLED />
</DataTemplate>
</Window.Resources>
因此您可以看到 Mainwindow 正在与不止 1 个视图模型对话,Main 是它自己的视图模型,它是它的数据上下文,它还引用了 otherviewmodel1 和 otherviewmodel2。希望这对您有所帮助,干杯!
DataTemplate 用于以超出简单文本块的方式显示数据。当然这样做:
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListView.ItemTemplate>
不买你很多。但它允许你做这样的事情:
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Source="{Binding ImagePath}"/>
<TextBlock Text="{Caption}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
太酷了!您可以将 任何内容 放入模板中,因此它使 ItemsControl
(及其派生词)成为 WPF 中最强大的 类。