嵌套ListView无法正确绑定

时间:2014-09-06 18:09:44

标签: c# xaml data-binding collections

我正在使用XAML获取每个包含属性列表的产品列表。当我输入静态文本时,我有XAML功能,但是当我尝试使用对象属性时,没有任何显示。

这是我的XAML:

<ListView ItemsSource="{Binding Products}">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid HorizontalAlignment="Stretch">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition />
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding Name}" FontSize="32" Margin="5" />
                        <GridView Grid.Column="1" ItemsSource="{Binding Attributes}">
                            <GridView.ItemContainerStyle>
                                <Style TargetType="GridViewItem">
                                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                                    <Setter Property="HorizontalAlignment" Value="Stretch"/>
                                </Style>
                            </GridView.ItemContainerStyle>
                            <GridView.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding Key}" Foreground="White" Height="Auto"/>
                                        <TextBlock Text="{Binding Value}" Foreground="White" Height="Auto"/>
                                    </StackPanel>
                                </DataTemplate>
                            </GridView.ItemTemplate>
                        </GridView>

                        <Grid Grid.Column="2">
                            <Grid.RowDefinitions>
                                <RowDefinition />
                                <RowDefinition />
                            </Grid.RowDefinitions>

                            <TextBlock Text="Here is some selling point about product 1." FontSize="14" Margin="5" />
                            <StackPanel Grid.Row="1" Orientation="Horizontal">
                                <TextBlock Margin="5" FontSize="14" Text="1/4 oz: $150" />
                                <TextBlock Margin="5" FontSize="14" Text="1/2 oz: $250" />
                                <TextBlock Margin="5" FontSize="14" Text="1 oz: $450" />
                            </StackPanel>
                        </Grid>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
            <Style TargetType="ListView">
                <Setter Property="HorizontalAlignment" Value="Stretch"/>
            </Style>
        </ListView>

这是我的对象定义:

public class Product
{

    public string Name { get; set; }

    public string SellingPoint { get; set; }

    public List<KeyValuePair<string, string>> Attributes { get; set; }

    public Scale Scale { get; set; }

    public UnitOfMeasure UoM { get; set; }
}

我最初只是尝试使用字典作为属性,但发现字典没有XAML绑定。另一篇文章建议我使用List&gt;代替。我知道我绑定到属性很好,因为当我使用静态文本时,为我创建的模拟对象显示正确数量的属性(每个产品2个KeyValuePairs)。

When I use static text:

When I try and use Binding

1 个答案:

答案 0 :(得分:2)

也许这不是你要求的,但你可以为属性创建自己的类,例如:

public class Attribute
{
  public string Name {get;set;}
  public string Value {get;set;}
}

然后,您可以将List<Attribute>用作Product类中的属性,而不是List<KeyValuePair<string, string>>

您的XAML将如下所示:

<GridView Grid.Column="1" ItemsSource="{Binding Attributes}">
                            <GridView.ItemContainerStyle>
                                <Style TargetType="GridViewItem">
                                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                                    <Setter Property="HorizontalAlignment" Value="Stretch"/>
                                </Style>
                            </GridView.ItemContainerStyle>
                            <GridView.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding Name}" Foreground="White" Height="Auto"/>
                                        <TextBlock Text="{Binding Value}" Foreground="White" Height="Auto"/>
                                    </StackPanel>
                                </DataTemplate>
                            </GridView.ItemTemplate>
                        </GridView>