C# DataGrid中的ComboxBox未在WPF中显示列表
我有一个Datagrid,在第一列中有一个ComboxBox,其中包含费用类型列表。用户需要选择费用类型,并在下一列(文本框)中输入金额。我已将其与ObservableCollection绑定,但列表未显示。这是我的xaml,我已经绑定了一个可观察的集合“lstFeeType”,它是一个FeeType列表,例如入学、实习等C# DataGrid中的ComboxBox未在WPF中显示列表,c#,wpf,combobox,wpfdatagrid,C#,Wpf,Combobox,Wpfdatagrid,我有一个Datagrid,在第一列中有一个ComboxBox,其中包含费用类型列表。用户需要选择费用类型,并在下一列(文本框)中输入金额。我已将其与ObservableCollection绑定,但列表未显示。这是我的xaml,我已经绑定了一个可观察的集合“lstFeeType”,它是一个FeeType列表,例如入学、实习等 <DataGrid Name="FTDataGrid" AutoGenerateColumns="False" CanUserAddRows="False" Grid.
<DataGrid Name="FTDataGrid" AutoGenerateColumns="False" CanUserAddRows="False" Grid.Row="5" Grid.ColumnSpan="2" ScrollViewer.VerticalScrollBarVisibility="Visible" Height="120" >
<DataGrid.Columns>
<DataGridTemplateColumn Header="Fee Type" Width="*" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=FeeTypeCollection}" Grid.Row="2" Grid.Column="1" Name="cmbFeeType" Width="165" Height="25" SelectedValuePath="Id" DisplayMemberPath="Description" SelectedItem="{Binding FeeType}" SelectedValue="{Binding FeeType}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Amount" Width="*" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Name="tbFeeAmount" Height="25" Width="140" Text="{Binding Path=Amount}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="+" Width="Auto" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button HorizontalAlignment="Center" VerticalAlignment="Center" Background="#2BB3EE" Foreground="#FFFDFAFA" Width="20" Height="20" Style="{StaticResource GelButton}" Click="AddRow_Click" >+</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="-" Width="Auto" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button HorizontalAlignment="Center" VerticalAlignment="Center" Background="#2BB3EE" Foreground="#FFFDFAFA" Width="20" Height="20" Style="{StaticResource GelButton}">-</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
+
-
代码:
observedcollection\lstFee;
公费
{
_lstFee=newobserveCollection(){new FeeCollection(){FeeTypeCollection=FeeTypeCollection()}};
初始化组件();
FTDataGrid.ItemsSource=\u lstFee;
}
private ObservableCollection FeeTypeCollection()
{
ObservableCollection lstFeeType=new ObservableCollection(){new Dictionary(){Id=1,Description=“acmission”},new Dictionary(){Id=1,Description=“Tution”},new Dictionary(){Id=1,Description=“Practicle”};
返回类型;
}
私有无效取消\单击(对象发送者,路由目标)
{
这个。关闭();
}
私有void AddRow\u单击(对象发送方,路由目标)
{
_添加(新的FeeCollection(){FeeTypeCollection=FeeTypeCollection()});
}
私有无效保存\单击(对象发送者,路由目标)
{
int a=FTDataGrid.Items.Count-1;
FeeCollection docPresObj=新的FeeCollection();
docPresObj=(FeeCollection)(FTDataGrid.Items[a]);
}
课程:
public class Dictionary
{
public int Id { get; set; }
public string Description { get; set; }
}
public class FeeCollection
{
public Dictionary FeeType { get; set; }
public double Amount { get; set; }
public ObservableCollection<Dictionary> FeeTypeCollection { get; set; }
}
公共类字典
{
公共int Id{get;set;}
公共字符串说明{get;set;}
}
公开课收费
{
公共字典类型{get;set;}
公共双倍金额{get;set;}
公共ObservableCollection FeeTypeCollection{get;set;}
}
我做错了什么,请告诉我。我的猜测是,由于FTDataGrids ItemSource设置为
\u lstFee
,它无法在FeeCollection中找到lstFeeType
(通常应显示为绑定错误)。我会尝试将“lstFeeType”作为属性放入FeeCollection
类中。请添加注释,显示lstFeeType的属性减速(也是您将对象分配给属性的位置),并请声明“什么是您的datacontext”。请参见第二行中的代码,我声明了一个可观察的集合lstFeeType,并且在表单的构造函数AddEditFee中,我正在从函数FeeTypeCollection中将列表分配给此属性。我不确定我分配给该lstFeeType的datacontext是否存在“datacontext=lstFeeType;”的原因?这不是组合框的项目来源吗?无论如何,我的猜测是,由于FTDataGrids ItemSource设置为“lstFee”,它在FeeCollection中找不到“lstFeeType”(通常应显示为绑定错误)。我会尝试将“lstFeeType”作为属性放入“FeeCollection”类中。谢谢,它解决了我的问题。请在单独的注释中添加您的答案,以便我将其标记为答案
public class Dictionary
{
public int Id { get; set; }
public string Description { get; set; }
}
public class FeeCollection
{
public Dictionary FeeType { get; set; }
public double Amount { get; set; }
public ObservableCollection<Dictionary> FeeTypeCollection { get; set; }
}