Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

C# WPF Bindind DataGrid Columns Properties to list of classes

can anyone help me with binding properties of datagridcolumn? I just don't understand how to do it.

I have DataGrid with columns: xaml

<DataGrid x:Name="DataGrid" ItemsSource="{Binding Path=RPS, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False"
                      IsReadOnly="True" SelectedItem="{Binding SelectedItem}"
                          material:DataGridAssist.CellPadding="0 5 3 5"
                          material:DataGridAssist.ColumnHeaderPadding="4 2 2 2"
                          Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType=UserControl}}">

                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="SelectionChanged">
                                <i:InvokeCommandAction Command="{Binding SelectionChangedCommand}" CommandParameter="{Binding ElementName=DataGrid, Path=SelectedItems}"/>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                     
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="##" Binding="{Binding TestNumber}"></DataGridTextColumn>
                            <DataGridTemplateColumn Header="State">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <Image Source="{Binding Icon}" Width="15" VerticalAlignment="Center" ToolTip="{Binding Message}" Height="15"></Image>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                            
                           
                            <DataGridTextColumn x:Name="ColSize" Header="Size" Binding="{Binding Size}"></DataGridTextColumn>
                            <DataGridTextColumn x:Name="ColName" Header="File Name" Binding="{Binding Name}"></DataGridTextColumn>
                            <DataGridTextColumn x:Name="ColVehicle" Header="Vehicle" Binding="{Binding Vehicle}"></DataGridTextColumn>
                            <DataGridTextColumn x:Name="ColVersion" Header="Version" Binding="{Binding Version}"></DataGridTextColumn>
                            <DataGridTextColumn x:Name="ColDateTime" Header="DateTime" Binding="{Binding DateTime, StringFormat={0:dd.MM.yy HH:mm:ss}}"></DataGridTextColumn>
                            <DataGridTextColumn x:Name="ColUnix" Header="Unix" Binding="{Binding ArenaCreationTime}"></DataGridTextColumn>
                           
                        </DataGrid.Columns>
                        
                    </DataGrid>

And List of columns params:

public List<DataGridColumn> Columns { get; set; }

class have variables:

public class DataGridColumn
{
public string Text { get; set; }
public string SubText { get; set; }
public bool IsVisible { get; set; }
public int Width { get; set; }
public int Position { get; set; }
}

So, i don't know how bind columns with that list of classes. Help me pls.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can bind to your Columns collection in XAML but it is not so easy. And you need to have fixed index numbers in the Columns collection.

This is a starting point for what you can do in the code-behind instead.

This is the saving part in the Closing event handler:

private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        //Extract the properties for each column into the Columns collection
        Columns.Clear();
        foreach (DataGridColumn col in DataGrid.Columns)
        {
            Columns.Add(new MyDataGridColumn() 
            { 
                Text = col.Header?.ToString(), 
                Width=col.Width
                //...
            });
        }
    }

Revert the action in the Loading event to set the values.

You may need a little more advanced code to find the correct column. Here I have hardcoded to the first column.

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        foreach (MyDataGridColumn col in Columns)
        {
            DataGrid.Columns[0].Header = col.Text;
            DataGrid.Columns[0].Width = col.Width;
    //..
        }
    }

Some observations:

Your name of class DataGridColumn conflict with the DataGrid's DataGridColumn. I have renamed your class to "MyDataGridColumn" above.

The DataGridColumn property Width is of type DataGridLength.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...