views:

28

answers:

2

This is what I want:

  • There is a combo-box column bound to the ApplicationKey property of ClassA.
  • ClassA.ApplicationKey is a Nullable<Int32>
  • The combo-box is populated with ApplicationTokens from a static function all.
  • An ApplicationToken has a ApplicationName and ApplicationKey property
  • When an item is selected in the drop-down, the ClassA.ApplicationKey property is set to the ApplicationToken.ApplicationKey on the selected item.
  • The "None" option is currently represented by a Null. This can be changed.

Current code

<DataGridComboBoxColumn 
    Header="Application" 
    SelectedValueBinding="{Binding ApplicationKey}"
    SelectedValuePath="ApplicationKey" 
    DisplayMemberPath="ApplicationName" 
    ItemsSource="{Binding Source={x:Static app:ApplicationLookup.GetAllOrNone}}"/>

Currently the binding works, except that I cannot select the "None" item from the list. The combobox shows it, but doesn't do anything when I try to select it with the mouse.

What is the standard way to offer none in a bound combo-box?

A: 

I don't know if this is the standard way of doing things but it seems to work:

  • All ApplicationTokens inherit from Token
  • Token has a "PrimaryKey" property.
  • There is a NullToken class defined as such:

    Public Class NullToken Inherits Token

    Private ReadOnly m_DisplayValue As String
    
    
    Private Sub New(ByVal displayValue As String)
        m_DisplayValue = displayValue
    End Sub
    
    
    Public Overrides Function ToString() As String
        Return m_DisplayValue
    End Function
    
    
    Public Overrides ReadOnly Property PrimaryKey As Integer?
        Get
            Return Nothing
        End Get
    End Property
    
    
    Public Shared ReadOnly BlankToken As New NullToken("")
    Public Shared ReadOnly NoneToken As New NullToken("None")
    Public Shared ReadOnly AllToken As New NullToken("All")
    

    End Class

  • ApplicationLookup.GetAllOrNone returns a collection of Token with the correct NullToken as the first item.

Jonathan Allen
A: 

What I have done when I needed a None or (Select All) type of user gesture for a comboBox is create some static value for the token and just bind to a collection that includes the token in the first position. Then account for it in whatever is handling the change in value:

    public string MidfixText {
        get { return _midfixText; }
        set {
            ...
            _filter(!_midfixText.Equals(Strings.ProjectSelection_MidfixChoice_SelectAll));
        }
    }

HTH,
Berryl

Berryl