WPF dataGrid(或ListView)通过绑定填充,不同的行模板
我有一个通过数据绑定填充的WPF数据网格。此列表包含不同的列。我有两种类型的行,一种类型包含行中的所有列,另一种应该跨越所有列中的一列 有没有一个简单的方法可以做到这一点?(可能使用ListView而不是DataGrid?) 我附上了一个屏幕截图,它应该是什么样子的: 我现在尝试使用项目模板选择器:WPF dataGrid(或ListView)通过绑定填充,不同的行模板,wpf,templates,data-binding,datagrid,Wpf,Templates,Data Binding,Datagrid,我有一个通过数据绑定填充的WPF数据网格。此列表包含不同的列。我有两种类型的行,一种类型包含行中的所有列,另一种应该跨越所有列中的一列 有没有一个简单的方法可以做到这一点?(可能使用ListView而不是DataGrid?) 我附上了一个屏幕截图,它应该是什么样子的: 我现在尝试使用项目模板选择器: public class VarTabRowItemTemplateSelector : DataTemplateSelector { public DataTemplate Norma
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的第一行中放置了一个停止符,但从未调用过 您可以与
列表视图一起使用。或者使用DataGrid
,它也可以在那里使用。就是一个例子。使用绑定可以像这样打开和关闭数据行的可见性。我假设您定义了列
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<ContentControl Style="{StaticResource CommentTemplate}" Content="{Binding Comment}" Visibility="{Binding IsCommentVisible, Converter={StaticResource BooleanToVisibilityConverter}}"/>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
检查这个
. 如果链接断开,下面将粘贴行和列详细信息代码以供参考
<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());
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Windows;
命名空间WpfTutorialSamples.DataGrid\u控件
{
公共部分类DataGridDetails示例:窗口
{
公共DataGridDetailsSample()
{
初始化组件();
列表用户=新列表();
Add(newuser(){Id=1,Name=“johndoe”,生日=newdatetime(1971,7,23)});
Add(newuser(){Id=2,Name=“Jane Doe”,生日=new DateTime(1974,1,17)});
Add(newuser(){Id=3,Name=“sammydoe”,生日=newdatetime(1991,9,2)});
dgUsers.ItemsSource=用户;
}
}
公共类用户
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共日期时间生日{get;set;}
公共字符串详细信息
{
得到
{
return String.Format(“{0}是在{1}上出生的,这是对此人的详细描述。”、this.Name、this.birth.ToLongDateString());
}
}
}
}
但是Item template selector不是只针对一个单元格,我希望合并单元格吗?@Jochen:AFAIK模板选择器是针对整行的。我已经尝试过了,但是ItemTemplateSelector从未被调用过@你确定你用对了吗?也许你可以发布实际代码?好的,代码在那里。。。。当这可以工作时,为了使用DataGrid.Columns中定义的列,我必须在ItemsTemplate中放入什么?
<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());
}
}
}
}