Пример #1
0
        private static bool TryBindSelectedItems(Type viewModelType,
                                                 string path,
                                                 PropertyInfo property,
                                                 FrameworkElement element,
                                                 ElementConvention convention,
                                                 IList selectedItems)
        {
            var parentApplied = ConventionManager.GetElementConvention(typeof(Selector))
                                .ApplyBinding(viewModelType, path, property, element, convention);
            var index = path.LastIndexOf('.');

            index = index == -1 ? 0 : index + 1;
            var baseName     = path.Substring(index);
            var propertyInfo = viewModelType.GetPropertyCaseInsensitive("Selected" + baseName);

            if (propertyInfo == null || !typeof(IList).IsAssignableFrom(propertyInfo.PropertyType))
            {
                return(parentApplied);
            }

            var target = (IList)propertyInfo.GetValue(element.DataContext, null);

            CollectionHelper.Bind(selectedItems, target);
            return(true);
        }
    public void BindTwoObservableCollections()
    {
        var c1 = new ObservableCollection <int>();
        var c2 = new ObservableCollection <int>();

        c1.Add(1);
        Assert.AreEqual(0, c2.Count);
        var subscription = CollectionHelper.Bind(c1, c2);

        c1.Add(2);
        Assert.AreEqual(1, c2.Count);
        Assert.AreEqual(2, c2[0]);
        c2.Add(3);
        Assert.AreEqual(3, c1.Count);
        Assert.AreEqual(3, c1[2]);
        c2.Remove(2);
        Assert.AreEqual(2, c1.Count);
        subscription.Dispose();
        c2.Remove(3);
        Assert.AreEqual(2, c1.Count);
    }