Home > Software design >  C# CheckBox Content correct formatting
C# CheckBox Content correct formatting

Time:12-22

I have a C# WPF application, where I create CheckBoxes dynamically, and their properties are set through a foreach loop through a MyList, made of objects, as I need to contain different types of data. At index 1, the lists contain the Name I want to use as the CheckBox.Content.

My loop looks like:

int i = 1;
foreach (List<Object> o in MyList)
{
CheckBox cb = new CheckBox();
cb.Name = o[1].ToString()   i.ToString();
cb.Content = string.Format("  "   o[1].ToString());
// then I add the CheckBox to a StackPanel
checkBoxesStackPanel.Children.Add(cb);
i  ;
}

The issue I face is that CheckBox.Content does not format correctly the names from the lists. I cannot control the characters of the list, as they are created by specific users groups with different requirements.

Example: if I have an element in the list like "1234_ABC_123456_AB_12" it is shown in the WPF as something similar to "1234ABC_123456_AB_12".

How do I format correctly the cb.Content so the CheckBoxes.Content will display correctly in my WPF app?

I looked at:

https://docs.microsoft.com/en-us/dotnet/desktop/wpf/controls/checkbox-styles-and-templates?view=netframeworkdesktop-4.8

but could not find an example to address this issue.

Initially, I had this solution in WindowsForms, and the CheckBoxes text was displaying correctly. Now I'm migrating the solution to WPF and I need to fix this last small (but annoying) issue.

Thank you for your help!

CodePudding user response:

You could try to see if the following method suits you. MainWindow.xaml:

<StackPanel x:Name="sp">
        
</StackPanel>

MainWindow.xaml.cs:

public partial class MainWindow : Window
  {
    List<string> MyList;
    public MainWindow()
    {
      InitializeComponent();
      MyList = new List<string>();
      for (int i = 1; i < 10; i  )
      {
        MyList.Add( $"1234_ABC_123456_AB_1{ i }");
      }
      int n = 1;
      foreach (var item in MyList)
      {
        if (n < MyList.Count)
        {
          CheckBox cb = new CheckBox();
          cb.Name = "cb"   n;
          cb.Content = MyList[n].ToString();
          sp.Children.Add(cb);
        }
        n  ;
      }
    }
  }

The result picture:

CodePudding user response:

It will be better if you use like this MainWindow.xaml.cs

public partial class MainWindow : Window
    {
        public ObservableCollection<CheckBoxItem> MyCollection { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            MyCollection = new ObservableCollection<CheckBoxItem>(CheckBoxItem.GetCheckBoxItems());
        }
    }

    public class CheckBoxItem
    {
        public bool IsChecked { get; set; }
        public string Title { get; set; }

        public static IEnumerable<CheckBoxItem> GetCheckBoxItems()
        {
            for (int i = 0; i < 10; i  )
            {
                yield return new CheckBoxItem
                {
                    Title = $"1234_ABC_123456_AB_1{i}"
                };
            }
        }
    
    }

MainWindow.xaml

 <ItemsControl ItemsSource="{Binding MyCollection}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding IsChecked}"
                              Content="{Binding Title}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
  • Related