简体   繁体   中英

Display a list of lists in datagrid with WPF

I have a class that contains list of other classes in them and I want to be able to use the list as the items source for a datagrid in WPF. Is it possible to make the inner list of other classes to appear in a dropdown or combobox. I could just show another sub form when the user clicks on a column that contains a collection but it would be more elegant if I could just see and edit the collection in place on the main grid.

This is what my class looks like:

    private string ChainID = string.Empty;
    private string Chain = string.Empty;
    private string State = string.Empty;
    private string NonMerchID = string.Empty;
    private string ReceiptText = string.Empty;
    private string Amount = string.Empty;
    **private List<string> CT1 = new List<string>(5);**
    private string RecyclingFeeFlag = string.Empty;
    private string MinPrompt = string.Empty;
    private string MaxPrompt = string.Empty;

CT1 is where my problem is.

Ok, so my XAML now looks like this:

    <DataGrid AutoGenerateColumns="False" Height="289" HorizontalAlignment="Left" Margin="10,10,0,0" Name="dataGrid1" VerticalAlignment="Top"
              Width="481" ItemsSource="{Binding nonMerchData}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Chain ID" Binding="{Binding Path=chainID}"></DataGridTextColumn>
            <DataGridTextColumn Header="Chain" Binding="{Binding Path=chain}"></DataGridTextColumn>
            <DataGridTextColumn Header="State" Binding="{Binding Path=state}"></DataGridTextColumn>
            <DataGridTextColumn Header="Non Merch ID" Binding="{Binding Path=nonMerchID}"></DataGridTextColumn>
            <DataGridTextColumn Header="Receipt Text" Binding="{Binding Path=receiptText}"></DataGridTextColumn>
            <DataGridTextColumn Header="Amount" Binding="{Binding Path=amount}"></DataGridTextColumn>
            <DataGridComboBoxColumn Header="CT" x:Name="_CT"></DataGridComboBoxColumn>
            <DataGridTextColumn Header="Recycling Fee Flag" Binding="{Binding Path=recyclingFeeFlag}"></DataGridTextColumn>
            <DataGridTextColumn Header="Min Prompt" Binding="{Binding Path=minPrompt}"></DataGridTextColumn>
            <DataGridTextColumn Header="Max Prompt" Binding="{Binding Path=maxPrompt}"></DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

It all works great except I can't figure out how to bind my inner list to the combox column.

There is an architectural mistake, I explain : ComboBox 's role is displaying choice list, so one to select, this leads that the list has to be provided from outside your object. But here, you want to display/edit what's inside CT1 list in a ComboBox . I think this wont do the trick for you unless you move to DataGridTemplateColumn , although, even this solution isn't the best for you scenario.

<DataGridTemplateColumn Header="template">
     <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox ItemsSource="{Binding Path=CT1}"/>
        </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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM