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

11
我写了一个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>

当我绑定时,我可以得到正确的结果,但是在转换回来时是否有一种方法可以获取绑定的id?我想要实现的是,如果复选框未被选中,则从列表中删除该值,如果选中,则将该值添加到列表中。


我有一个类似的问题,其中多绑定中的绑定之一是具有包含文本字段的对象列表的对象。多绑定基于其他绑定值绑定到文本框之一。我需要在convertBack时更改对象的文本框文本,但我只有新值,不知道哪个对象的文本需要更改。 - JoeSharp
2个回答

7

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

不要使用 IMultiValueConverterConvertBack 方法,你可以将 IsChecked 绑定设置为单向绑定,并使用 CheckBoxCommand 属性来执行检查逻辑。

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

然后,添加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);
    }

我知道这么做有点绕,但是IMultiValueConverter.ConvertBack方法真的非常无用 - 我想不出在没有访问当前绑定值的情况下它有太多用处。


4
谢谢你的这个方法,对我很有效。花了一些时间才意识到ConvertBack完全没用。他们应该在文档中说明并节省我们所有的时间。 - user169390
完全没用 :( - Asheh

5
我解决了我的问题,希望我的解决方案也能帮助到您。将您拥有的多重绑定,而不是放在IsChecked属性中,放在DataContext属性中。这可能是我们的问题不同之处...在我的转换方法中,我使用绑定中的数据获取一个对象,然后返回myobject.text。我改变了这个过程,改为返回对象本身,以便将其绑定到数据上下文中。然后,我将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>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接