在Windows商店应用中访问Hud Section的内部元素?

时间:2013-10-10 13:13:43

标签: windows-store-apps windows-store

如何在C#代码中设置ListView itemSource? Hun Sections中的元素无法通过名称访问。

<Hub x:Name="RootHub" SectionHeaderClick="Hub_SectionHeaderClick">
        <Hub.Header>
            <!-- Back button and page title -->
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="80"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Button  x:Name="backButton" Style="{StaticResource NavigationBackButtonNormalStyle}"
                    Margin="0,0,39,0" 
                    VerticalAlignment="Top"
                    Command="{Binding NavigationHelper.GoBackCommand, ElementName=pageRoot}"
                    AutomationProperties.Name="Back"
                    AutomationProperties.AutomationId="BackButton"
                    AutomationProperties.ItemType="Navigation Button"/>
                <TextBlock x:Name="pageTitle" Text="{StaticResource AppName}" Style="{StaticResource HeaderTextBlockStyle}" Grid.Column="1" 
                    VerticalAlignment="Top" IsHitTestVisible="false" TextWrapping="NoWrap" />
            </Grid>
        </Hub.Header>
        <HubSection Width="500" x:Name="OperationSection" Header="Operation">
            <DataTemplate>
                <Grid Height="535">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" MinHeight="92" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <SearchBox Margin="0,0,50,394"/>
                    <Image Source="Images/plus.png" Margin="375,6,4,394"/>
                    <ListView x:Name="OperationListView" ItemsSource="{Binding Operation}" HorizontalAlignment="Left" Height="453" VerticalAlignment="Top" Width="410" Margin="6,72,0,0" Grid.RowSpan="2">
                        <!--<ListView.ItemTemplate>
                            <DataTemplate>
                                <TextBlock x:Name="DataItem" Text="{Binding opId}" />

                            </DataTemplate>
                        </ListView.ItemTemplate>-->
                        <ListBoxItem>
                        <StackPanel Orientation="Vertical">
                            <TextBlock Text="OP : 0001" Width="auto" Height="30" FontSize="20"></TextBlock>
                                <TextBlock Text="Replace Transformer" Width="auto" Height="30" FontSize="20"></TextBlock>
                        </StackPanel>
                        </ListBoxItem>
                        <ListBoxItem>
                        <StackPanel Orientation="Vertical">
                                <TextBlock Text="OP : 0002" Width="auto" Height="30" FontSize="20"></TextBlock>
                                <TextBlock Text="Install New Pole" Width="auto" Height="30" FontSize="20"></TextBlock>
                        </StackPanel>
                        </ListBoxItem>
                        <ListViewItem>
                        <StackPanel Orientation="Vertical">
                                <TextBlock Text="OP : 0003" Width="auto" Height="30" FontSize="20"></TextBlock>
                                <TextBlock Text="Check Equipments" Width="auto" Height="30" FontSize="20"></TextBlock>
                        </StackPanel>
                        </ListViewItem>
                    </ListView>
                </Grid>
            </DataTemplate>
        </HubSection>
        <HubSection Width="500" x:Name="ComponentSection" Header="Component">
            <DataTemplate>
                <Grid Height="535">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" MinHeight="92" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <SearchBox Margin="0,0,50,394"/>
                    <Image Source="Images/plus.png" Margin="375,6,4,394"/>
                    <ListView x:Name="OperationListView" ItemsSource="{Binding Operation}" HorizontalAlignment="Left" Height="453" VerticalAlignment="Top" Width="410" Margin="6,72,0,0" Grid.RowSpan="2">
                    <ListBoxItem>
                            <StackPanel Orientation="Vertical">
                                <TextBlock Text="COM : 0001" Width="auto" Height="30" FontSize="20"/>
                                <TextBlock Text="Transformer" Width="auto" Height="30" FontSize="20"/>
                            </StackPanel>
                        </ListBoxItem>
                        <ListBoxItem>
                            <StackPanel Orientation="Vertical">
                                <TextBlock Text="COM : 0002" Width="auto" Height="30" FontSize="20"/>
                                <TextBlock Text="Pole" Width="auto" Height="30" FontSize="20"/>
                            </StackPanel>
                        </ListBoxItem>
                        <ListViewItem>
                            <StackPanel Orientation="Vertical">
                                <TextBlock Text="COM : 0003" Width="auto" Height="30" FontSize="20"/>
                                <TextBlock Text="Grid" Width="auto" Height="30" FontSize="20"/>
                            </StackPanel>
                        </ListViewItem>
                    </ListView>
                </Grid>
            </DataTemplate>
        </HubSection>
        <HubSection Width="500" x:Name="EquipmentSection" Header="Equipment">
            <DataTemplate>
                <Grid Height="535">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" MinHeight="92" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <SearchBox Margin="0,0,50,394"/>
                    <Image Source="Images/plus.png" Margin="375,6,4,394"/>
                    <ListView x:Name="OperationListView" ItemsSource="{Binding Operation}" HorizontalAlignment="Left" Height="453" VerticalAlignment="Top" Width="410" Margin="6,72,0,0" Grid.RowSpan="2">
                      <ListBoxItem>
                            <StackPanel Orientation="Vertical">
                                <TextBlock Text="EQ : 0001" Width="auto" Height="30" FontSize="20"/>
                                <TextBlock Text="Transformer" Width="auto" Height="30" FontSize="20"/>
                            </StackPanel>
                        </ListBoxItem>
                        <ListBoxItem>
                            <StackPanel Orientation="Vertical">
                                <TextBlock Text="EQ : 0002" Width="auto" Height="30" FontSize="20"/>
                                <TextBlock Text="Pole" Width="auto" Height="30" FontSize="20"/>
                            </StackPanel>
                        </ListBoxItem>
                        <ListViewItem>
                            <StackPanel Orientation="Vertical">
                                <TextBlock Text="EQ : 0003" Width="auto" Height="30" FontSize="20"/>
                                <TextBlock Text="Wire" Width="auto" Height="30" FontSize="20"/>
                            </StackPanel>
                        </ListViewItem>
                    </ListView>
                </Grid>
            </DataTemplate>
        </HubSection>
    </Hub>

这里我在OperationListView中有硬编码列表项。但我想在C#中动态加载项目。我怎么能解决这个问题? 提前谢谢。

1 个答案:

答案 0 :(得分:1)

问题的几个问题是你已经将ListView绑定到名为Operation的属性。删除单个ListView项目,您的数据将显示。您还需要ListView.ItemTemplate来显示各个项目。

数据绑定是这里的方法,但你也绑定到所有列表的相同属性,所以我假设你想绑定到每个ListView的不同属性,否则每个HubSection将包含相同的项。

如果您不想使用数据绑定并加载后面代码中的项目,可以使用ListView的Loaded事件。

<ListView x:Name="OperationListView" Loaded="OperationsLoaded">
</ListView>

然后在你的代码背后:

private void OperationsLoaded(object sender, RoutedEventArgs e)
{
  var listView = (ListView)sender;
  listView.ItemsSource = youritems;
}

希望有所帮助。