0

如果你愿意,我有一个自定义组合框一个多选组合框,

问题是选择取决于其他集合。我尝试将 ComboBox.IsChecked 属性绑定到 MultiBinding Converter,但未调用该转换器。

<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>

转换器是,

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;
    }
}

有什么建议么?

4

1 回答 1

0

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.

于 2013-04-25T05:31:14.943 回答