【问题标题】:WPF: Is there a way to get original values in ConvertBack method of MultiValueConverter?WPF:有没有办法在 MultiValueConverter 的 ConvertBack 方法中获取原始值?
【发布时间】:2011-01-15 09:33: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 上对象的文本,但我所拥有的只是新值,而不是需要更改的对象文本

标签: wpf xaml multibinding imultivalueconverter


【解决方案1】:

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

您可以将IsChecked 绑定设置为OneWay 并使用CheckBox Command 属性来执行检查逻辑,而不是使用IMultiValueConverterConvertBack 方法。

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

【讨论】:

  • 感谢这种方法,它对我有用。我花了一些时间才意识到 ConvertBack 完全没用。他们应该把它放在文档中,为我们节省一些时间。
  • 是的,完全没用:(
【解决方案2】:

我解决了我的问题,希望该解决方案也能对您有所帮助。使用您拥有的多重绑定,而不是将其放在 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 2015-12-16
    • 2011-02-04
    • 2010-09-26
    • 2011-10-27
    相关资源
    最近更新 更多