[英]bind one combobox to another in wpf
我有三个组合框,我正在从数据库中获取每个组合框的值,并且每个组合框都依赖于另一个。 当前,我在SelectionChanged()事件上填充组合框。 我正在MainWindow的构造函数中填充第一个组合框。
因此,当我从第一个组合框中选择一个值时,将调用selectionchanged事件。 然后填充第二个combox。 当我从第二个组合框中选择一个值时,在selectionChanged事件中填充第三个组合框。 从第三个组合框中选择值后,我正在填充数据网格。
从第三个组合框中选择值后尝试更改第一个组合框的值时,出现错误。
你调用的对象是空的。!
我正在尝试将组合框相互绑定,但是我不知道有什么方法可以使它成为wpf的新手。
我附上了combobox的代码以供参考。
public MainWindow()
{
InitializeComponent();
Fill_Combo();
}
//adding values in first combo box
void Fill_Combo()
{
try
{
string query = "select column_name from table_name";
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
OracleCommand command = new OracleCommand();
command.Connection = conn;
command.CommandText = query;
OracleDataReader reader = command.ExecuteReader();
while (reader.Read())
{
String str = reader.GetString(0);
//source_name is name of combobox1
source_name.Items.Add(str);
}
reader.Dispose();
}
catch (Exception e)
{
MessageBox.Show(e.StackTrace + " :: in fill_combo");
}
}
// adding value in second combo box based on value in first combobox
private void source_name_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string query = "select column from table where source_name='" + source_name.SelectedItem.ToString() + "'";
try
{
OracleConnection connection = new OracleConnection(connectionString);
connection.Open();
OracleCommand command = new OracleCommand();
command.Connection = connection;
command.CommandText = query;
OracleDataReader reader = command.ExecuteReader();
//clearing previous values in combobox2
//target_box is the name of second combo box
target_box.Items.Clear();
while (reader.Read())
{
String str = reader.GetString(0);
//adding values in second combobox
target_box.Items.Add(str);
}
reader.Dispose();
connection.Close();
}
catch (Exception exp)
{
MessageBox.Show(exp.Message + "source_name namespace SelectionChangedEventArgs chanegs");
}
}
private void target_box_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
if (target_box.SelectedItem.ToString() != null && target_box.SelectedItem.ToString() != "Target system")
{
string query = "select columnname from map_pair_attributes where source_name='" + source_name.SelectedItem.ToString() + "'"
+ "and target_name = '" + target_box.SelectedItem.ToString() + "'";
OracleConnection connection = new OracleConnection(connectionString);
connection.Open();
OracleCommand command = new OracleCommand();
command.Connection = connection;
command.CommandText = query;
OracleDataReader reader = command.ExecuteReader();
attribute_box.Items.Clear();
while (reader.Read())
{
String str = reader.GetString(0);
//attribute box is name of third combobox
attribute_box.Items.Add(str);
}
reader.Dispose();
connection.Close();
connection.Dispose();
}
}
catch (Exception ex)
{
MessageBox.Show("in target box selection changed!" + ex.StackTrace + ex.Message);
}
}
//atributebox is name of third combobox
private void attribute_box_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
String query = "select column_name from table where source_name='" + source_name.SelectedItem.ToString() + "' and target_name ='" +
target_box.SelectedItem.ToString() + "' and attribute_name = '" + attribute_box.SelectedItem.ToString() + "'";
OracleConnection conn = new OracleConnection(connectionString);
conn.Open();
DataSet ds = new DataSet();
OracleDataAdapter da = new OracleDataAdapter(query, conn);
da.Fill(ds, "table_name");
grid1.ItemsSource = ds.Tables["table_name"].DefaultView;
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show("attribute box selection changed!" + ex.Message);
}
}
.XAML文件
<ComboBox x:Name="target_box" HorizontalAlignment="Left" Margin="179,49,0,0" VerticalAlignment="Top" Width="120"
IsEditable="true" IsReadOnly="True" Text="Target system"
Focusable="True" SelectionChanged="target_box_SelectionChanged" />
<ComboBox x:Name="source_name" HorizontalAlignment="Left" Margin="19,49,0,0" VerticalAlignment="Top" Width="120"
IsEditable="true" IsReadOnly="True" Text="Source system" Focusable="True" SelectionChanged="source_name_SelectionChanged"/>
<ComboBox x:Name="attribute_box" HorizontalAlignment="Left" Margin="363,49,0,0" VerticalAlignment="Top" Width="120"
IsEditable="true" IsReadOnly="True" Text="Attribute" Focusable="True" SelectionChanged="attribute_box_SelectionChanged" />
有人可以帮我吗 我认为绑定可以解决问题,但是我不知道如何绑定组合框。
谢谢!
当您在ComboBox
选择一个新项目时, ComboBox.SelectedItem
首先设置为null。
在尝试在对象上调用ToString()之前,请尝试在每个事件处理程序中添加ComboBox.SelectedItem = null的检查。
例如
if(source_name.SelectedItem == null) return;
然后替换您的ToString空检查
if (target_box.SelectedItem.ToString() != null
与
if (!string.IsNullOrEmpty(target_box.SelectedItem.ToString())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.