我创建了下面的类,试图保持 Form 类的“干净”,因为在那里工作不多,主要只是引用其他完成工作的类。它在代码位于 Form 类中时起作用,因为我相信 XAML simlpy 中的绑定绑定到 MainList、TypeList 等。但我想这不是它的工作原理。
class CheckedBoxLists
{
public ObservableCollection<MainFilterCheckedListClass> MainList { get; set; }
public ObservableCollection<GroupFunctionCheckedListClass> GroupFunctionList { get; set; }
public ObservableCollection<FormCheckedListClass> FormList { get; set; }
public ObservableCollection<TypeFilterCheckedListClass> TypeList { get; set; }
public ObservableCollection<ThicknessFilterCheckedListClass> ThicknessList { get; set; }
DataTable BoughtProductsMatrix = new DataTable();
DataTable Distinct_filter_main = new DataTable();
DataTable Distinct_filter_groupfunction = new DataTable();
DataTable Distinct_filter_form = new DataTable();
DataTable Distinct_filter_type = new DataTable();
DataTable Distinct_filter_thickness = new DataTable();
public class MainFilterCheckedListClass
{
public string MainText { get; set; }
public bool MainIsSelected { get; set; }
}
public class GroupFunctionCheckedListClass
{
public string GroupFunctionText { get; set; }
public bool GroupFunctionIsSelected { get; set; }
}
public class FormCheckedListClass
{
public string FormText { get; set; }
public bool FormIsSelected { get; set; }
}
public class TypeFilterCheckedListClass
{
public string TypeText { get; set; }
public bool TypeIsSelected { get; set; }
}
public class ThicknessFilterCheckedListClass
{
public string ThicknessText { get; set; }
public bool ThicknessIsSelected { get; set; }
}
public void LoadFilters(DataTable productMatrix)
{
BoughtProductsMatrix = productMatrix;
int test = BoughtProductsMatrix.Columns.Count;
DataView productsView = new DataView(BoughtProductsMatrix);
productsView.RowFilter = "make_or_buy = 'B'";
DataView view = new DataView(BoughtProductsMatrix);
DataTable distinct_filter_main = view.ToTable(true, "filter_main");
DataTable distinct_filter_groupfunction = view.ToTable(true, "filter_groupfunction");
DataTable distinct_filter_form = view.ToTable(true, "filter_form");
DataTable distinct_filter_type = view.ToTable(true, "filter_type");
DataTable distinct_filter_thickness = view.ToTable(true, "filter_thickness");
Distinct_filter_main = distinct_filter_main;
Distinct_filter_groupfunction = distinct_filter_groupfunction;
Distinct_filter_form = distinct_filter_form;
Distinct_filter_type = distinct_filter_type;
Distinct_filter_thickness = distinct_filter_thickness;
}
public ObservableCollection<MainFilterCheckedListClass> MainCheckedListBox()
{
MainList = new ObservableCollection<MainFilterCheckedListClass>();
foreach (DataRow dr in Distinct_filter_main.Rows)
{
MainList.Add(new MainFilterCheckedListClass { MainIsSelected = false, MainText = dr["filter_main"].ToString() }); ;
}
return MainList;
}
public ObservableCollection<GroupFunctionCheckedListClass> GroupFunctionCheckedListBox()
{
GroupFunctionList = new ObservableCollection<GroupFunctionCheckedListClass>();
foreach (DataRow dr in Distinct_filter_groupfunction.Rows)
{
GroupFunctionList.Add(new GroupFunctionCheckedListClass { GroupFunctionIsSelected = false, GroupFunctionText = dr["filter_groupfunction"].ToString() }); ;
}
return GroupFunctionList;
}
public ObservableCollection<FormCheckedListClass> FormCheckedListBox()
{
FormList = new ObservableCollection<FormCheckedListClass>();
foreach (DataRow dr in Distinct_filter_form.Rows)
{
FormList.Add(new FormCheckedListClass { FormIsSelected = false, FormText = dr["filter_form"].ToString() }); ;
}
return FormList;
}
public ObservableCollection<TypeFilterCheckedListClass> TypeCheckedListBox()
{
TypeList = new ObservableCollection<TypeFilterCheckedListClass>();
foreach (DataRow dr in Distinct_filter_type.Rows)
{
TypeList.Add(new TypeFilterCheckedListClass { TypeIsSelected = false, TypeText = dr["filter_type"].ToString() }); ;
}
return TypeList;
}
public ObservableCollection<ThicknessFilterCheckedListClass> ThicknessCheckedListBox()
{
ThicknessList = new ObservableCollection<ThicknessFilterCheckedListClass>();
foreach (DataRow dr in Distinct_filter_thickness.Rows)
{
ThicknessList.Add(new ThicknessFilterCheckedListClass { ThicknessIsSelected = false, ThicknessText = dr["filter_thickness"].ToString() }); ;
}
return ThicknessList;
}
}
以下是我的列表框 XAML 代码:
<Label Content="Main Filter" Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="2"/>
<ListBox Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="4" ItemsSource="{Binding MainList}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding MainIsSelected}" Content="{Binding MainText}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Label Content="Group/Function Filter" Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2"/>
<ListBox Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="5" ItemsSource="{Binding GroupFunctionList}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding GroupFunctionIsSelected}" Content="{Binding GroupFunctionText}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Label Content="Form Filter" Grid.Row="6" Grid.Column="1" Grid.ColumnSpan="2"/>
<ListBox Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="7" ItemsSource="{Binding FormList}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding FormIsSelected}" Content="{Binding FormText}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Label Content="Type Filter" Grid.Row="8" Grid.Column="1" Grid.ColumnSpan="2"/>
<ListBox Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="9" ItemsSource="{Binding TypeList}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding TypeIsSelected}" Content="{Binding TypeText}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Label Content="Thickness Filter" Grid.Row="10" Grid.Column="1" Grid.ColumnSpan="2"/>
<ListBox Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="11" ItemsSource="{Binding ThicknessList}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding ThicknessIsSelected}" Content="{Binding ThicknessText}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
下面是我的表单代码,我在其中创建了 CheckedBoxLists 类的实例,并认为它会自动绑定到该类中的对象上,但事实并非如此。
public Form1(DataTable productMatrix)
{
InitializeComponent();
Classes.CheckedBoxLists checkedBoxLists = new Classes.CheckedBoxLists();
checkedBoxLists.LoadFilters(productMatrix);
}
我能想到的解决这个问题的唯一方法是将 x:Name 属性添加到每个 ListBox 并在 Form 类中执行 ListBoxName.DataContext = checkedBoxLists.MainCheckedListBox() 作为类中的方法应该返回 Observable收集 - 这是一种正确的方法还是我不应该这样做?
答案 0 :(得分:0)
我认为您只需要将 Form1 的 DataContext 设置为 CheckdBoxLists 的实例。
public Form1(DataTable productMatrix)
{
InitializeComponent();
Classes.CheckedBoxLists checkedBoxLists = new Classes.CheckedBoxLists();
checkedBoxLists.LoadFilters(productMatrix);
this.DataContext = checkedBoxLists;
}
看到您的 CheckedBoxLists 类,您还可以考虑使用更全面的 MVVM-UI architecture