将组合框显示文本设置为ObservableCollection< T>的属性。 [英] Set combobox display text to a property of ObservableCollection<T>

查看:117
本文介绍了将组合框显示文本设置为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; }
}

我需要将组合框的显示文本绑定到的Text属性。 Parameter类,下面我尝试了以下方法,但都无济于事:

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"

当我尝试[程序集名称]上方的3种方法时。

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}"

然后,该函数将第一个参数的文本拆分为字符,并将每个字符显示为单独它em在组合框中:

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}"

更新:

这是完整的XAML代码

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类:

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; }
}


推荐答案

只需删除组合框项目模板或设置 Text = {Binding Text}

Had to simply remove the combobox item template or set Text="{Binding Text}"

   <ComboBox.ItemTemplate> 
        <DataTemplate> 
            <TextBlock Grid.Column="1" Margin="2, 1" Text="{Binding}"/> 
        </DataTemplate> 
    </ComboBox.ItemTemplate> 

这篇关于将组合框显示文本设置为ObservableCollection&lt; T&gt;的属性。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆