MultiBinding Converter in CheckBox.IsChecked not c

2019-09-11 01:45发布

I have a custom combobox a multiselectioncombobox if you will,

the thing is the selections depend on an other collection. I tried to bind ComboBox.IsChecked property to MultiBinding Converter but the converter isn't called.

<DataTemplate>
<StackPanel Orientation="Horizontal" x:Name="ItemStack" VirtualizingStackPanel.IsVirtualizing="False">
    <CheckBox x:Name="CheckBoxItem"
        Command="{Binding SelectItem, RelativeSource={RelativeSource AncestorType={x:Type MultiSelectionComboBox}}}"
        CommandParameter="{Binding Key}"
              >
        <CheckBox.IsChecked>
            <MultiBinding Converter="{StaticResource MultiSelectionCommandConverter}" Mode="OneWay">
                <Binding Path="Key"/>
                <Binding Path="SelectedItem"
                         RelativeSource="{RelativeSource AncestorType={x:Type MultiSelectionComboBox}}" />
            </MultiBinding>
        </CheckBox.IsChecked>
    </CheckBox>
    <TextBlock Text="{Binding DisplayText}"></TextBlock>
</StackPanel>
</DataTemplate>

and the converter is,

public class MultiSelectionCommandConverter : IMultiValueConverter 
{      
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {   
            ///stuff to do...
    }

    public object[] ConvertBack(object values, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        return null;
    }
}

any suggestions?

1条回答
趁早两清
2楼-- · 2019-09-11 01:58

After trying out possibilities, I've found a work around. Still I'm not quite sure why this might work and the other won't.

I've changed my xaml to pass the whole object instead of the property. So the code looked liked this,

<DataTemplate>
<StackPanel Orientation="Horizontal" x:Name="ItemStack" VirtualizingStackPanel.IsVirtualizing="False">
    <CheckBox x:Name="CheckBoxItem"
        Command="{Binding SelectItem, RelativeSource={RelativeSource AncestorType={x:Type MultiSelectionComboBox}}}"
        CommandParameter="{Binding Key}"
              >
        <CheckBox.IsChecked>
            <MultiBinding Converter="{StaticResource MultiSelectionCommandConverter}" Mode="OneWay">
                <Binding Path="Key"/>
                <Binding 
                         RelativeSource="{RelativeSource AncestorType={x:Type MultiSelectionComboBox}}" />
            </MultiBinding>
        </CheckBox.IsChecked>
    </CheckBox>
    <TextBlock Text="{Binding DisplayText}"></TextBlock>
</StackPanel>
</DataTemplate>

and the converter is

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
    string key = (string)values[0];
    ObservableCollection<ListItem> selectedItems = (values[1] as MultiSelectionComboBox).SelectedItem;
    //do stuff
    return false;
}

This is definitely not a desired solution but, this will do until i figure out the other reason.

查看更多
登录 后发表回答