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


我编写了一个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吗?我想要实现的是,如果取消选中复选框,则将从列表中删除该值,如果选中该值,则该值将添加到列表中。


5359
2018-02-10 17:53


起源

我有一个类似的问题,其中我在multibinding中的一个绑定是一个对象,其中包含一个包含文本字段的对象列表。 multibinding基于其他绑定值绑定到其中一个文本框。我需要文本框来更改convertBack上对象的文本,但我所拥有的只是新值,而不是对象的文本需要更改 - JoeSharp


答案:


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

而不是使用 ConvertBack 的方法 IMultiValueConverter,你可以设置 IsChecked 绑定是 OneWay 并使用 CheckBox  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>

然后,添加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 方法真的很无用 - 如果没有访问当前绑定值,我无法想到它有太多用途。


6
2018-02-14 00:15



感谢这种方法,这对我有用。我花了一些时间才意识到ConvertBack完全没用。他们应该把它放在文档中并节省我们所有的时间。 - Jan
是的完全没用:( - Asheh


我解决了我的问题,希望解决方案也能帮到你。进行多重绑定,而不是将其放在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>


5
2018-05-20 20:59