Browse Source

BindingWrapper now supports MultiBinding and PriorityBinding.

pull/514/head
gumme 11 years ago
parent
commit
6d1d189713
  1. 91
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/XamlDom/MarkupExtensionTests.cs
  2. 34
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlObject.cs

91
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/XamlDom/MarkupExtensionTests.cs

@ -17,6 +17,9 @@ @@ -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;
@ -118,6 +121,79 @@ namespace ICSharpCode.WpfDesign.Tests.XamlDom @@ -118,6 +121,79 @@ namespace ICSharpCode.WpfDesign.Tests.XamlDom
{
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()
@ -177,4 +253,19 @@ namespace ICSharpCode.WpfDesign.Tests.XamlDom @@ -177,4 +253,19 @@ namespace ICSharpCode.WpfDesign.Tests.XamlDom
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
}
}

34
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlObject.cs

@ -18,6 +18,7 @@ @@ -18,6 +18,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
@ -548,11 +549,35 @@ namespace ICSharpCode.WpfDesign.XamlDom @@ -548,11 +549,35 @@ namespace ICSharpCode.WpfDesign.XamlDom
{
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);
@ -560,7 +585,8 @@ namespace ICSharpCode.WpfDesign.XamlDom @@ -560,7 +585,8 @@ namespace ICSharpCode.WpfDesign.XamlDom
pd.SetValue(b, val2);
} catch {}
}
return b.ProvideValue(XamlObject.ServiceProvider);
return b;
}
}

Loading…
Cancel
Save