C# Listview设置Listview项目windows应用商店应用的默认选定索引

C# Listview设置Listview项目windows应用商店应用的默认选定索引,c#,listview,data-binding,windows-store-apps,listviewitem,C#,Listview,Data Binding,Windows Store Apps,Listviewitem,我的代码包含两个listview。一个用于类别,另一个用于第一个列表中选定项的数据。我想设置类别列表的默认选定索引0,第二个listview将数据显示为第一个listview的选定索引。当my mainpage显示类别时,这两个listview都在my mainpage.xaml中。listview的索引=0被选中,而另一个listview显示第一个listview的选定类别的数据。listview都从internet获取数据。怎么可能呢 ? 请帮帮我。第一个listview的代码:` <

我的代码包含两个listview。一个用于类别,另一个用于第一个列表中选定项的数据。我想设置类别列表的默认选定索引0,第二个listview将数据显示为第一个listview的选定索引。当my mainpage显示类别时,这两个listview都在my mainpage.xaml中。listview的索引=0被选中,而另一个listview显示第一个listview的选定类别的数据。listview都从internet获取数据。怎么可能呢 ? 请帮帮我。第一个listview的代码:`

<ListView x:Name="ctlist" ItemsSource="{Binding}"   SelectionChanged="ctlist_SelectionChanged"
SelectionMode="Single"  ScrollViewer.HorizontalScrollMode="Enabled"                 ScrollViewer.HorizontalScrollBarVisibility="Visible" Margin="0,0,0,0" Width="1918" Height="80"   ItemContainerStyle="{StaticResource OrangeListViewItemStyle}" >   
     <ListView.ItemsPanel >
         <ItemsPanelTemplate>
            <StackPanel x:Name="stak3" Orientation="Horizontal" />
          </ItemsPanelTemplate>
      </ListView.ItemsPanel>
      <ListView.ItemTemplate>
          <DataTemplate>
              <StackPanel  Orientation="Horizontal" >
                  <TextBlock Text="{Binding Path=Name}"  Margin="10,20,10,10" FontSize="25" Foreground="Black"   />
               </StackPanel>
            </DataTemplate>
          </ListView.ItemTemplate>
      </ListView>`
代码:

 ctlist ctlistid;
      private void ctlist_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var app = App.Current as App;
            app.selectedct = (ctlist)ctlist.SelectedItem;
            ctlistid = app.selectedct;
            string no = ctlistid.Id.ToString();
            displayurl(no); // method for binding data to listview

        }

如果我很理解您的问题,您希望将类别列表视图的SelectEditedIndex设置为0,并为显示所选类别数据的第二个列表视图绑定数据

您可以按如下方式执行此操作:

在为ctlist设置数据源后的LoadState事件中,手动设置所选索引,然后调用Selection Changed事件。如下图所示:

private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
    {

        //TODO Set / Bind the data for Category List
        // do the binding here

        //Set the Selected Index
        ctlist.SelectedIndex = 0;

        //Manually call the selection changed event to set the data for the Categroy List
        ctlist_SelectionChanged(null, null);
    }

希望这有帮助

我找到了这个问题的答案,在向ctlist listbox提供数据源后,只需分配这个关键字即可设置默认的选定项

   private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
          {
            //TODO Set / Bind the data for Category List
             ctlist.ItemsSource = lst;

              this.ctlist.SelectedIndex = 0; 
          }

谢谢你能给我这个问题的答案吗。
private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
    {

        //TODO Set / Bind the data for Category List
        // do the binding here

        //Set the Selected Index
        ctlist.SelectedIndex = 0;

        //Manually call the selection changed event to set the data for the Categroy List
        ctlist_SelectionChanged(null, null);
    }
   private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
          {
            //TODO Set / Bind the data for Category List
             ctlist.ItemsSource = lst;

              this.ctlist.SelectedIndex = 0; 
          }