WPF ComboBox ItemTemplate 绑定到字符串集合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26138809/
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 11:21:24  来源:igfitidea点击:

WPF ComboBox ItemTemplate binding to a string collection

wpfxamldata-bindingcombobox

提问by MegaMind

I have a combobox in wpf which is bind to a List<string>. All works well, but now for some reason I need to bind to item template. XAML for combo box is

我在 wpf 中有一个组合框,它绑定到List<string>. 一切正常,但现在由于某种原因我需要绑定到项目模板。组合框的 XAML 是

<ComboBox ItemsSource="{Binding Tracks}" SelectedItem="{Binding SelectedTrack}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding **WhatShouldBeHere**}"></TextBlock>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

If my data source is a custom collection then binding is easy I should just pass the property name from custom collection but as binding source is list of string, what should be the binding property.

如果我的数据源是自定义集合,那么绑定很容易,我应该只传递自定义集合中的属性名称,但由于绑定源是字符串列表,绑定属性应该是什么。

回答by Clemens

It should be

它应该是

<TextBlock Text="{Binding}"/>

which is equivalent to

这相当于

<TextBlock Text="{Binding Path=.}"/>

See the Remarks section on the Binding.PathMSDN page for further details.

有关更多详细信息,请参阅Binding.PathMSDN 页面上的备注部分。