简体   繁体   English

如何将ComboboxItem文本绑定到Combobox文本?

[英]How to Bind ComboboxItem Text to Combobox Text?

This is a ComboBox ItemTemplate : 这是一个ComboBox ItemTemplate

<DataTemplate x:Key="DataTemplate2">
    <Grid Background="AliceBlue" Height="30">
        <TextBlock Text="{Binding Content}" Foreground="Lime"></TextBlock>  
    </Grid>
</DataTemplate>

I want to use Texblock.TextTrimming at the TextBlock above. 我想在上面的TextBlock使用Texblock.TextTrimming This TextBlock will be the ComboBoxItem at screen, but I don't know what to put at TextBlockText . TextBlock将在屏幕上显示为ComboBoxItem ,但我不知道在TextBlockText放置什么。 What should I bind? 我应该绑定什么?

I usually see examples where they put: Text={Binding CustomClass.CustomProperty} , but I need to make it generic. 我通常会看到放置它们的示例: Text={Binding CustomClass.CustomProperty} ,但是我需要使其通用。 So it will work for every ComboBox of the application. 因此它将适用于应用程序的每个ComboBox

What can I put at Text? 我可以在Text上输入什么?

Please, I am trying this for 4 hours. 请,我正在尝试4个小时。

If tou want it to be generic just use {Binding} 如果您希望它是通用的,请使用{Binding}

Example: 例:

 <Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="130" Width="167" Name="UI" >
    <Grid >
        <ComboBox ItemsSource="{Binding ElementName=UI, Path=Items}"  Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" >
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <Grid Background="AliceBlue">
                        <TextBlock Text="{Binding}" Foreground="Lime"/>
                    </Grid>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </Grid>
</Window>

Code: 码:

public partial class MainWindow : Window
{
    public MainWindow() 
    {
        InitializeComponent();
        Items.Add("Stack");
        Items.Add("Overflow");
    }

    private ObservableCollection<string> _items = new ObservableCollection<string>();
    public ObservableCollection<string> Items
    {
        get { return _items; }
        set { _items = value; }
    }
}

Result: 结果:

在此处输入图片说明

And if you are using a custom object you can just override the ToString method so it displays what you want in the combobox 而且,如果您使用的是自定义对象,则可以覆盖ToString方法,以便在组合框中显示所需的内容

Example

public partial class MainWindow : Window 
{
    public MainWindow()
    {
        InitializeComponent();
        Items.Add(new CustomObject { Name = "Stack" });
        Items.Add(new CustomObject { Name = "Overflow" });
    }

    private ObservableCollection<CustomObject> _items = new ObservableCollection<CustomObject>();
    public ObservableCollection<CustomObject> Items
    {
        get { return _items; }
        set { _items = value; }
    }

}

public class CustomObject
{
    public string Name { get; set; }

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

Xaml: Xaml:

Same as above 同上

Result: 结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过单击ComboBoxItem设置ComboBox文本 - Set ComboBox text by clicking on ComboBoxItem 将ComboBoxItem绑定到DataGrid中的ComboBox - Bind ComboBoxItem to ComboBox in DataGrid WPF-C#-ComboBox / ComboBoxItem:如何拉伸文本以填充组合框区域 - WPF - C# - ComboBox/ComboBoxItem: How to stretch text to fill combobox area 如何从Windows运行时中带有C#的组合框中获取文本而不是comboboxitem的名称? - How do I get the text, not the name of the comboboxitem, from a combobox with C# in Windows Runtime? 选择特定的ComboBoxItem时,如何将可编辑的ComboBox的文本更改为其他内容? - How to change the editable ComboBox's text to something else when a specific ComboBoxItem is selected? 具有值和文本的ComboBoxItem - ComboBoxItem with both Value and Text 将文本绑定到组合框中的选定项目 - Bind text to selected item in combobox 如何将ComboBoxItem标头绑定到Combobox控件的同级ListView的项目数 - How to bind ComboBoxItem header to the sibling ListView's items count of Combobox control WPF Combobox CompositeCollection绑定ComboboxItem内容和字符串列表 - WPF Combobox CompositeCollection bind ComboboxItem Content and list of strings 如何指定组合框使用特定的ComboBoxItem模板 - How to specify that a combobox use a specific ComboBoxItem Template
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM