Search code examples
c#wpflistboxseparator

How to add a separator to a listBox from code AND cast it to not selectable


enter image description here

This is what I do and it works:

 if (condition...)
 {
   ComboBoxItem cbi2 = new ComboBoxItem();
   cbi2.Content = new TextBox() { Background= Brushes.Red, HorizontalAlignment= HorizontalAlignment.Stretch, Margin=new Thickness(0) , Height = 5, IsHitTestVisible=false, BorderBrush=null};
   ibw.cmbOptions.Items.Add(cbi2);
 }

the only problem is that the textBox despite not being IsHitTestVisible is selectable and that is a problem. So I'm open also to totally different solution. The only constraint is to act from code.

enter image description here


Solution

  • I can see 2 options here:

    First, simply use the Separator element:

    ibw.cmbOptions.Items.Add(new Separator());
    

    Or, you could disable the ComboBoxItem, which would make it non-selectable for the ComboBox.

    ComboBoxItem cbi2 = new ComboBoxItem();
    cbi2.IsEnabled = false;
    cbi2.Content = new TextBox() { Background= Brushes.Red, HorizontalAlignment= HorizontalAlignment.Stretch, Margin=new Thickness(0) , Height = 5, IsHitTestVisible=false, BorderBrush=null};
    ibw.cmbOptions.Items.Add(cbi2);