【问题标题】:How do I bind a Silverlight ItemsControl inside of a databound ListBox.ItemTemplate?如何在数据绑定 ListBox.ItemTemplate 内绑定 Silverlight ItemsControl?
【发布时间】:2010-12-20 02:18:33
【问题描述】:

我正在编写一个 Silverlight 应用程序。我想要一个垂直显示行的列表框。然后对于每一行,该行应该有一个标题列,然后是面板的水平列表。我已经弄清楚了布局。我的问题在于数据绑定。

ListBox 被绑定到一个集合。该集合中的每个项目都将成为列表框中的一行。集合中的每个项目也都有一个集合,该集合将绑定到每个 ListBox 行内的 ItemsControl ItemsSource。

例如

[标题][x][y][z] [标题][x2][y2][z2] [标题][x3][y3][z3]

我需要使用什么绑定语法?

<ListBox Name="listRuleSteps" Height="150" Loaded="ListBox_Loaded" ScrollViewer.VerticalScrollBarVisibility="Auto">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="150"></ColumnDefinition>
                            <ColumnDefinition></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <StackPanel Grid.Column="0" Orientation="Vertical" Height="50">
                            <dataInput:Label Content="{Binding StepID}"></dataInput:Label>
                        </StackPanel>

                        <StackPanel Grid.Column="1" Orientation="Vertical">
                            <ItemsControl ItemsSource="{Binding SelectedItem.RuleEngineParts, ElementName=listRuleSteps}" >
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <controlToolkit:WrapPanel Orientation="Horizontal" />
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <controlToolkit:WrapPanel Width="100">
                                            <dataInput:Label Content="Text1"></dataInput:Label>
                                        </controlToolkit:WrapPanel>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>

                            </ItemsControl>                           

                        </StackPanel>
                    </Grid>
                </DataTemplate>

            </ListBox.ItemTemplate>
        </ListBox>

我认为问题出在这条线上。我显然不想使用 SelectedItem,但我不确定要绑定 ItemsSource 的内容。

<ItemsControl ItemsSource="{Binding SelectedItem.RuleEngineParts, ElementName=listRuleSteps}" >

如果您认为我这样做的方式完全错误,请告诉我。我真的是 Silverlight 的新手。

【问题讨论】:

    标签: silverlight wcf-binding


    【解决方案1】:

    首先,我认为这里不需要datainput:Label 控件,一个简单的TextBlock 在其Text 属性上进行绑定也可以在没有额外负担的情况下工作。

    在内部ItemsControl 中,您可以像这样简单地绑定:-

    <ItemsControl ItemsSource="{Binding RuleEngineParts}"
    

    现在您可以将内部 TextBlocks Text 属性绑定到在 RuleEngineParts 集合中找到的任何对象的适当属性。

    【讨论】:

      猜你喜欢
      • 2010-09-26
      • 1970-01-01
      • 1970-01-01
      • 2012-08-23
      • 1970-01-01
      • 1970-01-01
      • 2014-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多