我有一个ComboBox
ItemsSource
我在代码中设置为ObservableCollection<object>
。我还有两个Button
,点击后,会向ComboBoxItem
添加MenuItem
或MenuItem
及其自己的三个ObservableCollection
个MenuItem
。当我点击其中一个子ComboBox
时,我希望能够让父SelectedItem
容纳所有这些内容,以便我可以将其MenuItem
设置为父<Window x:Class="DynamicComboBoxTest.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">
<Grid Name="mainGrid">
<ComboBox x:Name="comboBox1" HorizontalAlignment="Left" Margin="12,17,0,0" VerticalAlignment="Top" Width="120" IsEditable="True"/>
<Button x:Name="comboButton" Content="combo" HorizontalAlignment="Left" Margin="132,44,0,0" VerticalAlignment="Top" Width="75" Click="comboButton_Click"/>
<Button x:Name="menuButton" Content="menu" HorizontalAlignment="Left" Margin="132,71,0,0" VerticalAlignment="Top" Width="75" Click="menuButton_Click"/>
</Grid>
1}}(带子项的那个)。
我尝试了各种方法,但没有成功。以下是我目前正在使用的内容:
XAML:
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
namespace DynamicComboBoxTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
ObservableCollection<object> comboBoxSource = new ObservableCollection<object>();
int i = 0;
int j = 0;
public MainWindow()
{
InitializeComponent();
comboBox1.ItemsSource = comboBoxSource;
}
private void MenuItem_GotFocus(object sender, RoutedEventArgs e)
{
(sender as MenuItem).IsSubmenuOpen = true;
}
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
//I want to get the parent 'ComboBox', in this case, the one named "comboBox1" from the 'MenuItem' that triggered this event.
var a = (sender as MenuItem).Parent;
var b = (a as MenuItem).Parent; //<-----this, I thought, would be the 'ComboBox' I'm trying to get, but it's null :C
}
private void comboButton_Click(object sender, RoutedEventArgs e)
{
ComboBoxItem comboBoxItem = new ComboBoxItem();
comboBoxItem.Content = i.ToString();
comboBoxSource.Add(comboBoxItem);
i++;
}
private void menuButton_Click(object sender, RoutedEventArgs e)
{
MenuItem menuItem = new MenuItem();
menuItem.Header = i.ToString();
menuItem.GotFocus += MenuItem_GotFocus;
MenuItem subMenuItem1 = new MenuItem();
subMenuItem1.Header = j.ToString();
subMenuItem1.Click += MenuItem_Click;
MenuItem subMenuItem2 = new MenuItem();
subMenuItem2.Header = (j+1).ToString();
subMenuItem2.Click += MenuItem_Click;
MenuItem subMenuItem3 = new MenuItem();
subMenuItem3.Header = (j+2).ToString();
subMenuItem3.Click += MenuItem_Click;
menuItem.Items.Add(subMenuItem1);
menuItem.Items.Add(subMenuItem2);
menuItem.Items.Add(subMenuItem3);
comboBoxSource.Add(menuItem);
i++;
j += 3;
}
}
}
C#:
MenuItem
在此示例中,我可以很好地获得子MenuItem
的父ComboBox
,但我无法获得包含父MenuItem
的{{1}}。当我试着在这一点上得到它时:
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
//I want to get the parent 'ComboBox', in this case, the one named "comboBox1" from the sub 'MenuItem' that triggered this event.
var a = (sender as MenuItem).Parent; //yay parent 'MenuItem'!
var b = (a as MenuItem).Parent; //<-----this, I thought, would be the 'ComboBox' I'm trying to get, but it's null :C
}
b
(父LogicalParent
的{{1}},MenuItem
),我原本希望它是我之后的a
,为空
让我感到困惑的一件事是,如果我将ComboBox
es / ComboBox
直接添加到MenuItem
的{{1}},就像这样:
comboBox1
一切正常,我能够遵循各种Item
,直到我得到comboBox1.Items.Add(menuItem); //assume 'menuItem' has sub 'MenuItem's
我正在寻找。如果我在XAML中添加LogicalParent
,也是如此。
这完全来自我试图开始工作的测试解决方案。在最终项目中,我无法通过简单引用(ComboBox
)直接访问MenuItem
。最终项目还将有几百个ComboBox
es,每个comboBox1
个x,我宁愿不必为每个ComboBox
引用设置变量或其他内容其父MenuItem
。出于这些原因(我确信其他人,我忘记了......)我只想回到树上并从点击的子MenuItem
中获取父ComboBox
。
我将ComboBox
的{{1}}设置为MenuItem
而非仅使用ComboBox
的原因是因为我理想地想填充所有我的ItemsSource
各种ObservableCollection
es,而不是遍历每个comboBox1.Items.Add()
并一次一个地添加数百/数千个项目,特别是因为它们都将使用相同的{{1}集合填充} S / ComboBox
秒。
希望我已经解释得这么好了。在过去的几天里,我一直在嘲笑我的大脑,所以现在它非常好吃&gt; _&lt;。如果我遗漏任何细节或某些事情没有意义,我会道歉。这是我第一次涉足WPF,如此简单而详细的解释将不胜感激!提前致谢! :d
PS:请忽略ComboBox
s永不关闭的事实。那会晚些时候:P。此外,任何想法为什么子ComboBoxItem
是白色的? (我怀疑它与MenuItem
与Submenu
废话或MenuItem
事件有关)