WPF组合框的奇怪行为

WPF组合框的奇怪行为

本文介绍了WPF组合框的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 好吧,我智慧结束了。我正在使用WPF和C#。我有一个应用程序(一个简单的支票帐户,簿记应用程序)。我有3个组合框(cboBroker,cboCatagory和cboSubCatagory)和3个列表框(lstBroker,lstCatagory和lstSubCatagory)。基本上,在组合框上的DropDownClosed事件处理程序中,ComboBox.Text应该发送到相应的列表框并显示在相应的列表框中。这适用于cboBroker和cboSubCatagory但在cboCatagory的情况下我遇到了非常奇怪的行为。 例如,如果我加载代理一切都很好; cboBroker中的SelectedItem被添加到lstBroker。接下来,如果我加载类别,类别。文本将为,而lstCatagory将收到一个空行。我想这是有道理的,除了显然cboCatagory中的SelectedItem没有被发送到cboCatagory.Text。现在,如果我加载子类别,一切都很好,SubCatagory.Text被添加到lstSubCatagory。好的,现在我回到cboCategory,当DropDownClosed被触发低时,看到 cboSubCatagory 中的SelectedItem出现在 lstCatagory 的第二行。这里发生了什么?在过去的三天里,我尝试了很多方法来解决这个问题,但显然WPF认为这是应该发生的行为。路由事件会发生什么事情,如果是这样,为什么不会出现其他两个组合框和列表框? 我附上了我认为相关的内容下面是XAML代码和C#代码: XAML: < 窗口 x:名称 = winTestScrolling x:Class = TestListboxScrolling.MainWindow xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x = http://schemas.microsoft.com/winfx/ 2006 / xaml 标题 = 测试滚动 高度 = 450 宽度 = 800 > < 网格 > < Grid.RowDefinitions > < RowDefinition / > < RowDefinition / > < / Grid.RowDefinitions > < Grid.ColumnDefinitions > < ColumnDefinition / > < ColumnDefinition / > < / Grid.ColumnDefinitions > < WrapPanel x:名称 = stkListBoxes 宽度 = 自动 MaxHeight = 120 Grid.Row = 0 保证金 = 30 ,10,30,10 方向 = 水平 已删除 = LightGreen Grid.Column = 1 > < ListBox x:名称 = lstBroker 宽度 = 125 高度 = 自动 MinHeight = 50 MaxHeight = 100 保证金 = 100,-110,0,5 ClipToBounds = True / > < ListBox x:名称 = lstCatagory 宽度 = 125 高度 = 自动 MinHeight = 50 MaxHeight = 100 保证金 = 5,-110,0,5 ClipToBounds = True / > < ListBox x:名称 = lstSubCatagory 宽度 = 125 高度 = 自动 MinHeight = 50 MaxHeight = 100 保证金 = 5,-110,0,5 ClipToBounds = True / > < / WrapPanel > < WrapPanel x:名称 = stkComboBoxes 宽度 = 自动 高度 = 自动 Grid.Row = 1 Grid.Column = 1 保证金 = 29.6,4.6,30.4 ,15 方向 = 水平 > < ComboBox x:名称 = cboBroker 宽度 = 125 高度 = 自动 MinHeight = 20 保证金 = 100,-85,0,5 DropDownClosed = cboBroker_DropDownClosed / > < ComboBox x:名称 = cboCatagory 宽度 = 125 高度 = 自动 MinHeight = 20 保证金 = 2,-85,0,5 DropDownClosed = cboCatagory_DropDownClosed / > < ComboBox x:名称 = cboSubCatagory 宽度 = 125 高度 = 自动 MinHeight = 20 保证金 = 2,-85,0,0 DropDownClosed = cboSubCatagory_DropDownClosed / > < / WrapPanel > < / Grid > < / Window > 代码背后: namespace TestListboxScrolling { public partial class MainWindow:Window { private void cboBroker_DropDownClosed( object sender,EventArgs e) { Broker = cboBroker.Text; lstBroker.Items.Add(Broker); } private void cboCatagory_DropDownClosed( object sender,EventArgs e) { Catagory = cboCatagory.Text; lstCatagory.Items.Add(Catagory); } private void cboSubCatagory_DropDownClosed( object sender,EventArgs e) { SubCatagory = cboSubCatagory.Text; lstSubCatagory.Items.Add(SubCatagory); } } } 解决方案 我不得不删除'removed =lightgreen '从网格开始,使网格更大,以便可以正确查看,但这段代码,基于你的代码在VS 2012下完美运行。你确实将 Catagory 声明为字符串? public MainWindow() { InitializeComponent(); cboBroker.Items.Add( Broker1); cboBroker.Items.Add( Broker2); cboBroker.Items.Add( Broker3); cboCatagory.Items.Add( Cat1); cboCatagory.Items.Add( Cat2); cboCatagory.Items.Add( Cat3); cboSubCatagory.Items.Add( Sub1); cboSubCatagory.Items.Add( Sub2); cboSubCatagory.Items.Add( Sub3); } string 经纪人; string Catagory; string SubCatagory; private void cboBroker_DropDownClosed( object sender,EventArgs e) { Broker = cboBroker.Text; lstBroker.Items.Add(Broker); } private void cboCatagory_DropDownClosed( object sender,EventArgs e) { Catagory = cboCatagory.Text; lstCatagory.Items.Add(Catagory); } private void cboSubCatagory_DropDownClosed( object sender,EventArgs e) { SubCatagory = cboSubCatagory.Text; lstSubCatagory.Items.Add(SubCatagory); } 好吧,注释掉的内联方法和LoadComboBox都适用于我。唯一的区别是我使用SqlConnectionStringBuilder()这无关紧要。只要您的组合框在您点击它时显示字符串它就应该正常工作。 尝试查找有问题的组合框的每个参考,并确保没有任何内容当你没想到的时候清理它。 我的测试表只有经纪人nchar(100) 公共部分类MainWindow:Window { public MainWindow() { InitializeComponent(); 尝试 { / * cboBroker.Items.Add(Broker1); cboBroker.Items.Add(Broker2); cboBroker.Items.Add(Broker3); * * / / * SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); builder [Data Source] =HY201486 \\Express2012; builder [integrated Security] = true; builder [Initial Catalog] =ajrtest; SqlConnection con = new SqlConnection(builder.ConnectionString); con.Open(); SqlCommand cmd = new SqlCommand(select * from Broker order by broker,con); SqlDataReader reader = cmd.ExecuteReader(); while(reader.Read()) { cboBroker.Items.Add(reader.GetString(0)); } con.Close(); * * / LoadComboBox(按经纪人的经纪人订单选择*,cboBroker); cboCatagory.Items.Add(Cat1); cboCatagory.Items.Add(Cat2); cboCatagory.Items.Add(Cat3); cboSubCatagory.Items.Add(Sub1); cboSubCatagory.Items.Add(Sub2); cboSubCatagory.Items.Add(Sub3); } catch(exception ex) { MessageBox.Show(Error:+ ex.Message); } } string Broker; string Catagory; string SubCatagory; private void LoadComboBox(string sql,ComboBox cboBox) { try { SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); builder [Data Source] =HY201486 \\Express2012; builder [integrated Security] = true; builder [Initial Catalog] =ajrtest; SqlConnection con = new SqlConnection(builder.ConnectionString); con.Open(); SqlCommand cmd = new SqlCommand(sql,con); SqlDataReader reader = cmd.ExecuteReader(); while(reader.Read()) { cboBox.Items.Add(reader.GetString(0)); } con.Close(); } catch(exception ex) { MessageBox.Show(Error:+ ex.Message); } } 首先,感谢arussel的有用评论。我发现并且现在能够重现我在WPF组合框中遇到的集市行为。这一切都归结为在创建新UI时大量使用剪切和粘贴操作。基本上,在创建cboCatagory时,我复制并粘贴了cboSubCatagory的XAML代码,然后在适当的位置将SubCategory更改为Category。此操作还显然为cboCatagory创建了一个DropDownClosed事件和相应的DropDownClosed事件处理程序。在创建后面的代码时,我必须再次复制并粘贴DropDownClosed事件处理程序的第二个副本,即cboCatagory_DropDownClosed_1。不知何故,这第二个副本包含令人反感的声明: 类别= cboSubCategory.Text; 附加代码(后面的XAML和C#代码)重现了我观察到的集市行为。再次感谢您的投入,它引导了正确的方向,并鼓励我跟进以了解正在发生的事情。 我实际开发了这个旁边应用程序来调查另一个问题; WPF列表框的滚动行为(这样我花了最近10天试图让它工作)。我将在另一个问题上提出我的滚动问题。 ttyler XAML: < window x:class = ComboBoxTester.MainWindow xmlns:x = #unknown > xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x =http://schemas.microsoft .com / winfx / 2006 / xaml Title =ReconstructHeight =450Width =800> < grid x:name = grdContent horizo​​ntalalignment = 左 背景 = AliceBlue margin = 10 > < grid.rowdefinitions > < rowdefinition height = 自动 minheight = 150 / > < rowdefinition height = * / > < / grid.rowdefinitions > < grid.columndefinitions > < columndefinition width = 150 / > < columndefinition width = 640 / > < / grid.columndefinitions > < stackpanel x:name = stkButtons width = 自动 height = 自动 边距 = 5,5,5,5 > < 按钮 x:name =btnReturn\" content=\"Return\" horizontalalignment=\"Left\" verticalalignment=\"Top\"> MinWidth=\"75\" Height=\"35\" Margin=\"5,10,0,5\" Click=\"btnReturn_Click\"/> </button></stackpanel> <wrappanel x:name=\"wrpListBoxes\" width=\"Auto\" maxheight=\"120\" grid.row=\"0\" grid.column=\"1\"> Margin=\"30,10,30,10\" Orientation=\"Horizontal\" Background=\"AliceBlue\"> <listbox x:name=\"lstBroker\" width=\"125\" height=\"Auto\" minheight=\"50\"> Margin=\"100,10,0,5\" HorizontalAlignment=\"Center\" VerticalAlignment=\"Center\" ScrollViewer.HorizontalScrollBarVisibility=\"Hidden\" ScrollViewer.VerticalScrollBarVisi bility=\"Hidden\" Background=\"LightYellow\"/> <listbox x:name=\"lstCatagory\" width=\"125\" height=\"Auto\" minheight=\"50\"> Margin=\"5,10,0,5\" HorizontalAlignment=\"Center\" VerticalAlignment=\"Center\" ScrollViewer.HorizontalScrollBarVisibility=\"Hidden\" ScrollViewer.VerticalScrollBarVisib ility=\"Hidden\" Background=\"LightYellow\"/> <listbox x:name=\"lstSubCatagory\" width=\"125\" height=\"Auto\" minheight=\"50\"> Margin=\"5,10,0,5\" HorizontalAlignment=\"Center\" VerticalAlignment=\"Center\" ScrollViewer.HorizontalScrollBarVisibility=\"Hidden\" ScrollViewer.VerticalScrollBarVis ibility=\"Hidden\" Background=\"LightYellow\"/> </listbox></listbox></listbox></wrappanel> <wrappanel x:name=\"wrpComboBoxes\" width=\"Auto\" height =\"Auto\" grid.row=\"1\" grid.column=\"1\"> Margin=\"30,5,30,10\" Orientation=\"Horizontal\" Background=\"AliceBlue\" > <combobox x:name=\"cboBroker\" width=\"125\" height=\"20\" margin=\"100,10,0,0\"> HorizontalAlignment=\"center\" VerticalAlignment=\"Center\" Background=\"PaleGreen\" ItemsSource=\"{Binding Path=EntryList}\" DisplayMemberPath=\"Broker\" DropDownClosed=\"c boBroker_DropDownClosed\" /> <combobox x:name=\"cboCatagory\" width=\"125\" height=\"20\" margin=\"5,10,0,0\"> HorizontalAlignment=\"Left\" VerticalAlignment=\"Top\" Background=\"PaleGreen\" ItemsSource=\"{Binding Path=EntryList}\" DisplayMemberPath=\"Catagory\" DropDownClosed=\"cboC atagory_DropDownClosed_1\" /> <combobox x:name=\"cboSubCatagory\" width=\"125\" height=\"20\" margin=\"5,10,0,0\"> HorizontalAlignment=\"Left\" VerticalAlignment=\"Top\" Background=\"PaleGreen\" ItemsSource=\"{Binding Path=EntryList}\" DisplayMemberPath=\"SubCatagory\" DropDownClosed =\"cboSubCatagory_DropDownClosed\" /> </combobox></combobox></combobox></wrappanel> </grid> </window> Code behind: using System; 使用 System.Collections.Generic; 使用 System.Windows; 使用 System.Windows.Controls; namespace ComboBoxTester { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { string Broker; string Catagory; string SubCatagory; int i = 0 ; string Num; 交易事务= 新 Transactions(); List<transactions> EntryList = new List<transactions>(); public MainWindow() { InitializeComponent(); // Create a list of Brokers, Catagories and SubCatagories to add to comboboxes for(i= 1; i<=10; i++) { Num = i.ToString(); Broker = \"Broker\" + Num; Catagory = Catagory + Num; SubCatagory = \"SubCatagory\" + Num; EntryList.Add(new Transactions() { Broker = Broker, Catagory = Catagory, SubCatagory = SubCatagory }); } cboBroker.ItemsSource = EntryList; cboCatagory.ItemsSource = EntryList; cboSubCatagory.ItemsSource = EntryList; } private void btnReturn_Click(object sender, RoutedEventArgs e) { Application.Current.Shutdown(); } private void cboBroker_DropDownClosed(object sender, EventArgs e) { Broker = cboBroker.Text; lstBroker.Items.Add(Broker); } private void cboCatagory_DropDownClosed( object sender,EventArgs e) { Catagory = cboCatagory.Text; lstCatagory.Items.Add(Catagory); } private void cboSubCatagory_DropDownClosed(object sender, EventArgs e) { SubCatagory = cboSubCatagory.Text; lstSubCatagory.Items.Add(SubCatagory); } private void cboCatagory_DropDownClosed_1(object sender, EventArgs e) { Catagory = cboSubCatagory.Text; lstCatagory.Items.Add(Catagory); } // Create a Transaction type to add to List<transaction> public class Transactions { public string Broker { get; set ; } public string Catagory { get; set ; } public string SubCatagory { get; set ; } } } } </transaction></transactions></transactions>All right, I.m at wits end. I’m working with WPF and C#. I have an application (a simple checking account, book keeping application). I have 3 comboboxes (cboBroker, cboCatagory and cboSubCatagory) and 3 listboxes (lstBroker, lstCatagory and lstSubCatagory). Essentially, upon the "DropDownClosed" event handler on the comboboxes, the "ComboBox.Text" should be sent to and appear in the respective listbox. This works for cboBroker and cboSubCatagory but I encounter very weird behavior in the case of cboCatagory.For instance, if I load the broker all is well; the SelectedItem in cboBroker is added to lstBroker. Next, if I load category, the Category. Text will be " " and lstCatagory will receive a blank line. I guess that makes sense except that apparently the SelectedItem in cboCatagory was not sent to cboCatagory.Text. Now, if I load the subcategory, again all is well, SubCatagory.Text is added to lstSubCatagory. Ok, now I go back to cboCategory and when when "DropDownClosed" is triggered low and behold the SelectedItem in cboSubCatagory appears in the second row of lstCatagory. What’s going on here? Over the past three days I’ve tried numerous approaches to resolve this issue but apparently WPF believes this is the behavior that should occur. Is something going on with routed events and if so why doesn’t it occur with the other two comboboxes and listboxes?I’ve attached what I believe is the pertainent XAML code and C# code behind below:XAML:<Window x:Name="winTestScrolling" x:Class="TestListboxScrolling.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Test Scrolling" Height="450" Width="800" > <Grid > <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <WrapPanel x:Name="stkListBoxes" Width="Auto" MaxHeight="120"Grid.Row="0" Margin="30,10,30,10" Orientation="Horizontal" removed="LightGreen" Grid.Column="1"> <ListBox x:Name="lstBroker" Width="125" Height="Auto" MinHeight="50" MaxHeight="100"Margin="100,-110,0,5" ClipToBounds="True" /> <ListBox x:Name="lstCatagory" Width="125" Height="Auto" MinHeight="50" MaxHeight="100" Margin="5,-110,0,5" ClipToBounds="True"/> <ListBox x:Name="lstSubCatagory" Width="125" Height="Auto" MinHeight="50" MaxHeight="100" Margin="5,-110,0,5" ClipToBounds="True"/> </WrapPanel> <WrapPanel x:Name="stkComboBoxes" Width="Auto" Height="Auto" Grid.Row="1" Grid.Column="1" Margin="29.6,4.6,30.4,15" Orientation="Horizontal"><ComboBox x:Name="cboBroker" Width="125" Height="Auto" MinHeight="20"Margin="100,-85,0,5" DropDownClosed="cboBroker_DropDownClosed" /><ComboBox x:Name="cboCatagory" Width="125" Height="Auto" MinHeight="20"Margin="2,-85,0,5" DropDownClosed="cboCatagory_DropDownClosed"/><ComboBox x:Name="cboSubCatagory" Width="125" Height="Auto" MinHeight="20"Margin="2,-85,0,0" DropDownClosed="cboSubCatagory_DropDownClosed" /> </WrapPanel> </Grid></Window>Code Behind:namespace TestListboxScrolling{ public partial class MainWindow : Window{ private void cboBroker_DropDownClosed(object sender, EventArgs e) { Broker = cboBroker.Text; lstBroker.Items.Add(Broker); } private void cboCatagory_DropDownClosed(object sender, EventArgs e) { Catagory = cboCatagory.Text; lstCatagory.Items.Add(Catagory); } private void cboSubCatagory_DropDownClosed(object sender, EventArgs e) { SubCatagory = cboSubCatagory.Text; lstSubCatagory.Items.Add(SubCatagory); } }} 解决方案 I had to delete the 'removed="lightgreen"' from the grid and make the grid larger so it could be viewed correctly, but this code, based on your code works perfectly under VS 2012. You did declare Catagory as a string?public MainWindow() { InitializeComponent(); cboBroker.Items.Add("Broker1"); cboBroker.Items.Add("Broker2"); cboBroker.Items.Add("Broker3"); cboCatagory.Items.Add("Cat1"); cboCatagory.Items.Add("Cat2"); cboCatagory.Items.Add("Cat3"); cboSubCatagory.Items.Add("Sub1"); cboSubCatagory.Items.Add("Sub2"); cboSubCatagory.Items.Add("Sub3"); } string Broker; string Catagory; string SubCatagory; private void cboBroker_DropDownClosed(object sender, EventArgs e) { Broker = cboBroker.Text; lstBroker.Items.Add(Broker); } private void cboCatagory_DropDownClosed(object sender, EventArgs e) { Catagory = cboCatagory.Text; lstCatagory.Items.Add(Catagory); } private void cboSubCatagory_DropDownClosed(object sender, EventArgs e) { SubCatagory = cboSubCatagory.Text; lstSubCatagory.Items.Add(SubCatagory); }Well, both the inline method that is commented out, and the LoadComboBox work for me. Only difference is I use SqlConnectionStringBuilder() which shouldn't matter. As long as your combobox displays the string when you click on it it should be working.Try looking for every reference of the combobox in question, and make sure nothing is clearing it when you don't expect.My test table just has brokers as nchar(100)public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); try { /* cboBroker.Items.Add("Broker1"); cboBroker.Items.Add("Broker2"); cboBroker.Items.Add("Broker3"); * */ /* SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); builder["Data Source"] = "HY201486\\Express2012"; builder["integrated Security"] = true; builder["Initial Catalog"] = "ajrtest"; SqlConnection con = new SqlConnection(builder.ConnectionString); con.Open(); SqlCommand cmd = new SqlCommand("select * from Broker order by broker", con); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { cboBroker.Items.Add(reader.GetString(0)); } con.Close(); * */ LoadComboBox("select * from Broker order by broker", cboBroker); cboCatagory.Items.Add("Cat1"); cboCatagory.Items.Add("Cat2"); cboCatagory.Items.Add("Cat3"); cboSubCatagory.Items.Add("Sub1"); cboSubCatagory.Items.Add("Sub2"); cboSubCatagory.Items.Add("Sub3"); } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); } } string Broker; string Catagory; string SubCatagory; private void LoadComboBox(string sql, ComboBox cboBox) { try { SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); builder["Data Source"] = "HY201486\\Express2012"; builder["integrated Security"] = true; builder["Initial Catalog"] = "ajrtest"; SqlConnection con = new SqlConnection(builder.ConnectionString); con.Open(); SqlCommand cmd = new SqlCommand(sql, con); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { cboBox.Items.Add(reader.GetString(0)); } con.Close(); } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); } }First, thank you arussel for your helpful comments. I have found and am now able to recreate the bazaar behavior I experienced with the WPF comboboxes. It all boils down to sloppy use of cut and paste operations in creating a new UI. Essentially, in creating cboCatagory I copied and pasted the XAML code for cboSubCatagory and then changed SubCategory to Category in the appropriate places. This action also apparently created a "DropDownClosed" event and corresponding "DropDownClosed" event handler for cboCatagory. In creating the code behind, again I must have copied and pasted a second copy of the "DropDownClosed" event handler, i.e. cboCatagory_DropDownClosed_1. Somehow this 2nd copy contained the objectionable statement: Category = cboSubCategory.Text;The attached code (XAML and C# code behind) recreates the bazaar behavior I was observing. Again, thank you for your input it lead ne in the right direction and encouraged me to follow through to find what was going on. I actually developed this "on the side" application to investigate another problem; the scrolling behavior of the WPF listbox (in so doing I spent the last 10 days trying to get it to work). I will pose my scrolling problem in another question.ttylerXAML:<window x:class="ComboBoxTester.MainWindow" xmlns:x="#unknown"> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Reconstruct" Height="450" Width="800"> <grid x:name="grdContent" horizontalalignment="Left" background="AliceBlue" margin="10"> <grid.rowdefinitions> <rowdefinition height="Auto" minheight="150" /> <rowdefinition height="*" /> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition width="150" /> <columndefinition width="640" /> </grid.columndefinitions> <stackpanel x:name="stkButtons" width="Auto" height="Auto" margin="5,5,5,5"> <button x:name="btnReturn" content="Return" horizontalalignment="Left" verticalalignment="Top"> MinWidth="75" Height="35" Margin="5,10,0,5" Click="btnReturn_Click"/> </button></stackpanel> <wrappanel x:name="wrpListBoxes" width="Auto" maxheight="120" grid.row="0" grid.column="1"> Margin="30,10,30,10" Orientation="Horizontal" Background="AliceBlue"> <listbox x:name="lstBroker" width="125" height="Auto" minheight="50"> Margin="100,10,0,5" HorizontalAlignment="Center" VerticalAlignment="Center" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" Background="LightYellow"/> <listbox x:name="lstCatagory" width="125" height="Auto" minheight="50"> Margin="5,10,0,5" HorizontalAlignment="Center" VerticalAlignment="Center" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" Background="LightYellow"/> <listbox x:name="lstSubCatagory" width="125" height="Auto" minheight="50"> Margin="5,10,0,5" HorizontalAlignment="Center" VerticalAlignment="Center" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" Background="LightYellow"/> </listbox></listbox></listbox></wrappanel> <wrappanel x:name="wrpComboBoxes" width="Auto" height="Auto" grid.row="1" grid.column="1"> Margin="30,5,30,10" Orientation="Horizontal" Background="AliceBlue" > <combobox x:name="cboBroker" width="125" height="20" margin="100,10,0,0"> HorizontalAlignment="center" VerticalAlignment="Center" Background="PaleGreen" ItemsSource="{Binding Path=EntryList}" DisplayMemberPath="Broker" DropDownClosed="cboBroker_DropDownClosed" /> <combobox x:name="cboCatagory" width="125" height="20" margin="5,10,0,0"> HorizontalAlignment="Left" VerticalAlignment="Top" Background="PaleGreen" ItemsSource="{Binding Path=EntryList}" DisplayMemberPath="Catagory" DropDownClosed="cboCatagory_DropDownClosed_1" /> <combobox x:name="cboSubCatagory" width="125" height="20" margin="5,10,0,0"> HorizontalAlignment="Left" VerticalAlignment="Top" Background="PaleGreen" ItemsSource="{Binding Path=EntryList}" DisplayMemberPath="SubCatagory" DropDownClosed="cboSubCatagory_DropDownClosed" /> </combobox></combobox></combobox></wrappanel> </grid></window>Code behind:using System;using System.Collections.Generic;using System.Windows;using System.Windows.Controls;namespace ComboBoxTester{ /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { string Broker; string Catagory; string SubCatagory; int i = 0; string Num; Transactions transaction = new Transactions(); List<transactions> EntryList = new List<transactions>(); public MainWindow() { InitializeComponent();// Create a list of Brokers, Catagories and SubCatagories to add to comboboxes for(i= 1; i<=10; i++) { Num = i.ToString(); Broker = "Broker" + Num; Catagory = "Catagory" + Num; SubCatagory = "SubCatagory" + Num; EntryList.Add(new Transactions() { Broker = Broker, Catagory = Catagory, SubCatagory = SubCatagory }); } cboBroker.ItemsSource = EntryList; cboCatagory.ItemsSource = EntryList; cboSubCatagory.ItemsSource = EntryList; } private void btnReturn_Click(object sender, RoutedEventArgs e) { Application.Current.Shutdown(); } private void cboBroker_DropDownClosed(object sender, EventArgs e) { Broker = cboBroker.Text; lstBroker.Items.Add(Broker); } private void cboCatagory_DropDownClosed(object sender, EventArgs e) { Catagory = cboCatagory.Text; lstCatagory.Items.Add(Catagory); } private void cboSubCatagory_DropDownClosed(object sender, EventArgs e) { SubCatagory = cboSubCatagory.Text; lstSubCatagory.Items.Add(SubCatagory); } private void cboCatagory_DropDownClosed_1(object sender, EventArgs e) { Catagory = cboSubCatagory.Text; lstCatagory.Items.Add(Catagory); }// Create a Transaction type to add to List<transaction> public class Transactions { public string Broker { get; set; } public string Catagory { get; set; } public string SubCatagory { get; set; } } }}</transaction></transactions></transactions> 这篇关于WPF组合框的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-27 14:03