如果列表框有一个选择项,WPF 会在其他控件上绑定 IsEnabled

发布于 2024-09-03 18:42:38 字数 237 浏览 8 评论 0原文

我有一个包含 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 技术交流群。

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

发布评论

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

评论(3

メ斷腸人バ 2024-09-10 18:42:38

为此,您需要一个ValueConverter这篇文章详细描述了它,但总结是你需要一个实现 IValueConverter 的公共类。在 Convert() 方法中,您可以执行如下操作:

if(!(value is int)) return false;
if(value == -1) return false;
return true;

现在,在 XAML 中,您需要执行以下操作:

<Window.Resources>
    <local:YourValueConverter x:Key="MyValueConverter">
</Window.Resources>

最后,将绑定修改为:

IsEnabled="{Binding myList.SelectedIndex, Converter={StaticResource MyValueConverter}"

您确定不是这个意思

IsEnabled="{Binding ElementName=myList, Path=SelectedIndex, Converter={StaticResource MyValueConverter}"

吗?您不能隐式地将元素的名称放入路径中(我猜除非Window本身就是DataContext)。绑定到 SelectedItem 并检查是否为 null 也可能更容易,但这实际上只是偏好。

哦,如果您不熟悉备用 xmlns 声明,请在 Window 顶部添加,

xmlns:local=

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:

if(!(value is int)) return false;
if(value == -1) return false;
return true;

Now, in your XAML, you need to do:

<Window.Resources>
    <local:YourValueConverter x:Key="MyValueConverter">
</Window.Resources>

And finally, modify your binding to:

IsEnabled="{Binding myList.SelectedIndex, Converter={StaticResource MyValueConverter}"

Are you sure you didn't mean

IsEnabled="{Binding ElementName=myList, Path=SelectedIndex, Converter={StaticResource MyValueConverter}"

though? You can't implicitly put the element's name in the path (unless the Window itself is the DataContext, 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 your Window, add

xmlns:local=

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

廻憶裏菂餘溫 2024-09-10 18:42:38

复制粘贴解决方案:

将此类添加到您的代码中:

public class HasSelectedItemConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value is int && ((int) value != -1);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

将转换器作为 StaticResource 添加到 部分中的 App.xml 中:

<local:HasSelectedItemConverter x:Key="HasSelectedItemConverter" />

现在您可以在 XAML 中使用它:

<Button IsEnabled="{Binding ElementName=listView1, Path=SelectedIndex,
 Converter={StaticResource HasSelectedItemConverter}"/>

Copy-paste solution:

Add this class to your code:

public class HasSelectedItemConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value is int && ((int) value != -1);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Add converter as StaticResource to App.xml in <Application.Resources> section:

<local:HasSelectedItemConverter x:Key="HasSelectedItemConverter" />

And now you can use it in your XAML:

<Button IsEnabled="{Binding ElementName=listView1, Path=SelectedIndex,
 Converter={StaticResource HasSelectedItemConverter}"/>
乖不如嘢 2024-09-10 18:42:38

嗯,也许它可以与 BindingConverter 一起使用,它可以显式转换所有索引 > 0 为真。

Hmm, perhaps it works with a BindingConverter, which converts explicitly all indexes > 0 to true.

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