Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
// DEALINGS IN THE SOFTWARE.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using NUnit.Framework;
using System.Windows.Markup;
using ICSharpCode.WpfDesign.XamlDom;
Expand Down Expand Up @@ -118,6 +121,79 @@ public void TestQuotedPathWithCommasAndSpaces()
{
TestMarkupExtension("Content=\"{t:String '" + PathWithCommasAndSpaces + "'}\"");
}

[Test]
public void TestMultiBinding()
{
string xaml = @"
<Window xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:t=""" + XamlTypeFinderTests.XamlDomTestsNamespace + @""">
<TextBox>
<MultiBinding>
<MultiBinding.Converter>
<t:MyMultiConverter />
</MultiBinding.Converter>
<Binding Path=""SomeProperty"" />
</MultiBinding>
</TextBox>
</Window>";

TestLoading(xaml);

TestWindowMultiBinding((Window)XamlReader.Parse(xaml));

var doc = XamlParser.Parse(new StringReader(xaml));

TestWindowMultiBinding((Window)doc.RootInstance);
}


void TestWindowMultiBinding(Window w)
{
var textBox = (TextBox)w.Content;

var expr = BindingOperations.GetMultiBindingExpression(textBox, TextBox.TextProperty);
Assert.IsNotNull(expr);

var converter = expr.ParentMultiBinding.Converter as MyMultiConverter;
Assert.IsNotNull(converter);

Assert.AreEqual(expr.ParentMultiBinding.Bindings.Count, 1);
}

[Test]
public void TestPriorityBinding()
{
string xaml = @"
<Window xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:t=""" + XamlTypeFinderTests.XamlDomTestsNamespace + @""">
<TextBox>
<PriorityBinding>
<Binding Path=""SomeProperty"" />
<Binding Path=""OtherProperty"" />
</PriorityBinding>
</TextBox>
</Window>";

TestLoading(xaml);

TestWindowPriorityBinding((Window)XamlReader.Parse(xaml));

var doc = XamlParser.Parse(new StringReader(xaml));

TestWindowPriorityBinding((Window)doc.RootInstance);
}


void TestWindowPriorityBinding(Window w)
{
var textBox = (TextBox)w.Content;

var expr = BindingOperations.GetPriorityBindingExpression(textBox, TextBox.TextProperty);
Assert.IsNotNull(expr);

Assert.AreEqual(expr.ParentPriorityBinding.Bindings.Count, 2);
}

// [Test]
// public void Test10()
Expand Down Expand Up @@ -177,4 +253,19 @@ public override object ProvideValue(IServiceProvider serviceProvider)
return null;
}
}

public class MyMultiConverter : IMultiValueConverter
{
#region IMultiValueConverter implementation
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return System.Windows.DependencyProperty.UnsetValue;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
Expand Down Expand Up @@ -548,19 +549,44 @@ class BindingWrapper : MarkupExtensionWrapper
{
public override object ProvideValue()
{
var target = XamlObject.Instance as Binding;
var target = XamlObject.Instance as BindingBase;
Debug.Assert(target != null);
//TODO: XamlObject.Clone()
var b = new Binding();
var b = CopyBinding(target);
return b.ProvideValue(XamlObject.ServiceProvider);
}

BindingBase CopyBinding(BindingBase target)
{
BindingBase b;
if (target != null) {
b = (BindingBase)Activator.CreateInstance(target.GetType());
} else {
b = new Binding();
}

foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(target)) {
if (pd.IsReadOnly) continue;
if (pd.IsReadOnly) {
if (pd.Name.Equals("Bindings", StringComparison.Ordinal)) {
var bindings = (Collection<BindingBase>)pd.GetValue(target);
var newBindings = (Collection<BindingBase>)pd.GetValue(b);

foreach (var binding in bindings) {
newBindings.Add(CopyBinding(binding));
}
}

continue;
}
try {
var val1 = pd.GetValue(b);
var val2 = pd.GetValue(target);
if (object.Equals(val1, val2)) continue;
pd.SetValue(b, val2);
} catch {}
}
return b.ProvideValue(XamlObject.ServiceProvider);

return b;
}
}

Expand Down