我目前正在尝试做什么: "通过Category_ComboBox及其primary_Key显示所有类别,以便该选项的组合框值包含该行的主键>允许进行检查以查看该行数据是否具有sub_category。"
数据源数据的外观:
Category / Sub_Category_ComboBox目前如何显示:
数据当前是从SQL数据源中提取的,这意味着类似
Category_ComboBox.Items.Add(new { cat.Key, cat.Category } )
不起作用。我假设我必须做类似的事情:
Category_ComboBox.ItemsSource = Enum.GetValues(typeof()).Cast<>();
或者我应该删除itemsource并让它通过后端代码添加值?得到的例外情况:
Operation is not valid while ItemsSource is in use.
Access and modify elements with ItemsControl.ItemsSource instead
如果没有意义,可以进一步澄清。
整个代码,供参考:
C#:
/*
* What we're trying to do:
*
* First, determine whether or not the row(s) data for the Category column have data for the Sub_Category column or if the Category data contains the same values (names, EX: BillTrak Pro), then sort them accordingly.
** Sub_Categories should only Sync with their corresponding Categories, not all Categories.
*
*
* Conditionals for the Category data pertaining to Sub_Category(s):
** If no Sub_Category(s) exist for the WHOLE Category, then > Sub_Category_ComboBox should be set to read ONLY(IsEnabled = false) and the four ListView's should just display single Category Information.
*
*
** If Category(s) contain Sub_Category(s), then SubCategory_ComboBox should display only the Sub_Categories that are Sync'd to the specific Category(s).
* Row Data should be sorted, first by Category, then Sub_Category, then displayed only when their Sub_Category is displayed via the SubCategory_ComboBox.
* The four ListView's data should only display data, coresponding to the SubCategory_ComboBox data.
*
*/
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
WpfApplication1.DataSet1 dataSet1 = ((WpfApplication1.DataSet1)(this.FindResource("dataSet1")));
// Load data into the table OP_MAIN_COLLECTIONS. You can modify this code as needed.
WpfApplication1.DataSet1TableAdapters.OP_MAIN_COLLECTIONSTableAdapter dataSet1OP_MAIN_COLLECTIONSTableAdapter = new WpfApplication1.DataSet1TableAdapters.OP_MAIN_COLLECTIONSTableAdapter();
dataSet1OP_MAIN_COLLECTIONSTableAdapter.Fill(dataSet1.OP_MAIN_COLLECTIONS);
System.Windows.Data.CollectionViewSource oP_MAIN_COLLECTIONSViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("oP_MAIN_COLLECTIONSViewSource")));
oP_MAIN_COLLECTIONSViewSource.View.MoveCurrentToFirst();
WpfApplication1.DataSet2 dataSet2 = ((WpfApplication1.DataSet2)(this.FindResource("dataSet2")));
// Load data into the table OP_TITLE_INFORMATION. You can modify this code as needed.
WpfApplication1.DataSet2TableAdapters.OP_TITLE_INFORMATIONTableAdapter dataSet2OP_TITLE_INFORMATIONTableAdapter = new WpfApplication1.DataSet2TableAdapters.OP_TITLE_INFORMATIONTableAdapter();
dataSet2OP_TITLE_INFORMATIONTableAdapter.Fill(dataSet2.OP_TITLE_INFORMATION);
System.Windows.Data.CollectionViewSource oP_TITLE_INFORMATIONViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("oP_TITLE_INFORMATIONViewSource")));
oP_TITLE_INFORMATIONViewSource.View.MoveCurrentToFirst();
// Load data into the table OP_MAIN_COLLECTIONS. You can modify this code as needed.
WpfApplication1.DataSet2TableAdapters.OP_MAIN_COLLECTIONSTableAdapter dataSet2OP_MAIN_COLLECTIONSTableAdapter = new WpfApplication1.DataSet2TableAdapters.OP_MAIN_COLLECTIONSTableAdapter();
dataSet2OP_MAIN_COLLECTIONSTableAdapter.Fill(dataSet2.OP_MAIN_COLLECTIONS);
System.Windows.Data.CollectionViewSource oP_MAIN_COLLECTIONSViewSource1 = ((System.Windows.Data.CollectionViewSource)(this.FindResource("oP_MAIN_COLLECTIONSViewSource1")));
oP_MAIN_COLLECTIONSViewSource1.View.MoveCurrentToFirst();
}
/**
* Operations Guide Name Display settings.
*/
private void OPGuideName_textBox_TextChanged(object sender, TextChangedEventArgs e)
{
}
/**
* Category_ComboBox Display settings.
*/
private void Category_ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
/**
* TO-DO:
* Determine whether or not the row(s) data for the Category column have data for the Sub_Category column
* or if the Category data contains the same values (names, EX: BillTrak Pro), then sort them accordingly.
*/
WpfApplication1.DataSet2 DS = ((WpfApplication1.DataSet2)(this.FindResource("dataSet2")));
var cats = DS.Tables[0].AsEnumerable().Select(r => new { Key = r.Field<decimal>("Primary_Key"), Category = r.Field<string>("Category") }).ToList();
foreach (var cat in cats)
{
//Add category and primary key to combo box
Category_ComboBox.Items.Add(new { cat.Key, cat.Category } );
}
// foreach (DataRow rows in DS.Tables[0].Rows)
// {
// decimal primary_key = rows.Field<decimal>("Primary_Key");
// string category = rows.Field<string>("Category");
//
// //populate the combo box
// Category_ComboBox.Items.Add(new { primary_key, category } );
// }
int id = 1; //Get form category combox
DataRow row = DS.Tables[0].AsEnumerable().Where(r => r.Field<decimal>("Primary_Key") == id).FirstOrDefault();
if (row != default(DataRow))
{
if (row["sub_category"] != DBNull.Value)
{
//There is a sub category
MessageBox.Show("Reached");
SubCategory_ComboBox.IsEnabled = true;
}
else
{
//sub category is null
SubCategory_ComboBox.IsEnabled = false;
}
}
else
{
//selected category id does not exists
}
}
/**
* SubCategory_ComboBox Display settings.
*/
private void SubCategory_ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
/**
* Purpose:
* Should convert SubCategory_ComboBox to readOnly(IsEnabled = false)
* if the data contained in the SubCategory SQL Column is null && Category doesn't contain any Sub_Categories.
* Else, display SubCategory_ComboBox as normal, sync'd with the corresponding category name.
*
* Currently, This code disables the subcategory combobox completely.
* Need to figure out how to get it to read ONLY the null data, not the entire column.
*/
if (string.IsNullOrEmpty(SubCategory_ComboBox.Text))
{
SubCategory_ComboBox.IsEnabled = false;
}
}
private void sCHEDULEListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}
}