2011-08-05 118 views
0

我正在使用ListView來顯示我的應用程序中的日誌內容。我想根據用戶當前在ListView中選擇的條目更改上下文MenuItem的圖標和可見性。WPF綁定到ListView SelectedItem不工作

這裏是我如何填充ListView控件:

// Create the collection view source. 
m_CollectionViewSource = new CollectionViewSource();     
m_CollectionViewSource.SortDescriptions.Add(new System.ComponentModel.SortDescription("Time", System.ComponentModel.ListSortDirection.Descending)); 
m_CollectionViewSource.Filter += new FilterEventHandler(LogEventCollectionViewSource_Filter); 

// Create the binding. 
Binding binding = new Binding(); 
binding.Source = m_CollectionViewSource; 
this.LogEventListView.SetBinding(ListView.ItemsSourceProperty, binding); 
m_CollectionViewSource.Source = LogEventManager.Instance.LogEventCollection; 

而且這裏是我創造我的ListView控件。

<ListView x:Name="LogEventListView" 
      Grid.Row="0" 
      Grid.Column="0" 
      SelectionMode="Single" 
      VirtualizingStackPanel.IsVirtualizing="True"> 
    <ListView.ContextMenu> 
     <ContextMenu Opened="ContextMenu_Opened"> 
      <MenuItem x:Name="ContextMenuViewDetails" 
         Header="View Details..." 
         ToolTip="Shows all of the data associated with the log event message." 
         Visibility="{Binding ElementName=LogEventListView, Path=SelectedItem, Converter={StaticResource NullToVisibilityConverter}}"> 
       <MenuItem.Icon> 
        <Image MaxHeight="16" MaxWidth="16" Source="{Binding ElementName=LogEventListView, Path=SelectedItem.Category, Converter={StaticResource LogEventCategoryConverter}, ConverterParameter='Small'}" /> 
       </MenuItem.Icon> 
      </MenuItem> 

一切工作正常,除了第一個菜單項的綁定。當一個項目沒有被選中時,我想讓第一個菜單項的可見性被摺疊。我還希望上下文菜單項圖像匹配選定的日誌事件。我已經證實,我的IValueConverter類都是正常工作的。出於某種原因,第一個MenuItem始終可見並且永遠不會有圖標。有人能告訴我我忽略了什麼嗎?

UPDATE: 似乎有一些真正的問題有如圖herehere在.net 3.5綁定到一個菜單項的圖標屬性。問題由於我使用IValueConverter來選擇合適的圖像這一事實而變得更加複雜。雖然它是而不是我更喜歡這個解決方案,因爲現在我已經決定在ContextMenu的開放事件中設置代碼隱藏的值。

ContextMenu menu = sender as ContextMenu; 

if (menu != null) 
{ 
     MenuItem item = LogicalTreeHelper.FindLogicalNode(menu, "ContextMenuViewDetails") as MenuItem; 

     if (item != null) 
     { 
      if (this.LogEventListView.SelectedItems.Count <= 0) 
       item.Visibility = Visibility.Collapsed; 
      else 
       item.Visibility = Visibility.Visible; 
      } 
     } 
} 
+0

請發佈您的轉換器。你已經驗證了轉換器被調用? – Paparazzi

+0

我已驗證轉換器正在工作。我在其他地方使用過它們。出於某種原因,他們不會從這個控制中被調用。 – bporter

回答

0

編輯:它再看看後,這實際上可能是低於原來的答覆描述here

問題的例子,但可能不會工作

在沒有看到我變流器不能評論爲什麼它可能不工作,但你可以嘗試實現相同的風格,而不是:

<ContextMenu.Style> 
    <Style> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding SelectedItem, ElementName=LogEventListView} Value="{x:Null}"> 
       <Setter Property="Visibility" Value="Collapsed"/> 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 
</ContextMenu.Style> 
+0

感謝您的建議。不幸的是,我無法讓樣式起作用。綁定MenuItem屬性似乎存在實際問題,特別是Icon屬性。 – bporter

0
<Image MaxHeight="16" MaxWidth="16" 
Source="{Binding ElementName=LogEventListView, 
Path=SelectedItem.Category, 
Converter={StaticResource LogEventCategoryConverter}, ConverterParameter='Small'}"/> 

我似乎無法找到解決的SelectedItem稱爲類別附加屬性?

+0

類別是綁定到ListView的ObservableCollection <>中項目的屬性。 – bporter