我正在嘗試獲取ContextMenu的SelectedItem。ContextMenu的SelectedItem爲null
XAML
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<StackPanel>
<ListBox x:Name="MyListBox" ItemsSource="{Binding MyList}" SelectedItem="{Binding MySelectedItem}">
<ListBox.ContextMenu>
<ContextMenu ItemsSource="{Binding OCContext}" PreviewMouseDown="ContextMenu_PreviewMouseDown"/>
</ListBox.ContextMenu>
</ListBox>
<Button Content="Delete Item" Click="Button_Click"/>
</StackPanel>
</Grid>
</Window>
代碼隱藏
public partial class MainWindow : Window
{
public MainWindow()
{
OCContext = new ObservableCollection<string>();
MyList = new ObservableCollection<string>();
MyList.Add("Item 1");
MyList.Add("Item 2");
InitializeComponent();
}
public ObservableCollection<string> MyList { get; set; }
public ObservableCollection<string> OCContext { get; set; }
public string MySelectedItem { get; set; }
private void ContextMenu_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
MenuBase s = sender as MenuBase;
ItemCollection ic = s.Items;
string MyItem = "";
MyItem = (string)ic.CurrentItem;
MyList.Add(MyItem);
OCContext.Remove(MyItem);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (MySelectedItem != null)
{
OCContext.Add(MySelectedItem);
MyList.Remove(MySelectedItem);
}
}
}
你可以複製/粘貼代碼和程序應該工作。
該程序執行以下操作:
您可以選擇在列表框中的項目。如果您點擊「刪除項目」,該項目將被刪除並添加到ContextMenu中。如果您單擊ContextMenu-Item,則應將該項目再次添加到列表框並從ContextMenu中移除。你應該可以一遍又一遍......
因此,ContextMenu被綁定到一個集合。我得到的項目爲ic.CurrentItem
。 問題是,當我刪除列表框中的項目並再次添加它時(通過單擊ContextMenu上的項目),ic.CurrentItem將爲空。
爲什麼?
編輯:Cyphryx的解決方案是工作,但現在我想通過使用MVVM /綁定做同樣的:
XAML:
<ContextMenu x:Name="MyContext" ContextMenu="{Binding MyContextMenu}" ItemsSource="{Binding OCContext}"/>
視圖模型:
private ObservableCollection<string> _occontext;
public ObservableCollection<string> OCContext
{
get
{
if (_occontext == null)
_occontext = new ObservableCollection<string>();
MyContextMenu.Items.Clear();
foreach (var str in _occontext)
{
var item = new System.Windows.Controls.MenuItem();
item.Header = str;
item.Click += Content_MouseLeftButtonUp;
MyContextMenu.Items.Add(item);
}
return _occontext;
}
set
{
_occontext = value;
RaisePropertyChanged(() => OCContext);
}
}
private void Content_MouseLeftButtonUp(object sender, RoutedEventArgs e)
{
var s = sender as System.Windows.Controls.MenuItem;
if (s == null) return;
string ic = s.Header.ToString();
}
private System.Windows.Controls.ContextMenu _mycontextmenu;
public System.Windows.Controls.ContextMenu MyContextMenu
{
get
{
if (_mycontextmenu == null)
_mycontextmenu = new System.Windows.Controls.ContextMenu();
return _mycontextmenu;
}
set
{
_mycontextmenu = value;
RaisePropertyChanged(() => MyContextMenu);
}
}
Content_MouseLeftButtonUp
是不是被稱爲?..
是的,這幫了我很多! :)。我找不到除ContextMenu_PreviewMouseDown之外的其他事件(沒有看到MouseLeftButton ...)。謝謝!還要感謝您澄清爲什麼它不起作用。 – Rudi
這也可能與綁定?我真的想用MVVM做到這一點,並將其放入ViewModel中。我使用'ViewModelLocator.Main.MyValue = ic'將'var ic'分配給ViewModel。 – Rudi