0

I have found several example of wpf combobox binding but when trying to adapt it I don't get the expected results. What appears in the combobox instead of the value is this: CuttingProblemHelper.Models.ComboBoxPairs

Can please anyone help?

Example of data in the SQL table:

Id CutProbCategId Problem

1 1 Brins coupés

2 1 Brins grafignés

3 1 Fil non dégainé

What I try to acheive is having a key, value pair for each entry in the combobox from an sql table.

So I created a class to store key, value pair:

namespace CuttingProblemHelper.Models {

public class ComboBoxPairs
{
    public int _Key { get; set; }
    public string _Value { get; set; }

    public ComboBoxPairs(int _key, string _value)
    {
        _Key = _key;
        _Value = _value;
    }
} }

Next get the data from the table and add them to the object ComboBoxPairs

private void AddProblemCategtoCombobox(int categ)
    {
        // erase list
        cmbxProblem.ItemsSource = null;

        // get list
        DataRow[] CutProblemsRows = gediDataSet.CutProblems.Select("CutProbCategId= " + categ);
        // we have rows
        if (CutProblemsRows != null)
        {
            // initialize object ComboBoxPairs
            List<ComboBoxPairs> cbp = new List<ComboBoxPairs>();
            foreach (DataRow row in CutProblemsRows)
            {
                // add id and problem key, value pair
                cbp.Add(new ComboBoxPairs(int.Parse(row["Id"].ToString()), row["Problem"].ToString()));
            }
            // define properties of combobox
            cmbxProblem.DisplayMemberPath = "_Value";
            cmbxProblem.SelectedValuePath = "_Key";
            // bind comboboxpairs list
            cmbxProblem.ItemsSource = cbp.ToList(); 
        }
    }

Here's the XAML of the combobox and formatting of the display of items:

<ComboBox x:Name="cmbxProblem" Grid.Column="1" Margin="10,10,10,10" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" SelectionChanged="cmbxProblem_SelectionChanged" FontSize="40" ItemsSource="{Binding Collection}" Grid.ColumnSpan="2">
            <ComboBox.ItemContainerStyle>
                <Style TargetType="{x:Type ComboBoxItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                                <Label   Name="lbl" Content="{Binding}" ></Label>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsHighlighted" Value="True">
                                        <Setter TargetName="lbl" Property="FontStyle" Value="Normal"></Setter>
                                        <Setter TargetName="lbl" Property="Background" Value="AliceBlue"></Setter>
                                    </Trigger>
                                    <Trigger Property="IsSelected" Value="True">
                                        <Setter TargetName="lbl" Property="FontStyle" Value="Italic"></Setter>
                                    </Trigger>
                                    <Trigger Property="IsSelected" Value="False">
                                        <Setter TargetName="lbl" Property="FontStyle" Value="Normal"></Setter>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ComboBox.ItemContainerStyle>
        </ComboBox>

Thank you very much!

Nico D
  • 7
  • 4
  • https://stackoverflow.com/questions/70309478/c-sharp-wpf-how-can-i-programatically-create-as-many-texblocks-as-needed-to Also this: `cmbxProblem.ItemsSource = cbp.ToList();` should be: `cmbxProblem.ItemsSource = cbp;` – Trevor Dec 20 '21 at 21:12
  • Try the [DisplayMemberPath](https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.itemscontrol.displaymemberpath?view=windowsdesktop-6.0) property – nalka Dec 21 '21 at 02:02
  • cmbxProblem.ItemsSource = cbp; does not work. – Nico D Dec 21 '21 at 14:51
  • DisplayMemberPath has been added but it did nothing alone. Maybe some other thing is missing. I dropped the idea to use a class to store the key value pair and I used this instead: MyCollection = new ObservableCollection>(); But I still need help to show only the value as the items in the combox not the [Key, Value] as it is now. I don't know yet how to fix this issue. – Nico D Dec 21 '21 at 14:53

1 Answers1

0

I found a solution but the combobox does not only display the value but it displays the paired key and value. But when a value is selected it displays only the selected value. Anyone knows what to change to show only the value without the key?

See pictures to better understand.Items in comboboxValue selected

Here's the solution I came up with after searching several combobox solutions: In the XAML

<ComboBox x:Name="cmbxProblem" Grid.Column="1" Margin="10,10,10,10" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" SelectionChanged="cmbxProblem_SelectionChanged" FontSize="40" ItemsSource="{Binding MyCollection}" DisplayMemberPath="_Value" SelectedValuePath="_Key" Grid.ColumnSpan="2">
            <ComboBox.ItemContainerStyle>
                <Style TargetType="{x:Type ComboBoxItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                                <Label   Name="lbl" Content="{Binding}" ></Label>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsHighlighted" Value="True">
                                        <Setter TargetName="lbl" Property="FontStyle" Value="Normal"></Setter>
                                        <Setter TargetName="lbl" Property="Background" Value="AliceBlue"></Setter>
                                    </Trigger>
                                    <Trigger Property="IsSelected" Value="True">
                                        <Setter TargetName="lbl" Property="FontStyle" Value="Italic"></Setter>
                                    </Trigger>
                                    <Trigger Property="IsSelected" Value="False">
                                        <Setter TargetName="lbl" Property="FontStyle" Value="Normal"></Setter>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ComboBox.ItemContainerStyle>
        </ComboBox>

In the code behind:

 private void AddProblemCategtoCombobox(int categ)
    {
        // delete list
        cmbxProblem.ItemsSource = null;

        // get items for list
        DataRow[] CutProblemsRows = gediDataSet.CutProblems.Select("CutProbCategId= " + categ);
        // we have items
        if (CutProblemsRows != null)
        {
            // create collection
            MyCollection = new ObservableCollection<KeyValuePair<int, string>>();

            foreach (DataRow row in CutProblemsRows)
            {
                // fill collection with items
                MyCollection.Add(new KeyValuePair<int, string>(int.Parse(row["Id"].ToString()), row["Problem"].ToString()));
            }
            // define properties for the combobox
            cmbxProblem.DisplayMemberPath = "Value";
            cmbxProblem.SelectedValuePath = "Key";
            cmbxProblem.ItemsSource = MyCollection; 
        }
    }
Nico D
  • 7
  • 4