1

I am trying to change the text of a WPF combobox button to something custom that isn't an actual selected item. The combobox control is setup with a custom datatemplate that allows it to contain checkboxes and what I'm trying to do is display "None Selected", [SelectedItem.Text] or "Multiple Selected" depending on whether zero items are checked, one is checked or more than one is checked. I found one solution on here that involved adding a new textblock instance that could display this text and then set the text of it to what I want to display. This works great until someone clicks and area next to the label of the checkbox and the text of that item shows up underneath my custom textblock causing weird overlap issues.

My assumption is a converter of some kind (which can replace the custom textblock as far as I'm concerned - no preference there), but I'm not entirely sure how to apply it. Here is my XAML thus far:

<Grid Grid.Row="4" Grid.Column="1">
     <Grid.RowDefinitions>
          <RowDefinition Height="Auto" />
          <RowDefinition Height="Auto" />
     </Grid.RowDefinitions>
<ComboBox x:Name="SubjectMatterList" Style="{StaticResource ComboBox}" ItemsSource="{Binding SubjectMatters}" Visibility="{Binding AdjunctListVisibility}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" MinWidth="125" MaxWidth="125" Margin="6">
                <CheckBox Content="{Binding Name}">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Checked">
                            <mvvm:EventToCommand Command="{Binding DataContext.SubjectMatterSelectedCommand, ElementName=GradeLevelList}" CommandParameter="{Binding}" />
                        </i:EventTrigger>
                        <i:EventTrigger EventName="Unchecked">
                            <mvvm:EventToCommand Command="{Binding DataContext.SubjectMatterDeselectedCommand, ElementName=GradeLevelList}" CommandParameter="{Binding}" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </CheckBox>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
<TextBlock IsHitTestVisible="False" x:Name="SelectedSubjectMatter" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10" Padding="10,6,0,6" Text="{Binding ComboBoxSubjectMatterText}" Visibility="{Binding AdjunctListVisibility}" />
<TextBlock Text="{Binding SelectedSubjectMatterText}" Grid.Row="1" Margin="10" Visibility="{Binding SubjectMatterSelectedVisibility}" />

Scott Salyer
  • 2,165
  • 7
  • 45
  • 82
  • 3
    Sounds like control-abuse to me... – H.B. Jul 26 '11 at 02:49
  • How do you figure? It can't be that uncommon to show a dropdown with those types of options. – Scott Salyer Jul 26 '11 at 04:16
  • That is exactly what i would claim: Pretty much no-one uses a ComboBox like that, since it hardly makes sense. – H.B. Jul 26 '11 at 04:18
  • Well, this SO link suggests otherwise (as do a number of others): http://stackoverflow.com/questions/2901536/can-a-wpf-combobox-display-alternative-text-when-its-selection-is-null. That is where I got the original idea for the extra TextBlock entry (last response). That aside, if you think no one ever uses a ComboBox like that, then what do you recommend? And why doesn't it make sense? There are a multitude of examples online of people building checkbox-based ComboBoxes so my idea for this isn't exactly original. – Scott Salyer Jul 26 '11 at 04:30
  • Problems: 1. The state is only observable via a click (unless none or only one item is selected) 2. Changing just one option also requires an additional click and the item has to be targeted in the list (if keyboard input is not supported). | What i would suggest? An expander containing a list of checkboxes (created via `ItemsControl`), leaves control with the user, less of pain in terms of interaction. – H.B. Jul 26 '11 at 04:39
  • Unfortunately I don't think an expander is an option. The reason I went down the ComboBox path is due to screen real estate. I have to support older systems so I'm planning on a minimum of 1024x768. Since the total number of ComboBoxes on this screen that do this exact functionality is 4 I don't have enough room to show all the options. I had originally used a ListBox and even with two columns I ran out of space. – Scott Salyer Jul 26 '11 at 04:52
  • Then using a separate modeless dialogue might be worth considering. – H.B. Jul 26 '11 at 04:57
  • The UI around the items in question is pretty minimal (no ribbon, toolbar, etc.) so a dialog isn't going to get me past the restrictions on height. – Scott Salyer Jul 26 '11 at 05:00

1 Answers1

2

http://blogs.microsoft.co.il/blogs/justguy/archive/2009/01/19/wpf-combobox-with-checkboxes-as-items-it-will-even-update-on-the-fly.aspx

Wallstreet Programmer
  • 9,567
  • 3
  • 37
  • 52
  • 1
    That's actually the link I originally started with but I never could get his example working. I might give it another shot now though since it isn't super late and maybe I can be more coherent. Thanks! – Scott Salyer Jul 26 '11 at 20:01