This project has moved. For the latest updates, please go here.

CheckListBox not showing data

Jun 3, 2014 at 11:10 PM
I've created a User Control with a CheckListBox and when the Control is displayed, there is no data visible. I have verified that the CheckListBox items have the contents that were added, but the display has unchecked boxes and no text. What am I missing?

This is the XAML:
<UserControl x:Class="frmMatlList"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"              
         mc:Ignorable="d" Height="259" Width="478">
<Grid>
    <xctk:CheckListBox x:Name="lbMatlList" 
                        Height="250"
                        Width="400"
                        DisplayMemberPath="TheText"
                        ValueMemberPath="TheText" 
                        SelectedMemberPath="IsSelected"
                        SelectedValue="{Binding TheText}"
                        Delimiter="|"
                        SelectedItemsOverride="{Binding SelectedItems}" >
    </xctk:CheckListBox>
....
The VB code that inserts data is:
    Me.lbMatlList.Items.Clear()
    iMatls = 0
    For iNum = 1 To MatlList.LongCount
        tString = MatlList(iNum - 1)
        ' Check whether this entry should be checked
        Used = ContainsLabel(tString, SelMatlList) 
        Me.lbMatlList.Items.Add(New Used_Material(Used, tString))
        Dim MatlItem As Used_Material = Me.lbMatlList.Items(iMatls)
        iMatls += 1
    Next iNum
with Class:
Class Used_Material
Public IsSelected As Boolean
Public TheText As String
Public Sub New(ByVal tmpUsed As Boolean, ByVal tmpName As String)
    IsSelected = tmpUsed
    TheText = tmpName
End Sub
End Class
Developer
Jun 4, 2014 at 1:26 PM
Hi,

Please modify your variable "IsSelected" and "TheText" to become Properties.
Here's your sample, slightly modified, in C#
  public partial class MainWindow : Window
  {
    private static List<string> MatlList = new List<string>()
    {
      "first", "second", "third", "fourth"
    };

    public MainWindow()
    {
      InitializeComponent();

      this.lbMatlList.Items.Clear();
      int iMatls = 0;
      for( int iNum = 1; iNum <= MatlList.Count; iNum++ )
      {
        string tString = MatlList[ iNum - 1 ];
        // Check whether this entry should be checked
        bool Used = true; //ContainsLabel( tString, SelMatlList );
        this.lbMatlList.Items.Add( new Used_Material( Used, tString ) );
        //Used_Material MatlItem = this.lbMatlList.Items( iMatls );
        iMatls += 1;
      }
    }
  }

  public class Used_Material
  {
    public bool IsSelected
    {
      get;
      set;
    }
    public string TheText
    {
      get;
      set;
    }

    public Used_Material( bool tmpUsed, string tmpName )
    {
      IsSelected = tmpUsed;
      TheText = tmpName;
    }
  }
Jun 4, 2014 at 2:35 PM

Thank you so much – the text is showing up, but the checkbox entries are not showing up (all unchecked).

Are the parameters correct for the CheckListBox object?

<xctk:CheckListBox x:Name="lbMatlList"

Height="250"

Width="400"

DisplayMemberPath="TheText"

ValueMemberPath="TheText"

SelectedMemberPath="IsSelected"

SelectedValue="{Binding TheText}"

Delimiter="|"

SelectedItemsOverride="{Binding IsSelected}" >

</xctk:CheckListBox>

Developer
Jun 4, 2014 at 5:16 PM
Hi,

You can find the doc on the properties of the CheckListBox here : https://wpftoolkit.codeplex.com/wikipage?title=CheckListBox&referringTitle=Home

If you don't have the "IsSelected" property, you don't need to set the "SelectedMemberPath". "SelectedValue" is the string representing the selection. If you set the "SelectedItemsOverride", you don't need to set the "SelectedValue". "SelectedItemsOverride" is the Collection of items selected. If you have a "SelectedItems" property on your object, you can use it to set/get the selected items.

Here's what works for me :
 <xctk:CheckComboBox x:Name="lbMatlList"
                         Height="250"
                         Width="400"
                         DisplayMemberPath="TheText"
                         Delimiter="|"
                         SelectedItemsOverride="{Binding SelectedItems}">
</xctk:CheckComboBox>

  public partial class MainWindow : Window
  {
    private static List<string> MatlList = new List<string>()
    {
      "first", "second", "third", "fourth"
    };

    public MainWindow()
    {
      InitializeComponent();

      this.lbMatlList.Items.Clear();
      int iMatls = 0;
      for( int iNum = 1; iNum <= MatlList.Count; iNum++ )
      {
        string tString = MatlList[ iNum - 1 ];
        // Check whether this entry should be checked
        bool Used = true; //ContainsLabel( tString, SelMatlList );
        this.lbMatlList.Items.Add( new Used_Material( Used, tString ) );
        //Used_Material MatlItem = this.lbMatlList.Items( iMatls );
        iMatls += 1;
      }

      this.lbMatlList.DataContext = this;  //To be able to use MainWindow.SelectedItems property
      this.SelectedItems = new ObservableCollection<Used_Material>() { this.lbMatlList.Items[ 1 ] as Used_Material };
    }

    public ObservableCollection<Used_Material> SelectedItems
    {
      get;
      set;
    }
  }

  public class Used_Material
  {
    public bool IsSelected
    {
      get;
      set;
    }
    public string TheText
    {
      get;
      set;
    }

    public Used_Material( bool tmpUsed, string tmpName )
    {
      IsSelected = tmpUsed;
      TheText = tmpName;
    }
  }