0

I have a CheckComboBox in a toolbar defined as follows:

<xctk:CheckComboBox
    x:Name="cboColumns"
    Width="75"
    Command="{x:Static self:CustomCommands.ChangeColumn}"
    DisplayMemberPath="Header"
    ValueMemberPath="Header"
    SelectedMemberPath="Visibility"
    IsEditable="False">                 
</xctk:CheckComboBox>

The ItemsSource is set as follows:

Dim ColList As New ObservableCollection(Of MemberColumn)
With ColList
    .Add(New MemberColumn With {.Header = "Last Name", .Visibility = True})
    .Add(New MemberColumn With {.Header = "First Name", .Visibility = True})
    .Add(New MemberColumn With {.Header = "Middle Name", .Visibility = False})
    <numerous additional items>
End With
cboColumns.ItemsSource = ColList

where MemberColumn is Class with just two properties:

Public Property Header As String
Public Property Visibility As Boolean

For debugging purposes, I defined the ChangeColumn command as:

Private Sub ChangeColumn(sender As Object, e As ExecutedRoutedEventArgs)
    Debug.WriteLine(DirectCast(cboColumns.SelectedItem, MemberColumn).Header)
End Sub

I expected the debug statement to write the Header of the SelectedItem but it always just writes "Last Name" no matter which item in the list I check/uncheck.

Is this a bug or am I doing something wrong?

Edit 1

I think I just raised the XY Problem so let me explain what I am trying to do so we can deal with the real problem.

I have a datagrid with numerous columns and I want to give the user the ability to select which columns are shown in the grid. To do this, I have a CheckComboBox (x:Name=cboColumns) in a toolbar that lists all the columns in the grid. The idea is that only the columns that are checked in the ComboBox will be visible in the grid.

Here is how I am trying to bind each column's Visibility property:

Visibility="{Binding ElementName=cboColumns, Path=SelectedValue}"

but this raises an error: System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element.

I think that the problem is that the ComboBox is outside the namescope of grid columns so the binding can't "see" it. If that is the case, how to proceed?

Edit 2

I think I solved the namescope problem by changing the binding to:

Visibility="{Binding Source={x:Reference cboColumns}, Path=SelectedValue}"

but that raises this error:

'TargetDefaultValueConverter' converter failed to convert value ''

Note that I changed my Visibility property to:

Public Property Visibility As Visibility

to avoid that error but that fix obviously doesn't work.

SezMe
  • 527
  • 8
  • 24
  • Try Binding to a `SelectedItem` instead of `ValueMemeberPath`. – XAMlMAX Dec 22 '17 at 08:22
  • @XAMIMAX As the debug statement in my OP shows, the SelectedItem is not actually returning the SelectedItem so binding to it will not fix the problem. – SezMe Dec 22 '17 at 16:29
  • Instead of `SelectedMemberPath="Visibility"` and `DisplayMemberPath="Header"` use `SelectedItem="{Binding }"`. That's what I meant, sorry if that wasn't clear. – XAMlMAX Dec 22 '17 at 23:38
  • @XAMIMAX See my edit to the OP. – SezMe Dec 23 '17 at 22:41

1 Answers1

0

It is not a bug. Xceed confirms that SelectedItem does NOT return what you might ordinarily expect, namely, the most recent item selected in the ComboBox. Rather, it always returns the first checked item no matter what was checked or unchecked...."by design".

SezMe
  • 527
  • 8
  • 24