0

如何在后面的 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# 中动态加载项目。我怎么能解决这个问题?提前致谢。

4

1 回答 1

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;
}

希望有帮助。

于 2014-01-04T12:37:44.050 回答