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?我想要实现的是,如果未选中复选框,则该值将从列表中删除,如果选中,则该值将添加到列表中。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道这是一个老问题,但这个解决方案可能对其他人有帮助。
您可以将
IsChecked
绑定设置为OneWay
并使用,而不是使用IMultiValueConverter
的ConvertBack
方法CheckBox
Command
属性来执行检查逻辑。然后,添加执行与此类似的操作的 CheckBoxCommand:
我知道这是一种稍微迂回的方法,但是
IMultiValueConverter.ConvertBack
方法确实毫无用处 - 我想不出太多使用它而无需访问当前的绑定值。I know this is an old issue, but this solution might help someone else.
Instead of using the the
ConvertBack
method of theIMultiValueConverter
, you can set theIsChecked
binding to beOneWay
and use theCheckBox
Command
property to perform the check logic.Then, add the CheckBoxCommand which performs something similar to this:
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.我解决了我的问题,希望该解决方案也能帮助您。使用您拥有的多重绑定,不要将其放入 IsChecked 属性中,而是将其放入 DataContext 属性中。这可能是我们的问题不同的地方......在我的转换方法中,我使用绑定中的数据来获取对象,然后返回 myobject.text。我将其更改为仅返回对象,以便它绑定到数据上下文。然后,我将 textbox.text 属性绑定到 myobject 的 text 属性。看起来效果很好。然后,您可以将删除值的列表绑定到 checkbox.ischecked...我猜。我不太确定你想做什么。
我想这可能会让你走上正确的道路......
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...