2016-01-29 111 views
2

我正在開發一個Windows Store應用程序,其中我有一個嵌套的listview,即可展開的listview。要保留孩子的列表視圖中選擇的項目我已經使用來自嵌套列表視圖的SelectedItems

<ListView Name="FiltersListview" ItemContainerStyle="{StaticResource StretchItemStyle}" SelectionChanged="FiltersListview_SelectionChanged" IsItemClickEnabled="True" ItemClick="FiltersListview_ItemClick" Grid.Row="1" Grid.ColumnSpan="2"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <Grid> 
       <Interactivity:Interaction.Behaviors> 
        <Core:DataTriggerBehavior Binding="{Binding IsSelected}" Value="True"> 
         <Core:ChangePropertyAction TargetObject="{Binding ElementName=view1}" PropertyName="Visibility" Value="Collapsed"/> 
         <Core:ChangePropertyAction TargetObject="{Binding ElementName=view2}" PropertyName="Visibility" Value="Visible"/> 
        </Core:DataTriggerBehavior> 
        <Core:DataTriggerBehavior Binding="{Binding IsSelected}" Value="False"> 
        <Core:ChangePropertyAction TargetObject="{Binding ElementName=view1}" PropertyName="Visibility" Value="Visible"/> 
         <Core:ChangePropertyAction TargetObject="{Binding ElementName=view2}" PropertyName="Visibility" Value="Collapsed"/> 
        </Core:DataTriggerBehavior> 
       </Interactivity:Interaction.Behaviors> 
       <Grid Height="50" x:Name="view1"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="3*"/> 
         <ColumnDefinition Width="*"/> 
        </Grid.ColumnDefinitions> 
        <TextBlock Text="{Binding Name}" VerticalAlignment="Center" Style="{StaticResource PageTextStyle}"/> 
        <Image Margin="10" Grid.Column="1" Source="/Images/arrow-down.png"/> 
        <Rectangle Fill="Black" VerticalAlignment="Bottom" Height="1" Grid.ColumnSpan="2"/> 
       </Grid> 
       <Grid x:Name="view2"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="3*"/> 
         <ColumnDefinition Width="*"/> 
        </Grid.ColumnDefinitions> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="50"/> 
         <RowDefinition/> 
        </Grid.RowDefinitions> 
        <TextBlock Text="{Binding Name}" VerticalAlignment="Center" Style="{StaticResource PageTextStyle}"/> 
        <Image Margin="10" Grid.Column="1" Source="/Images/arrow-up.png"/> 
        <ListView Margin="20,0" Grid.Row="1" RequestedTheme="Light" Grid.ColumnSpan="2" ItemsSource="{Binding SubList}" SelectionMode="Multiple"> 
         <ListView.ItemContainerStyle> 
          <Style TargetType="ListViewItem"> 
           <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
           <Setter Property="FontSize" Value="22"/> 
           <Setter Property="VerticalContentAlignment" Value="Center"/> 
           <Setter Property="Foreground" Value="Black"/> 
          </Style> 
         </ListView.ItemContainerStyle> 
        </ListView> 
       </Grid> 
      </Grid> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

,並在後面的代碼

private void FiltersListview_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    try 
    { 
     var item = (sender as ListView).SelectedItem as FilterM; 
     if (item != null) 
      item.IsSelected = true; 
     if (e.RemovedItems.Count > 0) 
     { 
      foreach (var Nitem in e.RemovedItems) 
      { 
       (Nitem as FilterM).IsSelected = false; 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     System.Diagnostics.Debug.WriteLine(ex.Message); 
    } 
} 

private void FiltersListview_ItemClick(object sender, ItemClickEventArgs e) 
{ 
    try 
    { 
     if ((sender as ListView).SelectedItem != null) 
     { 
      if ((sender as ListView).SelectedItem.Equals(e.ClickedItem)) 
       (sender as ListView).SelectedItem = null; 
      else 
       (sender as ListView).SelectedItem = e.ClickedItem; 
     } 
     else 
      (sender as ListView).SelectedItem = e.ClickedItem; 
    } 
    catch (Exception ex) 
    { 
     System.Diagnostics.Debug.WriteLine(ex.Message); 
    } 
} 

我已經改變了這個問題,我對此from here. 老問題後,但現在上的一個按鈕的自來水命名去我想知道每個項目的子列表視圖中的所有選定項目。我已經偶然發現了鏈接like this,但我沒有使用MVVM,無法使它適用於我的情況。

任何人都可以幫忙嗎?

回答

1

嗯,我想出瞭解決這個問題的方法。如果有人需要,我會提供我的解決方案。

我已經將子列表視圖作爲參數傳遞給ViewModel並通過命令進行管理。

<ListView ItemContainerStyle="{StaticResource StretchItemStyle}" SelectionChanged="FiltersListview_SelectionChanged" IsItemClickEnabled="True" ItemClick="FiltersListview_ItemClick" Grid.Row="1" Grid.ColumnSpan="2" Margin="-1,0,1,44" Grid.RowSpan="2"> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
       <Grid> 
        <Interactivity:Interaction.Behaviors> 
         <Core:DataTriggerBehavior Binding="{Binding IsSelected}" Value="True"> 
          <Core:ChangePropertyAction TargetObject="{Binding ElementName=view1}" PropertyName="Visibility" Value="Collapsed"/> 
          <Core:ChangePropertyAction TargetObject="{Binding ElementName=view2}" PropertyName="Visibility" Value="Visible"/> 
         </Core:DataTriggerBehavior> 
         <Core:DataTriggerBehavior Binding="{Binding IsSelected}" Value="False"> 
          <Core:ChangePropertyAction TargetObject="{Binding ElementName=view1}" PropertyName="Visibility" Value="Visible"/> 
          <Core:ChangePropertyAction TargetObject="{Binding ElementName=view2}" PropertyName="Visibility" Value="Collapsed"/> 
         </Core:DataTriggerBehavior> 
        </Interactivity:Interaction.Behaviors> 
        <Grid Height="50" x:Name="view1"> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="3*"/> 
          <ColumnDefinition Width="*"/> 
         </Grid.ColumnDefinitions> 
         <TextBlock Text="{Binding Name}" VerticalAlignment="Center" Style="{StaticResource PageTextStyle}"/> 
         <Image Margin="10" Grid.Column="1" Source="/Images/arrow-down.png"/> 
         <Rectangle Fill="Black" VerticalAlignment="Bottom" Height="1" Grid.ColumnSpan="2"/> 
        </Grid> 
        <Grid x:Name="view2"> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="3*"/> 
          <ColumnDefinition Width="*"/> 
         </Grid.ColumnDefinitions> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="50"/> 
          <RowDefinition/> 
         </Grid.RowDefinitions> 
         <TextBlock Text="{Binding Name}" VerticalAlignment="Center" Style="{StaticResource PageTextStyle}"/> 
         <Image Margin="10" Grid.Column="1" Source="/Images/arrow-up.png"/> 
         <ListView x:Name="SubListview" Margin="20,0" Grid.Row="1" RequestedTheme="Light" Grid.ColumnSpan="2" ItemsSource="{Binding SubList}" SelectionMode="Multiple"> 
          <ListView.ItemContainerStyle> 
           <Style TargetType="ListViewItem"> 
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
            <Setter Property="FontSize" Value="22"/> 
            <Setter Property="VerticalContentAlignment" Value="Center"/> 
            <Setter Property="Foreground" Value="Black"/> 
           </Style> 
          </ListView.ItemContainerStyle> 
          <Interactivity:Interaction.Behaviors> 
           <Core:EventTriggerBehavior EventName="SelectionChanged"> 
            <Core:InvokeCommandAction Command="{Binding SelectionChangedCommand}" CommandParameter="{Binding ElementName=SubListview}"/> 
            <!--<Core:InvokeCommandAction Command="{Binding SelectionChangedCommand}" InputConverter="{StaticResource SelectionChangedConverter}" InputConverterParameter="{Binding ElementName=SubListview}"/>--> 
           </Core:EventTriggerBehavior> 
          </Interactivity:Interaction.Behaviors> 
         </ListView> 
         <Rectangle Grid.Row="1" Fill="Black" VerticalAlignment="Bottom" Height="1" Grid.ColumnSpan="2"/> 
        </Grid> 
       </Grid> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 

,並在後面

public class FilterM 
{ 
public RelayCommand<IList<object>> SelectionChangedCommand 
{ 
    get 
    { 
     if (selectionChangedCommand == null) 
     { 
      selectionChangedCommand = new RelayCommand<IList<object>>(
       items => 
       { 
        // do something with selected items! 
       } 
      ); 
     } 

     return selectionChangedCommand; 
    } 
} 
} 

代碼,並設置一列表視圖中的ItemsSource。我有這個想法from this link.希望它能幫助像我這樣的人。