【发布时间】:2016-09-11 05:15:45
【问题描述】:
它很复杂,因为我正在通过对象 UserX.. 进行复制列表。 我有用户列表,我想在表格中显示它们。 我需要在 Role 列中,comboBox 的默认值将是用户的当前角色,我需要使用 ComboBox 更改此角色的选项,但我不知道如何(可能的角色是:管理员、经理和员工)
感谢您的帮助。代码如下:
XAML
<DataGrid FontSize="20" Name="dgUsers" ItemsSource="{Binding list}" AutoGenerateColumns="False" ScrollViewer.VerticalScrollBarVisibility="Visible">
<DataGrid.Columns>
<DataGridTextColumn Header="#" Binding="{Binding Id}" />
<DataGridTextColumn Header="Username" Binding="{Binding Username}" />
<DataGridTextColumn Header="Password" Binding="{Binding Password}" />
<DataGridComboBoxColumn Header="Role" ItemsSource="{Binding Role}" />
<DataGridTemplateColumn Header="Remove">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="ButtonRemove" Content="Remove User"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
CS
List<User> users = user.getUsersList();
List<Userx> updated = new List<Userx>();
//coping from the list users to list updated. I did it to gete the binding
for (int i = 0; i < users.Count; i++)
updated.Add(new Userx() { Id = i + 1, Username = users.ElementAt(i).getUsername(),
Password = users.ElementAt(i).GetPassword(),
Role = users.ElementAt(i).getRole() });
dgUsers.ItemsSource = updated;
}
【问题讨论】:
标签: wpf data-binding wpfdatagrid datagridcomboboxcolumn