【问题标题】:ComboxBox in DataGrid not showing List in WPFDataGrid中的组合框未在WPF中显示列表
【发布时间】:2018-06-17 22:46:03
【问题描述】:

我有一个 Datagrid,在第一列内我有一个 ComboxBox,其中包含费用类型列表。用户需要选择费用类型并在下一列中输入金额,这是一个文本框。我已将其与 ObservableCollection 绑定但列表没有显示出来。这是我的 xaml,请参阅我已经绑定了一个 observablecollection 'lstFeeType',它是 FeeType 的列表,例如 Admission、Practicle 等。

<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>

代码:

ObservableCollection<FeeCollection> _lstFee;
    public AddEditFee()
    {
        _lstFee = new ObservableCollection<FeeCollection>() { new FeeCollection() { FeeTypeCollection = FeeTypeCollection() } };
        InitializeComponent();
        FTDataGrid.ItemsSource = _lstFee;
    }
    private ObservableCollection<Dictionary> FeeTypeCollection()
    {

        ObservableCollection<Dictionary> lstFeeType = new ObservableCollection<Dictionary>() { new Dictionary() { Id = 1, Description = "Admission" },new Dictionary() { Id = 1, Description = "Tution" },new Dictionary() { Id = 1, Description = "Annual" }, new Dictionary() { Id = 1, Description = "Practicle" }};
        return lstFeeType;
    }
    private void Cancel_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }

    private void AddRow_Click(object sender, RoutedEventArgs e)
    {
        _lstFee.Add(new FeeCollection() { FeeTypeCollection = FeeTypeCollection() });

    }

    private void Save_Click(object sender, RoutedEventArgs e)
    {
        int a =FTDataGrid.Items.Count-1;
        FeeCollection docPresObj = new 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; }
}

我做错了什么,请指导我。

【问题讨论】:

  • 请添加评论以显示您对 lstFeeType 的属性减速(也是您将对象分配给属性的位置),并请说明“您的数据上下文是什么”。
  • 在第二行的代码中查看我已经声明了一个可观察的集合 lstFeeType,并且在我的表单的构造函数 AddEditFee 中,我从函数 FeeTypeCollection 将 List 分配给该属性。我不确定我分配的数据上下文那个 lstFeeType 给它
  • “DataContext = lstFeeType;”是否有原因?不是 Combobox 的 ItemsSource 吗?无论如何,我的猜测是,由于 FTDataGrids ItemSource 设置为“_lstFee”,因此在 FeeCollection 中找不到“lstFeeType”(通常应显示为绑定错误)。我会尝试将“lstFeeType”作为属性放入“FeeCollection”类中。
  • 感谢它解决了我的问题。请在单独的评论中添加您的答案,以便我将其标记为答案

标签: c# wpf combobox wpfdatagrid


【解决方案1】:

我的猜测是,由于 FTDataGrids ItemSource 设置为 _lstFee,它无法在 FeeCollection 中找到 lstFeeType(通常应显示为绑定错误)。我会尝试将“lstFeeType”作为属性放入FeeCollection 类中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-03
    • 2011-06-26
    • 2013-09-30
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    相关资源
    最近更新 更多