WPF 组合框 DisplayMemberPath

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1460612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:01:36  来源:igfitidea点击:

WPF Combobox DisplayMemberPath

wpfcombobox

提问by Jose

Ok, I looked at other questions and didn't seem to get my answer so hopefully someone here can.

好的,我看了其他问题,似乎没有得到我的答案,所以希望这里有人可以。

Very simple question why does the DisplayMemberPath property not bind to the item?

很简单的问题,为什么 DisplayMemberPath 属性没有绑定到项目?

<ComboBox Grid.Row="1" Grid.Column="2" ItemsSource="{Binding PromptList}" DisplayMemberPath="{Binding Name}" SelectedItem="{Binding Prompt}"/>

The trace output shows that it is trying to bind to the class holding the IEnumerable not the actual item in the IEnumerable. I'm confused as to a simple way to fill a combobox without adding a bunch a lines in xaml.

跟踪输出显示它正在尝试绑定到持有 IEnumerable 的类,而不是 IEnumerable 中的实际项目。我对填充组合框的简单方法感到困惑,而无需在 xaml 中添加一行行。

It simply calls the ToString() for the object in itemssource. I have a work around which is this:

它只是为 itemssource 中的对象调用 ToString()。我有一个解决方法是:

<ComboBox Grid.Row="1" Grid.Column="2" ItemsSource="{Binding PromptList}"  SelectedItem="{Binding Prompt}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

But in my opinion it's too much for such a simple task. Can I use a relativesource binding?

但在我看来,对于这样一个简单的任务来说太过分了。我可以使用相对源绑定吗?

回答by Ben M

DisplayMemberPathspecifies the path to the display string property for each item. In your case, you'd set it to "Name", not "{Binding Name}".

DisplayMemberPath指定每个项目的显示字符串属性的路径。在您的情况下,您会将其设置为"Name",而不是"{Binding Name}"

回答by Muad'Dib

You are not binding to the data in the class, you are telling it to get it's data from the class member that is named by the member "name" so, if your instance has item.Name == "steve"it is trying to get the data from item.steve.

您没有绑定到类中的数据,而是告诉它从由成员“name”命名的类成员获取数据,因此,如果您的实例有item.Name == "steve"它试图从item.steve.

For this to work, you should remove the binding from the MemberPath. Change it to MemberPath = "Name"this tells it to get the data from the member "Name". That way it will call item.Name, not item.steve.

为此,您应该从 MemberPath 中删除绑定。将其更改MemberPath = "Name"为此告诉它从成员“名称”获取数据。这样它就会调用item.Name,而不是item.steve.

回答by paparazzo

You could remove DisplayMemberPath and then set the path in the TextBlock.
The DisplayMemberPath is really for when you have no ItemTemplate.
Or you could remove your ItemTemplate and use DisplayMemberPath - in which case it basically creates a TextBlock for you. Not recomended you do both.

您可以删除 DisplayMemberPath,然后在 TextBlock 中设置路径。
DisplayMemberPath 真正适用于没有 ItemTemplate 的情况。
或者您可以删除您的 ItemTemplate 并使用 DisplayMemberPath - 在这种情况下,它基本上为您创建了一个 TextBlock。不建议你两个都做。

   <TextBlock text="{Binding Path=Name, Mode=OneWay}" 

回答by Emu

You should change the MemberPath="{Binding Name}"to MemberPath="Name". Then it will work.

您应该将 更改MemberPath="{Binding Name}"MemberPath="Name"。然后它会起作用。

回答by JJ_Coder4Hire

Alternatively you don't need to set the DisplayMemberPath. you can just include an override ToString() in your object that is in your PromptList. like this:

或者,您不需要设置 DisplayMemberPath。您可以在 PromptList 中的对象中包含覆盖 ToString()。像这样:

class Prompt {
    public string Name = "";
    public string Value = "";

    public override string ToString() {
        return Name;
    }
}

The ToString() will automatically be called and display the Name parameter from your class. this works for ComboBoxes, ListBoxes, etc.

ToString() 将自动被调用并显示您的类中的 Name 参数。这适用于组合框、列表框等。

回答by rmagnien

Trying this :

试试这个:

<ComboBox Grid.Row="1" Grid.Column="2" ItemsSource="{Binding PromptList}"  SelectedItem="{Binding Prompt}">
<ComboBox.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding Content}"/>
    </DataTemplate>
</ComboBox.ItemTemplate>