WPF - 按钮 IsEnabled 绑定转换器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11957833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 04:58:03  来源:igfitidea点击:

WPF - Button IsEnabled Binding Converter

wpfbuttonbindingconverterisenabled

提问by Anthony

I have an ObservableCollection<MyEntity>and MyEntityhas a IsCheckedproperty with a PropertyChangedevent. I have a Buttonand I would like to change IsEnabledproperty to truewhen at least one of MyEntityof the MyObservableCollectionis checked. I created a converter which takes the ObservableCollectionand return true when a MyEntityis checked at least. But the return "null"is returned. What is wrong ? Thank you for your help.

我有一个ObservableCollection<MyEntity>并且MyEntity有一个IsChecked带有PropertyChanged事件的属性。我有一个Button,我想改变IsEnabled属性true,当至少一个MyEntityMyObservableCollection检查。我创建了一个转换器,它至少在检查ObservableCollectiona 时接受并返回 true MyEntity。但是return "null"被退回了。怎么了 ?感谢您的帮助。

XAML

XAML

<Window.Resources>
    <CollectionViewSource x:Key="MyObservableCollection"/>
    <src:MyConverter x:Key="MyConverter"/>
</Window.Resources>
<Button IsEnabled="{Binding Converter={StaticResource MyConverter}, Source={StaticResource MyObservableCollection}}"/>

C# Converter

C# 转换器

class MyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (null == value)
            return "null";

        ReadOnlyObservableCollection<object> items = (ReadOnlyObservableCollection<object>)value;

        List<MyEntity> myEntities = (from i in items select (MyEntity)i).ToList();

        foreach (MyEntity entity in myEntities)
        {
            if (entity.IsChecked)
            {
                return true;
            }
        }
        return false;
    }

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

采纳答案by LPL

I think your Binding is wrong. The Converter want's the underlying collection not the CollectionView. And set the CollectionViewSource.Sourceafter InitializeComponent(), the Binding will be refreshed.

我认为您的绑定是错误的。转换器想要的是基础集合而不是CollectionView. 并设置CollectionViewSource.Sourceafter InitializeComponent(), Binding 将被刷新。

<Button IsEnabled="{Binding Path=SourceCollection,
                            Converter={StaticResource MyConverter},
                            Source={StaticResource MyObservableCollection}}" />

回答by Rohit Vats

Since StaticResources are resolved at the time of intializing itself i.e. at the time of InitializeComponent()but till that time your collection is yet not intialized that's why null value is passed to the converter. So, better choice would be to move that property in your code behind and bind to that property since binding will be resloved after InitializeComponent(). Create property in your code-behind-

由于 StaticResources 在初始化时解析,即在InitializeComponent()但直到那时您的集合尚未初始化时,这就是将空值传递给转换器的原因。因此,更好的选择是将代码中的该属性移到后面并绑定到该属性,因为绑定将在InitializeComponent(). 在您的代码隐藏中创建属性

public CollectionViewSource MyObservableCollection { get; set; }

and bind to your button -

并绑定到您的按钮 -

    <Button IsEnabled="{Binding MyObservableCollection, RelativeSource=
{RelativeSource AncestorType=Window}, Converter={StaticResource MyConverter}}"/>