简体   繁体   中英

DataGrid within ListBox ItemTemplate not inheriting bindings?

I've got some nested controls that don't seem to be inheriting bindings correctly... or it could be something else entirely. I'm a bit new to WPF.

I've got an ObservableCollection of objects bound to the ListBox and at the top level this appears to be working since a row is added for each item in the collection as expected. The problem is that I need to place a DataGrid within the ListBox's ItemTemplate and...

仅具有标题的DataGrid

All I get are the headers, which I specified manually. No data from the bound objects.

I've probably just done something dumb. Here's the code, slightly simplified:

XAML:

<ListBox x:Name="ReportListBox" Margin="10,10,10,10" Grid.Row="1" ItemsSource="{Binding Path=SummaryTotals, Mode=OneWay}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <DataGrid>
                        <DataGrid.Columns>
                            <DataGridTextColumn Binding="{Binding Date, StringFormat=\{0:MM/dd/yyyy\}}" Header="Date"/>
                            <DataGridTextColumn Binding="{Binding GrossRevenue}" Header="Gross Revenue" />
                            <DataGridTextColumn Binding="{Binding OurRevenue}" Header="Our Revenue" />
                            <DataGridTextColumn Binding="{Binding Cost}" Header="Cost" />
                            <DataGridTextColumn Binding="{Binding Profit}" Header="Profit" />
                            <DataGridTextColumn Binding="{Binding Roi}" Header="ROI" />
                            <DataGridTextColumn Binding="{Binding Rpc}" Header="RPC" />
                            <DataGridTextColumn Binding="{Binding Clicks}" Header="Clicks" />
                            <DataGridTextColumn Binding="{Binding Conversions}" Header="Conversions" />
                            <DataGridTextColumn Binding="{Binding ConversionRate}" Header="Conversion Rate" />
                        </DataGrid.Columns>
                    </DataGrid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

C#:

public class SummaryTotalRecord
        {
            public DateTime Date { get; set; }
            public decimal Cost { get { return Google.Cost + Ypa.PureLocal.Cost + Ypa.BuyerPricer.Cost; } }
            public decimal GrossRevenue { get { return Google.GrossRevenue + Ypa.PureLocal.GrossRevenue + Ypa.BuyerPricer.GrossRevenue; } }
            public decimal OurRevenue { get { return Google.OurRevenue + Ypa.PureLocal.OurRevenue + Ypa.BuyerPricer.OurRevenue; } }
            public decimal Roi { get { return (Google.Roi + Ypa.PureLocal.Roi + Ypa.BuyerPricer.Roi)/3; } }
            public decimal ConversionRate { get { return (Google.ConversionRate + Ypa.PureLocal.ConversionRate + Ypa.BuyerPricer.ConversionRate)/3; } }
            public decimal Profit { get { return Google.Profit + Ypa.PureLocal.Profit + Ypa.BuyerPricer.Profit; } }
            public decimal Rpc { get { return (Google.Rpc + Ypa.PureLocal.Rpc + Ypa.BuyerPricer.Rpc)/3; } }
            public long Clicks { get { return Google.Clicks + Ypa.PureLocal.Clicks + Ypa.BuyerPricer.Clicks; } }
            public long Conversions { get { return Google.Conversions + Ypa.PureLocal.Conversions + Ypa.BuyerPricer.Conversions; } }
            // ... plus a bunch of other properties I haven't touched yet
        }

public ObservableCollection<SummaryTotalRecord> SummaryTotals = new ObservableCollection<SummaryTotalRecord>();

I found others with similar problems and the accepted fix seems to be something like

RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBox}}

But neither that, nor x:Type ListItem seems to produce any difference in results.

Update: Trying a new method with similar results. I've changed my class to return a DataTable called TableData and modified the XAML like so:

<Expander IsExpanded="True" Header="{Binding Date, StringFormat=\{0:MM/dd/yyyy\}}">
                        <DataGrid DataContext="{Binding Path=TableData}" AutoGenerateColumns="True" />
                    </Expander>

Similar results:

扩展器可以访问,但DataGrids无法访问?

The Expander has access to the Date property, but the DataGrid it contains doesn't have access to TableData ?

I ended up adding a new property to the bound object:

public DataTable TableData { get { var table = new DataTable();

                table.Columns.Add("Date", typeof (string));
                table.Columns.Add("Gross Revenue", typeof(string));
                table.Columns.Add("Our Revenue", typeof(string));
                table.Columns.Add("Cost", typeof(string));
                table.Columns.Add("Profit", typeof(string));
                table.Columns.Add("ROI", typeof(string));
                table.Columns.Add("RPC", typeof(string));
                table.Columns.Add("Clicks", typeof(long));
                table.Columns.Add("Conversions", typeof(long));
                table.Columns.Add("Conversion Rate", typeof(string));

                table.Rows.Add(
                    Date.ToString("MM/dd/yyyy"),
                    GrossRevenue.ToString("C"),
                    OurRevenue.ToString("C"),
                    Cost.ToString("C"),
                    Profit.ToString("C"),
                    Roi.ToString("P"),
                    Rpc.ToString("P"),
                    Clicks,
                    Conversions,
                    ConversionRate.ToString("P")
                );

                return table;
            }
        }

At which point the following XAML worked:

<DataGrid ItemsSource="{Binding Path=TableData}" AutoGenerateColumns="True" 
CanUserAddRows="False" />

I still have no idea why the original code failed and this code works.

It makes perfect sense why the Grid was empty; it wasn't bound to any collection. If the source isn't specified, then there is none, and the grid will be empty. This isn't something that can be inferenced by the binding engine.

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