Silverlight Datagrid:是否可以水平合并行单元格?

时间:2013-11-27 14:42:50

标签: c# .net silverlight datagrid

我需要数据网格中的功能,例如跟随Record3

Col1 | Col2 | Col3 | COL4


Rec1 | Rec1 | Rec1 | REC1
Rec2 | Rec2 | Rec2 | REC2
Record3信息水平显示
Rec4 | Rec4 | Rec4 | REC4
Rec5 | Rec5 | Rec5 | Rec5

有可能吗?有什么建议吗?

Cell Merging

1 个答案:

答案 0 :(得分:0)

你有一些示例视图吗?任何图片或草图?

也许RowDetailsTemplate可以解决您的问题? MSDN - DataGrid.RowDetailsTemplate Property

...或检查解决方案(我不知道这是实现这一点的方法,尤其是我不知道如何解决你的问题,而不是混合UI层和数据层。 我认为,您自己的自定义控件可以实现以下代码。):

我的观点:

非常简单的网格,并为DataGridRows定义了ControlTemplate(但未在xaml中分配给网格)。

<Grid x:Name="LayoutRoot">
        <Grid.Resources>
            <ControlTemplate x:Key="DataGridRowTemplate">
                <TextBlock Text="{Binding FullData}" HorizontalAlignment="Stretch"/>
            </ControlTemplate>
        </Grid.Resources>

        <ScrollViewer x:Name="PageScrollViewer" Style="{StaticResource PageScrollViewerStyle}">
            <sdk:DataGrid x:Name="MySilverlightDataGrid"
                          AutoGenerateColumns="True"
                          HorizontalAlignment="Stretch"
                          VerticalAlignment="Stretch">

            </sdk:DataGrid>
        </ScrollViewer>
    </Grid>

我的CodeBehind:

在方法范围内OnNavigatedTo我从视图中获取DataGridRowTemplate,将我的网格的ItemsSource分配给employees colection(在ctor中初始化),然后我将MySilverlightDataGrid_LoadingRow方法分配给LoadingRow事件。

MySilverlightDataGrid_LoadingRow方法获取加载的行及其DataContext,然后根据该DataContext的设置,我设置行模板。

protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            _template = LayoutRoot.Resources["DataGridRowTemplate"] as ControlTemplate;
            MySilverlightDataGrid.ItemsSource = _employees;
            MySilverlightDataGrid.LoadingRow += MySilverlightDataGrid_LoadingRow;
        }

        void MySilverlightDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            DataGridRow row = DataGridRow.GetRowContainingElement(e.Row);
            var rowEmployee = row.DataContext as Employee;
            if (rowEmployee != null && !rowEmployee.IsEmployeed)
            {
                row.Template = _template;
            }
        }

        private readonly IEnumerable<Employee> _employees;
        private ControlTemplate _template;

示例模型类:

当然,每次使用FullData getter时启动GetFullData方法都是愚蠢的,但是这个样本不是importand。

public class Employee
    {
        public string Name { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
        public string City { get; set; }
        public bool IsEmployeed { get; set; }
        public string FullData
        {
            get
            {
                return GetFullData();
            }
        }

        private string GetFullData()
        {
            return string.Format("Employee data - Name: {0}, Last name: {1}, Age: {2}, City: {3}, Is still employeed: {4}",
                Name, LastName, Age, City,
                (IsEmployeed ? "Yes" : "No"));
        }
    }
相关问题