文本不会显示ComboBox中的选定项目

时间:2023-01-19 10:08:38

I feel really stupid for asking this but I have been thrashing for over 8 hours. How do I get the Selected Item to show its text in my WPF combo box when selected?

我觉得这个问题真的很蠢,但是我已经打了八个多小时。如何选中时,如何让选定项目在我的WPF组合框中显示其文本?

文本不会显示ComboBox中的选定项目

Above is an option dialog that allows users to select and configure the available tournament displays. The problem is the selected combo box item shows the UserControl instead of the Display name.

上面是一个选项对话框,允许用户选择和配置可用的锦标赛显示。问题是所选的组合框项目显示UserControl而不是显示名称。

On Window Loaded:

在窗口加载:

        //_displayer is a private member populated using MEF
        //[ImportMany(typeof (IDisplayer))] 
        //private IEnumerable<IDisplayer> _displayers;
        DisplayTypeComboBox.ItemsSource = _displayers;

The ComboBox Xaml:

ComboBox Xaml:

   <ComboBox
     Name="DisplayTypeComboBox"
     Grid.Column="1"
     Grid.ColumnSpan="2"
     Grid.Row="1" 
     IsEditable="False"
     SelectionChanged="DisplayTypeComboBox_SelectionChanged">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <ComboBoxItem Content="{Binding DisplayerName}" />
            </DataTemplate>
        </ComboBox.ItemTemplate>
   </ComboBox>

The IDisplayer:

public interface IDisplayer
{
    string DisplayDataLocation { get; set; }
    string DisplayerName { get; }
    string DisplayerDescription { get;}
    bool WatcherEnabled { get; }
    UserControl View { get; }
    string DisplayerImageLeft { get; set; }
    string DisplayerImageRight { get; set; }
    void Update();
}

4 个解决方案

#1


I encountered the same thing. Took me a while too. :( You should have used the ItemContainerStyle and not ItemTemplate. Because ComboBox wraps the internal items with a ComboBoxItem - you basically wrapped the ComboBoxItem with another one.

我遇到了同样的事情。我也花了一会儿。 :(你应该使用ItemContainerStyle而不是ItemTemplate。因为ComboBox用ComboBoxItem包装内部项 - 你基本上用另一个包装ComboBoxItem。

#2


Check what DisplayerName member actually contains. Most likely it contains the UserControl name instead of the Display name.

检查DisplayerName成员实际包含的内容。它很可能包含UserControl名称而不是Display名称。

#3


Try using a TextBlock to bind to the DisplayerName instead of a ComboboxItem. I believe that when you set the itemsource, the combo control will wrap the items inside comboboxitems controls automatically.

尝试使用TextBlock绑定到DisplayerName而不是ComboboxItem。我相信当你设置itemsource时,组合控件会自动将项目包装在comboboxitems控件中。

Edit: I misunderstood your question. Try setting the SelectionBoxItemTemplate.

编辑:我误解了你的问题。尝试设置SelectionBoxItemTemplate。

#4


I don't even want to think about how many hours I have spent trying to solve what should be a simple problem. Why is it so hard to get your selected text to appear as the selected value? I give up, WPF you have beat me into submission. I changed the control to a list box it takes up more room to display the selectable Items but at least it works.

我甚至不想想我花了多少时间来解决应该是一个简单的问题。为什么难以让所选文本显示为所选值?我放弃了,WPF你已经打败我了。我将控件更改为列表框,它占用了更多空间来显示可选项,但至少它可以工作。

   <ListBox
     Name="DisplayTypeComboBox"
     Grid.Column="1"
     Grid.ColumnSpan="2"
     Grid.Row="1" 
     SelectionChanged="DisplayType_SelectionChanged">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Label Content="{Binding DisplayerName}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

文本不会显示ComboBox中的选定项目

#1


I encountered the same thing. Took me a while too. :( You should have used the ItemContainerStyle and not ItemTemplate. Because ComboBox wraps the internal items with a ComboBoxItem - you basically wrapped the ComboBoxItem with another one.

我遇到了同样的事情。我也花了一会儿。 :(你应该使用ItemContainerStyle而不是ItemTemplate。因为ComboBox用ComboBoxItem包装内部项 - 你基本上用另一个包装ComboBoxItem。

#2


Check what DisplayerName member actually contains. Most likely it contains the UserControl name instead of the Display name.

检查DisplayerName成员实际包含的内容。它很可能包含UserControl名称而不是Display名称。

#3


Try using a TextBlock to bind to the DisplayerName instead of a ComboboxItem. I believe that when you set the itemsource, the combo control will wrap the items inside comboboxitems controls automatically.

尝试使用TextBlock绑定到DisplayerName而不是ComboboxItem。我相信当你设置itemsource时,组合控件会自动将项目包装在comboboxitems控件中。

Edit: I misunderstood your question. Try setting the SelectionBoxItemTemplate.

编辑:我误解了你的问题。尝试设置SelectionBoxItemTemplate。

#4


I don't even want to think about how many hours I have spent trying to solve what should be a simple problem. Why is it so hard to get your selected text to appear as the selected value? I give up, WPF you have beat me into submission. I changed the control to a list box it takes up more room to display the selectable Items but at least it works.

我甚至不想想我花了多少时间来解决应该是一个简单的问题。为什么难以让所选文本显示为所选值?我放弃了,WPF你已经打败我了。我将控件更改为列表框,它占用了更多空间来显示可选项,但至少它可以工作。

   <ListBox
     Name="DisplayTypeComboBox"
     Grid.Column="1"
     Grid.ColumnSpan="2"
     Grid.Row="1" 
     SelectionChanged="DisplayType_SelectionChanged">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Label Content="{Binding DisplayerName}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

文本不会显示ComboBox中的选定项目