将 WPF 控件绑定到 class 中的对象,单独形成 class
Binding WPF control to objects within a class separate to form class
我创建了下面的 class 试图保持表格 class “干净”,因为在那里没有太多工作,主要只是引用其他 classes 做工作。当代码在 Form class 中时它起作用了,因为我相信 XAML 中的绑定 simlpy 绑定到 MainList、TypeList 等。ObservableCollection 我想如果我创建 CheckedBoxLists [=26= 的实例] 它仍然会找到那些对象并显示它们的数据,但我想这不是它的工作方式。
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 class 的实例,并认为它会自动绑定到 class 中的对象,但它没有。
public Form1(DataTable productMatrix)
{
InitializeComponent();
Classes.CheckedBoxLists checkedBoxLists = new Classes.CheckedBoxLists();
checkedBoxLists.LoadFilters(productMatrix);
}
我能想到的解决这个问题的唯一方法是将 x:Name 属性 添加到每个 ListBox 并在表单 class 中执行 ListBoxName.DataContext = checkedBoxLists.MainCheckedListBox() 作为 class 中的方法应该 return Observable Collection - 这是正确的方法还是我不应该这样做?
我认为您只需要将 Form1 的 DataContext 设置为 CheckdBoxLists 的实例即可。
public Form1(DataTable productMatrix)
{
InitializeComponent();
Classes.CheckedBoxLists checkedBoxLists = new Classes.CheckedBoxLists();
checkedBoxLists.LoadFilters(productMatrix);
this.DataContext = checkedBoxLists;
}
查看您的 CheckedBoxLists class 您还可以考虑使用更全面的 MVVM-UI architecture
我创建了下面的 class 试图保持表格 class “干净”,因为在那里没有太多工作,主要只是引用其他 classes 做工作。当代码在 Form class 中时它起作用了,因为我相信 XAML 中的绑定 simlpy 绑定到 MainList、TypeList 等。ObservableCollection 我想如果我创建 CheckedBoxLists [=26= 的实例] 它仍然会找到那些对象并显示它们的数据,但我想这不是它的工作方式。
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 class 的实例,并认为它会自动绑定到 class 中的对象,但它没有。
public Form1(DataTable productMatrix)
{
InitializeComponent();
Classes.CheckedBoxLists checkedBoxLists = new Classes.CheckedBoxLists();
checkedBoxLists.LoadFilters(productMatrix);
}
我能想到的解决这个问题的唯一方法是将 x:Name 属性 添加到每个 ListBox 并在表单 class 中执行 ListBoxName.DataContext = checkedBoxLists.MainCheckedListBox() 作为 class 中的方法应该 return Observable Collection - 这是正确的方法还是我不应该这样做?
我认为您只需要将 Form1 的 DataContext 设置为 CheckdBoxLists 的实例即可。
public Form1(DataTable productMatrix)
{
InitializeComponent();
Classes.CheckedBoxLists checkedBoxLists = new Classes.CheckedBoxLists();
checkedBoxLists.LoadFilters(productMatrix);
this.DataContext = checkedBoxLists;
}
查看您的 CheckedBoxLists class 您还可以考虑使用更全面的 MVVM-UI architecture