WPF多绑定/聚合绑定到集合

时间:2018-05-30 17:42:25

标签: wpf xaml data-binding

我有一个Control,如果另一个控件没有 visibile 子元素,我想自动消失。我不知道如何实现这一点。我觉得我需要创建一个绑定,为每个子元素的visible属性返回绑定,然后将它们聚合到MultiValueConverter中。我认为这是有效的,但似乎当我将项目添加到我的集合中时,集合绑定不会被重新评估。有人曾经这样做过吗?

以下是我的代码:

    <Grid.Resources>
        <local:BindingExpander x:Key="BindingExpander"/>
        <local:TestConverter x:Key="TestConverter" />
    </Grid.Resources>

    <Button Content="Button" HorizontalAlignment="Left" Margin="237,166,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click">
        <Button.Visibility>
            <MultiBinding Converter="{StaticResource TestConverter}">
                <Binding ElementName="lstItems" Path="Items" Converter="{StaticResource BindingExpander}" ConverterParameter="Visibility"/>
            </MultiBinding>
        </Button.Visibility>
    </Button>

    <ListBox x:Name="lstItems" HorizontalAlignment="Left" Height="100" Margin="601,130,0,0" VerticalAlignment="Top" Width="100" DisplayMemberPath="Content"/>

public class TestConverter : IMultiValueConverter {
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {
        var ret = Visibility.Collapsed;

        foreach (var item in values) {
            if(item is IEnumerable IE) {
                foreach (var Child in IE) {

                }
            }
        }

        return ret;
    }

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

public class BindingExpander : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
        var ret = new List<Binding>();
        if(value is IEnumerable IE) {
            foreach (var item in IE) {
                ret.Add(new Binding(parameter.ToString()) {
                    Source = item,
                    Mode = BindingMode.OneWay
                });
            }
        }

        return ret;
    }

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

````

1 个答案:

答案 0 :(得分:1)

  

我有一个控件,如果是另一个我想要自动消失   控制没有可见的孩子..

只需创建一个布尔属性,该属性报告其他控件绑定的状态,例如:

public bool HasItems { get { return _SomeArray?.Any(); }}

此属性可以根据需要精心制作,但会显示上面的基本示例。

然后将相关控件的可见性标记绑定到HasItems

请注意,HasItems没有INotifyPropertyChanged的管道。在将项目添加到_SomeArray的代码中,只需调用PropertyChanged("HasItems")

在我的博客上,我提供了一个基本的例子(Xaml: ViewModel Main Page Instantiation and Loading Strategy for Easier Binding),看起来像某人会绑定到IsMemebershipAtMax,例如你正在做的事情:

   public bool IsMembershipAtMax 
    {
        get { return MemberCount > 3; }
    }
    public int MemberCount 
    { 
        get { return _MemberCount; }
        set
        {
            _MemberCount = value; 
            OnPropertyChanged();
            OnPropertyChanged("IsMembershipAtMax");
        } 
    }

    public List<string> Members 
    { 
        get { return _Members; }
        set { _Members = value; OnPropertyChanged(); } 
    }