WPF DataGrid RowHeader databinding

前端 未结 6 2055
孤城傲影
孤城傲影 2020-12-05 13:17

I have a DataGrid, bound to a DataTable. I want to display text in the RowHeader, to achieve something like this:

         Col0      Col1      Col2      Col3         


        
相关标签:
6条回答
  • 2020-12-05 13:50

    I tried both answers, and neither worked for me. Essentially what I had to do was mix them together.

    This works for me:

    <DataGrid name="ui_dataGrid>
        <DataGrid.RowHeaderTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                                          AncestorType={x:Type DataGridRow}}, 
                                          Path=Item.Header}"/>
            </DataTemplate>
        </DataGrid.RowHeaderTemplate>
    </DataGrid>
    

    The trick is to find the ancestor DataGridRow, then Bind the TextBlock.Text attribute to its Item's property that you care about, in this case Header (easier said in XAML than English maybe).

    Then in the .xaml.cs:

    ui_dataGrid.ItemsSource = dataSource.Rows;
    

    N.B. Each Row object has a Header property which is what I'm binding too.

    0 讨论(0)
  • 2020-12-05 13:52

    You were almost there just change the AncestorType to DataGridRow instead of DataGrid then the row headers will be different for each row e.g.

    0 讨论(0)
  • 2020-12-05 13:53

    Just FYI, if you bind directly to a data table, then this binding text worked for me:

    {Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                                      AncestorType={x:Type DataGridRow}}, 
                                      Path=Item.Row.Header}
    

    Poked around a little bit and found that in the Item.Row.Header path, the path starts at the DataGridRow, the Item takes you to the DataGridView, and the Row takes you to the DataRow.

    Again, if you bind directly to the data table.

    0 讨论(0)
  • 2020-12-05 14:02

    Try to set the rowheader template, something like this

    <DataGrid>
            <DataGrid.RowHeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding YourProperty}"></TextBlock>
                </DataTemplate>
            </DataGrid.RowHeaderTemplate>uff
    
            //your stuff
    </DataGrid>
    
    0 讨论(0)
  • 2020-12-05 14:06

    2 ways to do it, the prev example almost had it but the binding would fail to resolve the property because the expression was missing "DataContext."

    <DataGrid>
            <DataGrid.RowHeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding DataContext.YourProperty}"></TextBlock>
                </DataTemplate>
            </DataGrid.RowHeaderTemplate>
    
            <!--your stuff-->
    </DataGrid>
    

    2nd way to do it is to create a converter to get the binding, parse it in the converter and spit out whatever string value you want:

    <Views:DataGridRowDataContextToRowHeaderValueConverter x:Key="toRowHeaderValue"/>
    
    <DataGrid.RowHeaderTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
                           AncestorType={x:Type DataGridRow}},
                           Converter={StaticResource toRowHeaderValue}}"/>
            </DataTemplate>
    </DataGrid.RowHeaderTemplate>
    

    Sample converter code:

    public class DataGridRowDataContextToRowHeaderValueConverter : IValueConverter
    {
        public object Convert (object value, Type targetType, object parameter, 
                               CultureInfo culture)
        {     
            var dataGridRow = (DataGridRow) value;
            var row = (GridModelExtensions.HourRow) dataGridRow.DataContext;
            return row.Days[0].Hour;
        }
    }
    
    0 讨论(0)
  • 2020-12-05 14:11

    @michele: If I modify the Binding to:

    <TextBlock Text="{Binding DataContext.YourProperty, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}}"/> 
    

    then it almost works. The problem is that this results in the same row header for each row.

    0 讨论(0)
提交回复
热议问题