我有一个 CommentsData 类,它用于在 DataGrid 中加载、操作和保存值。我想让类中的状态字段显示为网格中的下拉列表。注释值只需填充一次。我尝试了很多变化,但这不起作用。组合是空白的。I need to be able to populate the values in the combo and when the selection changes the value should remain there and not dissappear.
这是网格的 Xaml(更新 2)
<DataGrid Grid.Row="2" AutoGenerateColumns="False" Height="Auto" HorizontalAlignment="Stretch" Name="grdComments" VerticalAlignment="Stretch" CanUserAddRows="True" CanUserDeleteRows="True" BeginningEdit="grdComments_BeginningEdit" InitializingNewItem="grdComments_InitializingNewItem" PreviewKeyDown="grdComments_PreviewKeyDown" SizeChanged="grdComments_SizeChanged">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Author}" Header="Author" />
<DataGridTemplateColumn Header="Status" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding UserValues}" DisplayMemberPath="UserStatus" SelectedValuePath="UserStatus" SelectedValue="{Binding Status, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding Path=Comment}" Header="Comment" Width="570" />
</DataGrid.Columns>
这是 CommentData 类的代码(更新 2)
public class CommentsData
{
public string Author { get; set; }
public string Status { get; set; }
public string Comment { get; set; }
public string Username { get; set; }
public ObservableCollection<StatusValue> UserValues { get; set; }
public CommentsData()
{
UserValues = new ObservableCollection<StatusValue>();
UserValues.Add(new StatusValue("New"));
UserValues.Add(new StatusValue("Open"));
UserValues.Add(new StatusValue("ReOpen"));
UserValues.Add(new StatusValue("Closed"));
}
}
public class StatusValue
{
public string UserStatus { get; set; }
public StatusValue (string value)
{
UserStatus = value;
}
}
这是初始化注释列表的代码
private List<CommentsData> _commentsList;
private void InitializeObjects()
{
_commentsList = new List<CommentsData>();
grdComments.ItemsSource = _commentsList;
}
上面的代码正在运行感谢所有的反馈