MultiBinding Converter in CheckBox.IsChecked not called

青春壹個敷衍的年華 提交于 2020-01-16 19:11:48

问题


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:


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.



来源:https://stackoverflow.com/questions/16186682/multibinding-converter-in-checkbox-ischecked-not-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!