如何基于WPF中的列表框选定项绑定stackpanel可见性 [英] How to bind stackpanel visibility based on listbox selected item in WPF

查看:220
本文介绍了如何基于WPF中的列表框选定项绑定stackpanel可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我有ListBox有7项Rainbow颜色和stackpanel与各自的颜色。我需要根据ListBox选择的Item绑定这个堆栈面板的可见性。简单来说,如果我从ListBox中选择Red Color,那么Red Color stackpanel应该是可见的,其他堆栈面板应该会崩溃



我尝试了什么:



Visibility ={Binding Path = SelectedItem,Mode = OneWay,ElementName = ListBox1}

Hi I have ListBox with 7 items of Rainbow colors and stackpanel with respective colors. I need to bind this stack panel visibility based on ListBox selected Item. In simple words if i select Red Color from ListBox then Red Color stackpanel should be visible and rest other stack panels should collapse

What I have tried:

Visibility="{Binding Path=SelectedItem,Mode=OneWay,ElementName=ListBox1}"

推荐答案

绑定应该像这样工作,只是选择项不是System.Windows.Visibility的正确类型 - 所以使用转换器,或者将它作为ViewModel上的(计算)属性(但似乎你不要不使用MVVM - 你知道WPF最适合MVVM吗?)。也许DataTrigger可以在没有转换器的情况下做同样的事情(不是100%舒适,不要为你的情况尝试)
Binding should work like this, just that the selecteditem won't be of the correct type for System.Windows.Visibility - so use a converter, or make it a (calculated) property on your ViewModel (but it seems you don't use MVVM - you know that WPF works best with MVVM?). Maybe a DataTrigger could do the same without a converter (not 100% shure, din't try it for your case)


这篇关于如何基于WPF中的列表框选定项绑定stackpanel可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆