How to Bind Checked / Selected Items From CheckListBox in WPF by using MVVM Model (I am using "WPFToolkit.Extended.dll" to get CheckListBox Control)

163 views Asked by At

CheckList Box from WPFToolKit. Below is XAML code (MainWindow.xaml)

<xctk:CheckListBox x:Name="SiteCheckList" Margin="0,0,512,0" Height="100" Width="150"
                               ItemsSource="{Binding SiteList}"
                               DisplayMemberPath="SiteName"
                               CheckedMemberPath="IsChecked">
            </xctk:CheckListBox>

Below Properties added in Model Class. I would like to get Checked Items from CheckListBox to my string List (Model.cs). This string List I will be using for further in project logic.

private string _SiteName;

        public string SiteName
        {
            get { return _SiteName; }
            set { _SiteName = value; }
        }

    private List<string> _SelectedSiteList;
            public List<string> SelectedSiteList
            {
                get { return _SelectedSiteList; }
                set
                {
                    _SelectedSiteList = value;
                }
            }

View Model (ViewModel.cs)

class ViewModel : INotifyPropertyChanged {

    private ObservableCollection<Model> _SiteList;

    public ObservableCollection<DataModel> SiteList
    {
        get { return _SiteList; }
        set { _SiteList = value; }
    }

    public ViewModel()
    {
        SiteList = new ObservableCollection<Model>();
        PoppulateSiteNames();
    }


    private void PoppulateSiteNames()
    {
        Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
        keyValuePairs = Files.ReadIni_KeyValue("SiteSection");
        foreach (string Key in keyValuePairs.Keys)
        {
            keyValuePairs.TryGetValue(Key, out string LogTable);
            SiteList.Add(new Model() { SiteName = LogTable });
        }

    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string PropertyName)
    {
        if (PropertyChanged !=null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }
    }

}

Here I would like to get list of Checked / Selected Items from UI. If I don't want to write any code in MainWindow.cs i.e. CheckedChanged event then How I can do it by using Binding method ?

1

There are 1 answers

0
Satyavan Choure On BEST ANSWER

Updated Model Class with IsChecked Boolean Property.

private bool _IsChecked;

        public bool IsChecked
        {
            get { return _IsChecked; }
            set { _IsChecked = value; }
        }

Then ViewModel Updated with Below Function to Populate SiteList

 private void PoppulateSiteNames()
        {
            Dictionary<string, string> keyValuePairs = Files.ReadIni_KeyValue(Vars.MSSQL_Section);
            string [] Sites = keyValuePairs.Values.ToArray();
            for (int i = 0; i < Sites.Length; i++)
            {
                SiteList.Add(new HistoricalDataModel { SiteName = Sites[i] });
            }
        }

Now, Finally I got CheckedItems in below Variable using LINQ

var checkedSites = from site in SiteList
                                where (site.IsChecked == true)
                                select new { site.SiteName };

Thank you all of you for responding my question and triggering me to think more.