如果列表框有一个选择项,WPF 会在其他控件上绑定 IsEnabled
我有一个包含 2 列的网格,第 0 列中有一个列表框,主网格第 1 列的辅助网格中有许多其他控件。
我希望只有在选择了某个项目时才启用(或可能可见)此控件通过绑定的列表框。我尝试了组合框:
IsEnabled="{Binding myList.SelectedIndex}"
但这似乎不起作用。
我错过了什么吗?像这样的东西应该起作用吗?
谢谢
I have a grid with 2 columns, a listbox in column 0 and a number of other controls in the a secondary grid in the main grids column 1.
I want this controls only to be enabled (or perhaps visible) if an items is selected in the listbox through binding. I tried on a combo box:
IsEnabled="{Binding myList.SelectedIndex}"
But that does not seem to work.
Am I missing something? Should something like this work?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为此,您需要一个
ValueConverter
。 这篇文章详细描述了它,但总结是你需要一个实现 IValueConverter 的公共类。在 Convert() 方法中,您可以执行如下操作:现在,在 XAML 中,您需要执行以下操作:
最后,将绑定修改为:
您确定不是这个意思
吗?您不能隐式地将元素的名称放入路径中(我猜除非
Window
本身就是DataContext
)。绑定到 SelectedItem 并检查是否为 null 也可能更容易,但这实际上只是偏好。哦,如果您不熟悉备用
xmlns
声明,请在Window
顶部添加,VS 将提示您各种可能性。您需要找到与放置您创建的值转换器的名称空间相匹配的名称空间。
You'll need a
ValueConverter
for this. This article describes it in detail, but the summary is you need a public class that implements IValueConverter. In the Convert() method, you could do something like this:Now, in your XAML, you need to do:
And finally, modify your binding to:
Are you sure you didn't mean
though? You can't implicitly put the element's name in the path (unless the
Window
itself is theDataContext
, I guess). It might also be easier to bind to SelectedItem and check for not null, but that's really just preference.Oh, and if you're not familiar with alternate
xmlns
declarations, up at the top of yourWindow
, addand VS will prompt you for the various possibilities. You need to find the one that matches the namespace you put the valueconverter you made in.
复制粘贴解决方案:
将此类添加到您的代码中:
将转换器作为 StaticResource 添加到
部分中的 App.xml 中:现在您可以在 XAML 中使用它:
Copy-paste solution:
Add this class to your code:
Add converter as StaticResource to App.xml in
<Application.Resources>
section:And now you can use it in your XAML:
嗯,也许它可以与 BindingConverter 一起使用,它可以显式转换所有索引 > 0 为真。
Hmm, perhaps it works with a BindingConverter, which converts explicitly all indexes > 0 to true.