扩展器 IsExpanded 绑定 [英] Expander IsExpanded binding

查看:89
本文介绍了扩展器 IsExpanded 绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码中:

http://msdn.microsoft.com/en-us/library/ms754027.aspx

如何将 IsExpanded 绑定到对象的 MyData 列表,其中每个对象都有 IsExpanded 属性?

How to Bind IsExpanded to the MyData list of objects, where each object has the IsExpanded property?

<Expander IsExpanded={Binding Path=IsExpanded, Mode=TwoWay} />

这不行!

MyData is List<GroupNode>;

GroupNode 是一个包含通知属性更改属性 IsExpanded 的类.

GroupNode is a class containing notify property changed property IsExpanded.

因此,如果我手动打开一个扩展器,它应该将 MyData 的 GroupNode 的 IsExpanded 属性设置为 true.

So, if I open one of the expander manually it should set the IsExpanded property to true of that MyData's GroupNode.

推荐答案

不是很容易做,因为GroupItemDataContextCollectionViewGroup,还有这个类没有 IsExpanded 属性.但是,您可以在 GroupDescription 中指定转换器,允许您返回组的名称"的自定义值(CollectionViewGroup.Name 属性).这个名字"可以是任何东西;在您的情况下,您需要它是一个包装组名称(例如分组键)并具有 IsExpanded 属性的类:

It's not very easy to do, because the DataContext of the GroupItem is an instance of CollectionViewGroup, and this class doesn't have a IsExpanded property. You can however specify a converter in the GroupDescription, allowing you to return a custom value for the "name" of the group (the CollectionViewGroup.Name property). This "name" can be anything; in your case, you need it to be a class that wraps the group name (e.g. grouping key) and has a IsExpanded property:

这是一个例子:

public class ExpandableGroupName : INotifyPropertyChanged
{
    private object _name;
    public object Name
    {
        get { return _name; }
        set
        {
            if (_name != value)
            {
                _name = value;
                OnPropertyChanged("Name");
            }
        }
    }

    private bool? _isExpanded = false;
    public bool? IsExpanded
    {
        get { return _isExpanded; }
        set
        {
            if (_isExpanded != value)
            {
                _isExpanded = value;
                OnPropertyChanged("IsExpanded");
            }
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion

    public override bool Equals(object obj)
    {
        return object.Equals(obj, _name);
    }

    public override int GetHashCode()
    {
        return _name != null ? _name.GetHashCode() : 0;
    }

    public override string ToString()
    {
        return _name != null ? _name.ToString() : string.Empty;
    }
}

这是转换器:

public class ExpandableGroupNameConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return new ExpandableGroupName { Name = value };
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var groupName = value as ExpandableGroupName;
        if (groupName != null)
            return groupName.Name;
        return Binding.DoNothing;
    }

    #endregion
}

在 XAML 中,只需按如下方式声明分组:

In XAML, just declare the grouping as follows:

<my:ExpandableGroupNameConverter x:Key="groupConverter" />
<CollectionViewSource x:Key='src' 
                      Source="{Binding Source={StaticResource MyData}, 
                               XPath=Item}">
  <CollectionViewSource.GroupDescriptions>
    <PropertyGroupDescription PropertyName="@Catalog" Converter="{StaticResource groupConverter}" />
  </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>

并像这样绑定 IsExpanded 属性:

And bind the IsExpanded property like that:

<Expander IsExpanded={Binding Path=Name.IsExpanded} />

这篇关于扩展器 IsExpanded 绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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