开发者

Combobox with default value that is not a selection

Is there a way to have a default value in a ComboBox in WPF? I do not want this value to be somethi开发者_StackOverflow社区ng in my list.

Is there a way to asign a value to the index -1, then it may make it the default value.

How do I proceed?

Thanks for your help


You cannot assign a default values to the Selected properties, as djacobson said, however, you could always use index 0 as the default value, and then add the rest of the collection. If that's not what your looking for, you can change the ComboBox's value so that it takes the default value you wish to put, and add a button or a CheckBox so that the user can reset to the ComboBox with the default value.

I think that is the simplest way to do this with the tools that are currently available in .net as of now


It's relatively simple to add the functionality, you can inherit the control. Setting SelectedItem = null will change selectedIndex and selectedValue as you'd expext.

Then you can also access the Text property, to directly set the combo-box's default text.

// sample code snippet
public NewDropDown()
{
    this.SelectionChanged += NewDropDown_SelectionChanged;
    this.Loaded += NewDropDown_Loaded;
}

void NewDropDown_SelectionChanged( object sender, SelectionChangedEventArgs e )
{
    if ( e.AddedItems.Count == 0 && this.SelectedItem == null )
    {
        this.Text = "Please Select..";
    }
}

This will set the text when there is no selected value. It will not appear as a selectable option in the list, to get around this you may either inject null as a value in the list (as @DanJ says) or I've added a button to the controltemplate which nulls the combobox.

Though I haven't really looked into it and can't see a ItemsSourceChanged event, their may be an event in the control which would let you inject null into every combo-box, saving you the hassle in your ViewModels.


I have solved it for myself by just modifying ComboBox template.

The Template contains <ContentPresenter x:Name="ContentPresenter" .. > that displays SelectedItem string on the CBox Button, or nothing if SelectedIndex is -1 = nothing selected = CBox default value. You can just add another ContentPresenter, with different Name, Content bound to CBox 'Text' property and Visibility set to 'Hidden'. Then add Trigger that will hide the standard Presenter and show the Default text Presenter, if SelectedIndex is -1.

<ContentPresenter x:Name="ContentPresenter"
    Content="{TemplateBinding SelectionBoxItem}" />

<ContentPresenter x:Name="DefaultTextContentPresenter"
    Content="{TemplateBinding Text}"  
    Visibility="Hidden" />

<ControlTemplate.Triggers>
    <Trigger Property="SelectedIndex" Value="-1">
        <Setter TargetName="ContentPresenter" Property="Visibility" Value="Hidden"/>
        <Setter TargetName="ContentPresenter_DefaultText" Property="Visibility" 
            Value="Visible"/>
    </Trigger>
</ControlTemplate.Triggers>

Then just set CBox Text value to your default text. You can of course bind or set the Default text Presenter to any value or property you like.
This is in my humble opinion easier then subclassing Cbox and adding code.
This approach is usable in lot of other situations as well.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜