WPF:有没有办法在MultiValueConverter的ConvertBack方法中获取原始值?

aks*_*aks 11 wpf xaml multibinding imultivalueconverter

我编写了一个MultiValueConverter,它检查给定列表是否包含给定值,如果有,则返回true.我用它来绑定自定义复选框列表.现在我想编写ConvertBack方法,这样如果选中复选框,原始值将被发送到模型.有没有办法在ConvertBack方法中访问值?

XAML:

<ListBox.ItemTemplate>
    <HierarchicalDataTemplate>
        <CheckBox Content="{Binding Path=Description}">
            <CheckBox.IsChecked>
                <MultiBinding Converter="{x:Static Classes:ListContainsMultiConverter.Instance}">
                    <Binding Path="Id" />
                    <Binding Path="DataContext.ContactTypes" RelativeSource="{RelativeSource AncestorType={x:Type Window}}" />
                </MultiBinding>
            </CheckBox.IsChecked>
        </CheckBox>
    </HierarchicalDataTemplate>
</ListBox.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)

当我绑定时,我得到了正确的结果但是有没有办法在转换回来时获得绑定的id?我想要实现的是,如果取消选中复选框,则将从列表中删除该值,如果选中该值,则该值将添加到列表中.

Kai*_*i G 6

我知道这是一个老问题,但这个解决方案可能会帮助其他人.

您可以将绑定设置为并使用该属性来执行检查逻辑,而不是使用该ConvertBack方法.IMultiValueConverterIsCheckedOneWayCheckBox Command

<ListBox.ItemTemplate>
    <HierarchicalDataTemplate>
        <CheckBox Content="{Binding Path=.}" Command="{Binding Path=CheckBoxCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" CommandParameter="{Binding Path=.}">
            <CheckBox.IsChecked>
                <MultiBinding Converter="{x:Static Classes:ListContainsMultiConverter.Instance}" Mode="OneWay">
                    <Binding Path="." />
                    <Binding Path="SelectedItems" RelativeSource="{RelativeSource AncestorType={x:Type Window}}" />
                </MultiBinding>
            </CheckBox.IsChecked>
        </CheckBox>
    </HierarchicalDataTemplate>
</ListBox.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)

然后,添加CheckBoxCommand,它执行类似于此的操作:

    // the items bound to your checkbox
    public Collection<string> Items { get; set; }

    // the list of selected items
    public Collection<string> SelectedItems { get; set; }

    private void ExecuteCheckBoxCommand(string obj)
    {
        SelectedItems.Add(obj);
    }
Run Code Online (Sandbox Code Playgroud)

我知道这是一种略微圆润的方式,但这种IMultiValueConverter.ConvertBack方法实在没用 - 我无法想到它有太多的用途而无法访问当前的绑定值.

  • 感谢这种方法,这对我有用.我花了一些时间才意识到ConvertBack完全没用.他们应该把它放在文档中并保存我们一段时间. (3认同)

Joe*_*arp 5

我解决了我的问题,希望解决方案也能帮到你.进行多重绑定,而不是将其放在IsChecked属性中,将其放在DataContext属性中.这可能是我们的问题不同的地方......在我的转换方法中,我使用绑定中的数据来获取对象,然后我返回了myobject.text.我将其更改为仅返回对象,以便将其绑定到datacontext.然后我将textbox.text属性绑定到myobject的text属性.它似乎工作正常.然后,您可以将删除值的列表绑定到checkbox.ischecked ...我猜.我不确定你要做什么.

我想这可能会让你走上正确的道路......

<ListBox.ItemTemplate>
<HierarchicalDataTemplate>
    <CheckBox Content="{Binding Path=Description}">
        <CheckBox.DataContext>
            <MultiBinding Converter="{x:Static Classes:ListContainsMultiConverter.Instance}">
                <Binding Path="Id" />
                <Binding Path="DataContext.ContactTypes" RelativeSource="{RelativeSource AncestorType={x:Type Window}}" />
            </MultiBinding>
        </CheckBox.DataContext>
        <CheckBox.IsChecked>
            <Binding Path="Some_Property_For_IsChecked_In_Some_Object_In_The_Converter" />
        </CheckBox.IsChecked>
    </CheckBox>
</HierarchicalDataTemplate>
Run Code Online (Sandbox Code Playgroud)