2016-03-21 17 views
0

我目前正在使用Windows 10,並使用ListView根據我的要求顯示項目。現在,我想在用戶單擊ListView項目時向同一頁面展開ListView,併爲他提供一些選項以簡化用戶體驗。如何擴展Windows 10中SelectionChanged事件的ListView項目?

我知道如何使用DataTemplate實現ListView和顯示項目,但我不確定是否可以使用ListView實現我的需求。

我想實現的東西象下面這樣:

enter image description here

我要像顯示聯繫人選項,添加照片,等上ListView項點擊。我也嘗試使用PopupMenu來實現相同的功能,但它具有添加多達6個命令的限制,並且我擁有比此更多的命令。我想動態添加這些選項。

+0

我真的不明白你的需要。但我會嘗試: 當用戶點擊按鈕時,使用按鈕和彈出顯示帶有預定義內容的彈出窗口? – thang2410199

回答

1

下面是你想要的基本實現。

使用此ListView的XAML:

<ListView SelectionChanged="ListView_SelectionChanged"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 

       <Panel.Resources> 
        <Style x:Name="MainAreaStyle" TargetType="Image"> 
         <Setter Property="Width" Value="300" /> 
         <Setter Property="Margin" Value="0" /> 
        </Style> 
        <Style x:Name="DetailAreaStyle" TargetType="Rectangle"> 
         <Setter Property="Height" Value="150" /> 
         <Setter Property="Height" Value="300" /> 
         <Setter Property="Margin" Value="0,0,0,8" /> 
         <Setter Property="Visibility" Value="Collapsed" /> 
        </Style> 
       </Panel.Resources> 

       <Image Source="http://i.stack.imgur.com/L5sb9.png" Style="{StaticResource MainAreaStyle}" /> 
       <Rectangle x:Name="DetailArea" Fill="DarkBlue" Style="{StaticResource DetailAreaStyle}" /> 

      </StackPanel> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
    <x:String>One</x:String> 
    <x:String>Two</x:String> 
    <x:String>Three</x:String> 
</ListView> 

使用此代碼隱藏:

UIElement previous = null; 
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    if (previous != null) previous.Visibility = Visibility.Collapsed; 
    if (e.AddedItems.Any()) 
    { 
     var container = (sender as ListView).ContainerFromItem(e.AddedItems.First()); 
     (previous = Child<Rectangle>(container, "DetailArea")).Visibility = Visibility.Visible; 
    } 
} 

public T Child<T>(DependencyObject parent, string name) where T : FrameworkElement 
{ 
    return Children(parent).OfType<T>().FirstOrDefault(x => x.Name == name); 
} 

public List<DependencyObject> Children(DependencyObject parent) 
{ 
    var list = new List<DependencyObject>(); 
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) 
    { 
     var child = VisualTreeHelper.GetChild(parent, i) as DependencyObject; 
     if (child != null) 
     { 
      list.Add(child); 
      list.AddRange(Children(child)); 
     } 
    } 
    return list; 
} 

是這樣的:

enter image description here

有意義嗎?當然還有更多事情要做。但是這應該讓你開始。

祝你好運。

+0

非常感謝。它真的有效。您可以建議一些API在下載圖像後更改圖像像素顏色而不會導致性能問題? –

相關問題