So, I need to make a WPF app that'll take values from the list and store them to DataGrid. I have 3 fields I want to show in the grid: Username, FullName and Role. Now, the Role can have 2 values: shopper and supplier. How do I make that part of the DataGridTemplate? In addition, how do I select the right one on load for every user? The empty line is where the missing code should be. Here's the code I'm working on:
<Grid>
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding UserList}" Name="dgUsers" HorizontalAlignment="Left" Height="450" Width="400">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Username" Width="100" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Username}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Full Name" Width="200" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding FullName}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Role" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
EDIT:
As a DataTemplate I did this:
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding valueList}" SelectedIndex="{Binding Role}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
As a Role, inside of the constructor, I did this:
public class UserToChange
{
public string Username { get; set; }
public string FullName { get; set; }
public int Role { get; set; }
public List<string> valueList { get; set; }
public UserToChange()
{
}
public UserToChange(UserVM row)
{
Username = row.UserName;
FullName = row.FirstName + " " + row.LastName;
Role = row.RoleId == 102 ? 0 : 1;
valueList = new List<string>
{
"Supplier",
"Shopper"
};
}
}
And it worked. Now I need to program the logic to update Database, but I can handle that. Thanks everyone!
You can bind the Collection in two ways.
1) Create a ComboBox
inside DataTemplate
<DataGridTemplateColumn Header="Role" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding dataList}" Width="100" DisplayMemberPath="{Binding DisplayProperty}" SelectedValuePath="{Binding PropertyId}" SelectedIndex="0" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
For ComboBox, Use SelectedIndex to set a default Item on Load.
2) Instead of DataGridTemplateColumn
use DataGridComboBoxColumn
<DataGridComboBoxColumn ItemsSource="{Binding dataList}" Width="100" Header="Role2" DisplayMemberPath="{Binding DisplayProperty}" SelectedValuePath="{Binding PropertyId}" SelectedItemBinding="{Binding DefaultValuePro, Mode=TwoWay}" />
For DataGridComboBoxColumn use SelectedItemBinding bind it with property to set a default value.
Bind the SelectedValue property of combo-box, and there comes to magic, bind SelectedValue to that variable which contains a Role value in a dataList.
Note: ListofRole collection should not be an of custom type, then we need to add some more parameters in combo box, it should be a normal string type.
<DataGridTemplateColumn Header="Role" Width="0.45*"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <ComboBox ItemsSource="{Binding ListOfRole}" SelectedValue="{Binding Role,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.