1

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?

Diplay Options

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.

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

4 回答 4

1

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.

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

alt text

于 2009-05-14T18:18:51.113 回答
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.

于 2009-06-11T15:05:08.680 回答
0

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.

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

于 2009-05-11T18:41:09.520 回答
0

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

于 2009-05-11T18:54:48.627 回答