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

发布于 2024-08-21 04:33:12 字数 898 浏览 5 评论 0原文

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

I've written a MultiValueConverter which checks if a given list contains a given value and returns true if it does. I use it for binding to custom checkbox list. Now I'd like to write ConvertBack method so that if checkbox was checked, original value would be sent to the model. Is there a way to access values in ConvertBack method?

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>

I get correct results when I'm binding but is there a way to get the bound id when converting back? What I would like to achieve is that if checkbox is unchecked, the value would be removed from the list and if it is checked, the value would be added to the list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落花随流水 2024-08-28 04:33:12

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

您可以将 IsChecked 绑定设置为 OneWay 并使用,而不是使用 IMultiValueConverterConvertBack 方法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 方法确实毫无用处 - 我想不出太多使用它而无需访问当前的绑定值。

I know this is an old issue, but this solution might help someone else.

Instead of using the the ConvertBack method of the IMultiValueConverter, you can set the IsChecked binding to be OneWay and use the CheckBox Command property to perform the check logic.

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

Then, add the CheckBoxCommand which performs something similar to this:

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

I know it's a slightly round-about way of doing this but the IMultiValueConverter.ConvertBack method is really quite useless - I can't think of too many uses for it without having access to the current binding values.

ヤ经典坏疍 2024-08-28 04:33:12

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

I solved my problem, and hopefully the solution will help you too. Take the multibinding you have, and instead of putting it in the IsChecked attribute, put it in the DataContext attribute. This may be where our issues differ... in my convert method, i was using the data in the bindings to grab an object, and then i returned myobject.text. I changed this to instead return just the object, so that it gets bound to the datacontext. I then bound the textbox.text property to the text property of myobject. it seems to work fine. You could then bind the list where values are removed to the checkbox.ischecked... I guess. I'm not exactly sure what you're trying to do.

I'm thinking this might get you on the right path...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文