在工具箱扩展器视图中绑定嵌套列表

时间:2014-02-15 11:28:53

标签: wpf listview binding windows-phone expander

我的项目中有一个小问题,我正在使用Windows Phone Toolkit的ExpanderView,我无法成功绑定嵌套列表。

情况如此,我有两个班级:

  • 帐户
  • PersonObj

帐户类公开三个属性:

public ObservableCollection<PersonObj> People;
public string TotalText;
public string PeopleText;

PersonObj:

public string Name
public string QuotaText

相关的XAML是:

<toolkit:ExpanderView.HeaderTemplate>
      <DataTemplate>
          <TextBlock Text="{Binding Description}" Foreground="{StaticResource PhoneForegroundBrush}"
                                            FontSize="{StaticResource PhoneFontSizeExtraLarge}" 
                                            FontFamily="{StaticResource PhoneFontFamilySemiLight}"/>
                                </DataTemplate>
                            </toolkit:ExpanderView.HeaderTemplate>

                            <toolkit:ExpanderView.ExpanderTemplate>
                                <DataTemplate>
                                    <TextBlock Grid.Row="0" Grid.Column="1"
                                               LineHeight="25" LineStackingStrategy="BlockLineHeight"
                                               TextWrapping="Wrap">
                                        <Run Text="{Binding TotalText}"
                                             Foreground="Red"
                                             FontSize="{StaticResource PhoneFontSizeNormal}" 
                                             FontFamily="{StaticResource PhoneFontFamilySemiBold}"/> 
                                        <LineBreak/>
                                        <Run Text="{Binding PeopleText}"   
                                             Foreground="{StaticResource PhoneSubtleBrush}"
                                             FontSize="{StaticResource PhoneFontSizeNormal}"
                                             FontFamily="{StaticResource PhoneFontFamilyNormal}"/>
                                    </TextBlock>
                                </DataTemplate>
                            </toolkit:ExpanderView.ExpanderTemplate>

                            <toolkit:ExpanderView.ItemTemplate>
                                <DataTemplate>
                                    <ListBoxItem toolkit:TiltEffect.IsTiltEnabled="True">
                                        <StackPanel>
                                            <TextBlock Text="{Binding People.Name}"       
                                                   Margin="0,8,0,-4"
                                                   Foreground="{StaticResource PhoneForegroundBrush}"
                                                   FontSize="{StaticResource PhoneFontSizeExtraLarge}" 
                                                   FontFamily="{StaticResource PhoneFontFamilySemiLight}"/>
                                                <TextBlock Text="{Binding People.QuotaText}"
                                                   TextWrapping="Wrap"
                                                   Margin="0,0,0,-2"
                                                   Foreground="Red"
                                                   FontSize="{StaticResource PhoneFontSizeNormal}" 
                                                   FontFamily="{StaticResource PhoneFontFamilyNormal}"/>
                                        </StackPanel>
                                    </ListBoxItem>
                                </DataTemplate>
                            </toolkit:ExpanderView.ItemTemplate>

我在ListView中拥有控件。

在后面的代码中我设置了ListView.ItemSource

ListView.ItemsSource = application.Equalize.Accounts;

现在,这一切似乎都有效但是TotalText和PeopleText这两个属性显示出来但是嵌套类Name和QuotaText的属性没有显示出来。 我哪里错了?

1 个答案:

答案 0 :(得分:0)

问题在于这种约束:Binding People.Name 您不能指望这样做,因为“People”属性的类型为List,而List中没有Name属性!!

相反,你应该在这里做的是在ExpanderView.ItemTemplate中有一个ItemsControl(例如:ListBox),然后在ItemsControl的ItemTemplate中使用你的ListBoxItem模板。

类似的东西:

<toolkit:ExpanderView.ItemTemplate>
                                <DataTemplate>
<ListBox ItemsSource="{Binding Path=People}">
    <ListBox.ItemTemplate>
            <ListBoxItem toolkit:TiltEffect.IsTiltEnabled="True">...
//your current code
     </ListBox.ItemTemplate>
</ListBox>
<toolkit: ExpanderView.ItemTemplate>...

这实际上取决于您希望如何显示List内部扩展器视图。但希望你明白这一点。

相关问题