Pav
Pav

Reputation: 25

Binding WPF control to objects within a class separate to form class

I have created the below class in attempt to keep Form class "clean" as in not much working out there and mainly just references to other classes which do the work. It worked when the code was within the Form class as I believe the binding in XAML simlpy bound to the MainList, TypeList etc. ObservableCollection and I thought if I create an instance of the CheckedBoxLists class it will still find those objects and display data from them but I guess that is not how it works.

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;
    }
}

Below is my XAML code for the ListBoxes:

<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>

So below is my Form code where I have created an instance of the CheckedBoxLists class and thought it will automatically bind on the objects within that class, but it did not.

public Form1(DataTable productMatrix)
    {
        InitializeComponent();

        Classes.CheckedBoxLists checkedBoxLists = new Classes.CheckedBoxLists();

        checkedBoxLists.LoadFilters(productMatrix);
    }

The only way of solving this that I can think of would be to add x:Name property to each ListBox and within the Form class do ListBoxName.DataContext = checkedBoxLists.MainCheckedListBox() as the method in the class should return the Observable Collection - is this a correct approach or should I not do it this way?

Upvotes: 0

Views: 42

Answers (1)

oleconer
oleconer

Reputation: 61

I think you just need to set the DataContext of your Form1 to the instance of CheckdBoxLists.

public Form1(DataTable productMatrix)
{
    InitializeComponent();

    Classes.CheckedBoxLists checkedBoxLists = new Classes.CheckedBoxLists();

    checkedBoxLists.LoadFilters(productMatrix);
    this.DataContext = checkedBoxLists;
}

Seeing your CheckedBoxLists class you could also look into using a more comprehensive MVVM-UI architecture

Upvotes: 1

Related Questions