开发者

WPF - Combobox with a Prompt

开发者 https://www.devze.com 2022-12-09 21:29 出处:网络
I\'m looking for a way to add a \"SELECT An Item\" to a combobox that does not have an item 开发者_如何学JAVAselected. This would be different than a selected item as a default.

I'm looking for a way to add a "SELECT An Item" to a combobox that does not have an item 开发者_如何学JAVAselected. This would be different than a selected item as a default.

I want the combobox to say "SELECT an Item" and be bound to one list for the possible selections and another model for the selected item.

I'd prefer a style that I can apply to multiple comboboxes and have a way to set the prompt. I've seen something similiar at http://marlongrech.wordpress.com/2008/03/09/input-prompt-support-in-wpf/, but the it does not work smoothly, requiring 2 clicks to get to the list.

Thanks!


You could use the adorner solution you linked to with a couple of changes, or you could do this with a style and converter.

Adorner solution

The adorner solution is more complex, but has a better interface and encapsulation. The changes you would need to make are straightforward but possibly difficult if you're not a WPF expert. They are:

  1. Recognize ComboBox as another special case (like TextBox). Subscribe to its SelectedItemChanged, and update adorner visibility using SelectedItem==null.

  2. Don't handle input events (HitTestVisible=False, Focusable=False, etc)

In this case, your ComboBox style will be very simple, just setting the attached property.

Style and converter

Doing it with a style and converter may be simpler for you. Here is the body of the converter:

  object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  {
    return value==null ? Visibility.Visible : Visibility.Hidden;
  }

Your style will replace the default ComboBox style and contain a copy of the ControlTemplate from the theme, wrapped with something like this (or use an adorner):

  <Style TargetType="{x:Class ComboBox}">
    <Style.Setters>
      <Setter Property="local:MyInputPromptClass.PromptText" Value="SELECT an item" />
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Class ComboBox}">
            <Grid>
              ... existing XAML from theme ControlTemplate ...
              <TextBlock
                Text="{Binding local:MyInputPromptClass.PromptText, RelativeSource={RelativeSource TemplatedParent}}"
                Visibility="{Binding SelectedItem, Converter={x:Static local:MyInputPromptClass.Converter}, RelativeSource={RelativeSource TemplatedParent}}"
                HitTestVisible="False" Focusable="False"
                HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
            </Grid>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style.Setters>
  </Style>

This solution is less satisfying than the other, since by copying the default ComboBox template from a theme you end up with an app that doesn't track the current Windows theme. It's possible to get around this using multiple ControlTemplates along with StaticResource and some tricky binding, but at that point I would recommend just using the adorner and attached property.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号