Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set combobox display text to a property of ObservableCollection<T>

I have the following collection which i would like to bind to a combobox:

public ObservableCollection<Parameter> Values
{ get; set; }

public class Parameter
{
    public String Text { get; set; }
    public String Value { get; set; }
}

I need to bind the display text of the combobox to the Text property of the Parameter class, I've tried the following ways below but all to no avail:

<ComboBox ItemsSource="{Binding Values}" DisplayMemberPath="Parameter.Text"
<ComboBox ItemsSource="{Binding Values}" DisplayMemberPath="Parameter\Text"
<ComboBox ItemsSource="{Binding Values}" DisplayMemberPath="Text"

When I try the 3 methods above the [assembly name].Parameter is displayed in the combobox for each parameter.

The 2 methods above don't display anything in the combobox

<ComboBox ItemsSource="{Binding Values, Path=Text}"
<ComboBox ItemsSource="{Binding Values, Path=Values.Text}"

And this one takes the text of the first parameter,splits it into characters and displays each character as a seperate item in the combobox:

<ComboBox ItemsSource="{Binding Values, Path=Values/Text}"

enter image description here

UPDATE:

This is the complete XAML code as requested

<ListBox BorderBrush="{x:Null}" Grid.Column="0" Height="100" Grid.ColumnSpan="2" Grid.Row="1" ItemsSource="{Binding ItemParams}" ScrollViewer.VerticalScrollBarVisibility="Auto" Margin="2" HorizontalContentAlignment="Stretch">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <TextBlock TextTrimming="CharacterEllipsis" Grid.Column="0" Margin="2" Text="{Binding Name}" Background="{Binding ElementName=cmbColors, Path=SelectedItem}"/>
                <ComboBox ItemsSource="{Binding Values}" DisplayMemberPath="Text" SelectedIndex="0" HorizontalAlignment="Left" Grid.Column="1" Margin="2" Width="150" Name="cmbColors" >
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Grid.Column="1" Margin="2, 1" Text="{Binding}"/>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Params class:

public class Params
{
    public Params(String name, ObservableCollection<Parameter> values)
    {
        Name = name;
        Values = values;
    }

    public String Name
    { get; set; }

    public ObservableCollection<Parameter> Values
    { get; set; }
}
like image 418
Denys Wessels Avatar asked Dec 26 '22 22:12

Denys Wessels


1 Answers

The correct formulation is one of those you already mention:

<ComboBox ItemsSource="{Binding Values}" DisplayMemberPath="Text"

Please check again, and if it does not work post the XAML that you have verbatim.

like image 109
Jon Avatar answered Dec 29 '22 11:12

Jon