我有一个WPF dataGrid,它通过DataBinding填充。此列表包含不同的列。我有两种类型的行,一种类型包含行中的所有列,另一种类型应跨越所有列的一列。
有没有一种简单的方法可以实现这一目标? (也许使用ListView而不是DataGrid?)
我附上截图应该是这样的:
我现在尝试使用项目模板选择器:
我在资源中的模板(这两个模板不正确,但它们仅用于测试!)
<DataTemplate x:Key="commentTemplate">
<TextBlock Text="{Binding}"/>
</DataTemplate>
<DataTemplate x:Key="normalTemplate">
<Image Source="{Binding }" />
</DataTemplate>
<WPFVarTab:VarTabRowItemTemplateSelector
NormalRowsTemplate="{StaticResource normalTemplate}"
CommentRowsTemplate="{StaticResource commentTemplate}"
x:Key="vartabrowItemTemplateSelector" />
和我的Datagrid:
<DataGrid AutoGenerateColumns="False" Margin="0,22,0,22"
Name="dataGrid" Grid.RowSpan="2" CanUserAddRows="True"
RowBackground="Azure" AlternatingRowBackground="LightSteelBlue"
ItemTemplateSelector="{StaticResource vartabrowItemTemplateSelector}" >
和我的模板选择器:
public class VarTabRowItemTemplateSelector : DataTemplateSelector
{
public DataTemplate NormalRowsTemplate { get; set; }
public DataTemplate CommentRowsTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
S7VATRow vRow = item as S7VATRow;
if (vRow == null || string.IsNullOrEmpty(vRow.Comment))
return NormalRowsTemplate;
return CommentRowsTemplate;
}
}
我在SelectTemplate的第一行停了一下,但从未调用过它!
答案 0 :(得分:0)
您可以将item template selector与ListView
一起使用。或者使用DataGrid
,它也可以在那里使用。 Here就是一个例子。
答案 1 :(得分:0)
使用绑定来打开和关闭数据流的可见性。我假设你定义了列。
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<ContentControl Style="{StaticResource CommentTemplate}" Content="{Binding Comment}" Visibility="{Binding IsCommentVisible, Converter={StaticResource BooleanToVisibilityConverter}}"/>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
检查一下 http://www.wpf-tutorial.com/datagrid-control/details-row/。如果链接断开,下面会粘贴您参考的行和列详细信息的代码。
<Window x:Class="WpfTutorialSamples.DataGrid_control.DataGridDetailsSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DataGridDetailsSample" Height="200" Width="400">
<Grid Margin="10">
<DataGrid Name="dgUsers" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTextColumn Header="Birthday" Binding="{Binding Birthday}" />
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<TextBlock Text="{Binding Details}" Margin="10" />
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</Grid>
</Window>
代码视图模型
using System;
using System.Collections.Generic;
using System.Windows;
namespace WpfTutorialSamples.DataGrid_control
{
public partial class DataGridDetailsSample : Window
{
public DataGridDetailsSample()
{
InitializeComponent();
List<User> users = new List<User>();
users.Add(new User() { Id = 1, Name = "John Doe", Birthday = new DateTime(1971, 7, 23) });
users.Add(new User() { Id = 2, Name = "Jane Doe", Birthday = new DateTime(1974, 1, 17) });
users.Add(new User() { Id = 3, Name = "Sammy Doe", Birthday = new DateTime(1991, 9, 2) });
dgUsers.ItemsSource = users;
}
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime Birthday { get; set; }
public string Details
{
get
{
return String.Format("{0} was born on {1} and this is a long description of the person.", this.Name, this.Birthday.ToLongDateString());
}
}
}
}