[英]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.