【问题标题】:How to Bind ItemsSource of ListBox which is a Child of ItemsControl?如何绑定 ItemsControl 的子 ListBox 的 ItemsSource?
【发布时间】:2014-03-04 02:22:36
【问题描述】:

假设我有一个类似于下面的视图/页面:

Category1     Category2     .........     Category(n)

Tile1         Tile1                       Tile1
Tile2         Tile2                       Tile2
.....         .....                       .....
.....         .....                       .....
Tile(n)       Tile(n)                     Tile(n)

用于创建上述输出的 XAML:

<ItemsControl ItemsSource="{Binding MenuCategories}" >

    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel IsItemsHost="True" Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid Height="500">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <TextBlock Text="{Binding Title}" FontSize="30" />

                <ListBox Grid.Row="1" x:Name="lst"
                         ItemsSource="{Binding ??????}" >

                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <WrapPanel IsItemsHost="True" Orientation="Vertical" MaxHeight="{Binding ElementName=lst, Path=ActualHeight}"/>
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>

                    <ListBox.Resources>
                        <Style TargetType="{x:Type ListBoxItem}">
                            <Setter Property="Width" Value="250" />
                            <Setter Property="Height" Value="125" />
                            <Setter Property="Margin" Value="2.5" />
                            <Setter Property="Padding" Value="2.5" />
                            <Setter Property="Background" Value="{Binding DataContext.Background, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBox}}, Converter={StaticResource stringToBrushConverter}}" />
                            <Setter Property="Foreground" Value="White" />
                            <Setter Property="VerticalContentAlignment" Value="Bottom" />
                            <Style.Triggers>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter Property="Foreground" Value="{Binding DataContext.Background, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBox}}, Converter ={StaticResource stringToBrushConverter}}" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </ListBox.Resources>

                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" Height="125" Width="250">
                                <Path Data="{Binding DataContext.ImageData, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}"  VerticalAlignment="Center" 
                                      Stretch="Uniform" Fill="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"
                                      Width="68" Height="68" Margin="10" RenderTransformOrigin="0.5,0.5">
                                    <Path.RenderTransform>
                                        <TransformGroup>
                                            <TransformGroup.Children>
                                                <RotateTransform Angle="0" />
                                                <ScaleTransform ScaleX="1" ScaleY="1" />
                                            </TransformGroup.Children>
                                        </TransformGroup>
                                    </Path.RenderTransform>
                                </Path>
                                <TextBlock Text="{Binding Title, Converter={StaticResource spaceToNewLineConverter}}" VerticalAlignment="Top" 
                                           Margin="40,10,10,10" FontSize="24" Foreground="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>

                </ListBox>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

但问题是我不知道ListBox的ItemsSource的绑定。所以,我没有得到如上所示的Output。

这是我的数据库表的样子:

【问题讨论】:

  • 显示MenuCategories属性的定义..
  • 私有 ObservableCollection _menuCategories;公共 ObservableCollection MenuCategories { get { return _menuCategories; } 设置 { _menuCategories = 值; OnPropertyChanged("MenuCategories") }
  • Design_Master_Category 是否有包含Design_Master_TileItems 集合的属性?如果是,您可以将 ListBox 的 ItemsSource 绑定到该属性
  • 不,我只得到空的列表框。
  • 如果您在运行时检查该属性(调试->断点),它实际上是空的吗?如果是,那么问题不在于绑定部分

标签: c# wpf xaml mvvm


【解决方案1】:

您可以简单地绑定到Design_Master_Category 存储集合Design_Master_TileItems,例如:

......
<ListBox Grid.Row="1" x:Name="lst"
         ItemsSource="{Binding Design_Master_TileItems} >
 ......

如果您发现该属性在运行时如注释中所述为空,则表示问题不在您的绑定上。您需要修复您的查询。我对 EF 的经验并不丰富,但这是我的建议:

查询Design_Master_Category表时尽量使用.Include(o =&gt; o.Design_Master_TileItems)函数。 [Reference]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    • 1970-01-01
    • 2012-02-18
    • 2010-11-29
    • 1970-01-01
    • 2010-11-24
    • 2010-12-21
    相关资源
    最近更新 更多