I have such a class,
public class Class1
{
public bool IsSelected { get; set; } = false;
public List<BaseInfo> baseInfos { get; set; }
}
and a datagrid
<DataGrid
hc:DataGridAttach.CanUnselectAllWithBlankArea="True"
AutoGenerateColumns="True"
AutoGeneratingColumn="DataGrid_AutoGeneratingColumn"
HeadersVisibility="All"
IsReadOnly="True"
ItemsSource="{Binding Class1}"
RowHeaderWidth="60">
<DataGrid.RowHeaderTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=DataGridRow}}" />
</DataTemplate>
</DataGrid.RowHeaderTemplate>
</DataGrid>
I can't actually do that How should I do to baseInfos Set AutoGenerateColumns="True" Automatically generated Columns, Thank you in advance!
CodePudding user response:
ItemsSource binding should be a list and not a single class, and the IsSelected is expected to be a property inside BaseInfo and not something outside of it!!
Could not understand why you have Class1
as it does not help in anything!!
so add public bool IsSelected { get; set; } = false;
to the class BaseInfo. something like that:
public class BaseInfo {
public bool IsSelected { get; set; } = false;
public string Name { get; set; }
public string SOMETHING { get; set; }
}
CodePudding user response:
Autogeneratecolumns property will automatically create empty columns for you,
but if you want create new row ,You need to create a list of Class1 and bind it to itemsource
.cs:
public List<Class1> ClassList
{
get
{
return classList?? (classList= new List<Class1>());
}
set
{
classList= value;
}
}
List<Class1> classList;
add some items:
ClassList.Add(new Class1
{
IsSelected = true,
});
ClassList.Add(new Class1
{
IsSelected = true,
});
xaml:
<DataGrid ItemsSource="{Binding ClassList}">
</DataGrid>