Browse Source

Fixed bug; If a Binding inside a MultiBinding is referencing a resource not available until the MultiBinding is added to the tree, the resource was not resolved.

pull/711/head
gumme 10 years ago
parent
commit
7b78ef6fad
  1. 62
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/ModelTests.cs
  2. 13
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlObject.cs
  3. 9
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlProperty.cs

62
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/ModelTests.cs

@ -895,6 +895,52 @@ namespace ICSharpCode.WpfDesign.Tests.Designer @@ -895,6 +895,52 @@ namespace ICSharpCode.WpfDesign.Tests.Designer
AssertLog("");
}
[Test]
public void AddBindingWithStaticResourceToMultiBinding()
{
DesignItem textBox = CreateCanvasContext("<TextBox/>");
DesignItem canvasItem = textBox.Parent;
DesignItemProperty canvasResources = canvasItem.Properties.GetProperty("Resources");
DesignItem exampleClassItem = textBox.Services.Component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClass());
exampleClassItem.Key = "testKey";
exampleClassItem.Properties["StringProp"].SetValue("String value");
canvasResources.CollectionElements.Add(exampleClassItem);
DesignItem multiBindingItem = textBox.Context.Services.Component.RegisterComponentForDesigner(new MultiBinding());
multiBindingItem.Properties["Converter"].SetValue(new ReturnFirstValueMultiConverter());
DesignItemProperty bindingsProp = multiBindingItem.ContentProperty;
DesignItem myBindingExtension = textBox.Context.Services.Component.RegisterComponentForDesigner(new Binding());
myBindingExtension.Properties["Path"].SetValue("StringProp");
myBindingExtension.Properties["Source"].SetValue(new StaticResourceExtension());
myBindingExtension.Properties["Source"].Value.Properties["ResourceKey"].SetValue("testKey");
// Adding it to MultiBinding "Bindings" collection.
bindingsProp.CollectionElements.Add(myBindingExtension);
textBox.ContentProperty.SetValue(multiBindingItem);
// Verify that the text have the expected value, proving that the StaticResource have been resolved.
Assert.AreEqual("String value", ((TextBox)textBox.Component).Text);
const string expectedXaml = "<Canvas.Resources>\n" +
" <Controls0:ExampleClass x:Key=\"testKey\" StringProp=\"String value\" />\n" +
"</Canvas.Resources>\n" +
"<TextBox>\n" +
" <MultiBinding>\n" +
" <MultiBinding.Converter>\n" +
" <t:ReturnFirstValueMultiConverter />\n" +
" </MultiBinding.Converter>\n" +
" <Binding Path=\"StringProp\" Source=\"{StaticResource testKey}\" />\n" +
" </MultiBinding>\n" +
"</TextBox>";
AssertCanvasDesignerOutput(expectedXaml, textBox.Context, "xmlns:Controls0=\"" + ICSharpCode.WpfDesign.Tests.XamlDom.XamlTypeFinderTests.XamlDomTestsNamespace + "\"");
AssertLog("");
}
[Test]
public void AddBrushAsResource()
{
@ -1024,6 +1070,22 @@ namespace ICSharpCode.WpfDesign.Tests.Designer @@ -1024,6 +1070,22 @@ namespace ICSharpCode.WpfDesign.Tests.Designer
}
}
public class ReturnFirstValueMultiConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if(values != null && values.Length > 0) {
return values[0];
} else {
return System.Windows.DependencyProperty.UnsetValue;
}
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class ExampleClass
{
string stringProp;

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

@ -283,8 +283,17 @@ namespace ICSharpCode.WpfDesign.XamlDom @@ -283,8 +283,17 @@ namespace ICSharpCode.WpfDesign.XamlDom
void UpdateChildMarkupExtensions(XamlObject obj)
{
foreach (XamlObject propXamlObject in obj.Properties.Where((prop) => prop.IsSet).Select((prop) => prop.PropertyValue).OfType<XamlObject>()) {
UpdateChildMarkupExtensions(propXamlObject);
foreach (var prop in obj.Properties) {
if (prop.IsSet) {
var propXamlObject = prop.PropertyValue as XamlObject;
if (propXamlObject != null) {
UpdateChildMarkupExtensions(propXamlObject);
}
} else if (prop.IsCollection) {
foreach (var propXamlObject in prop.CollectionElements.OfType<XamlObject>()) {
UpdateChildMarkupExtensions(propXamlObject);
}
}
}
if (obj.IsMarkupExtension && obj.ParentProperty != null) {

9
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlProperty.cs

@ -266,6 +266,15 @@ namespace ICSharpCode.WpfDesign.XamlDom @@ -266,6 +266,15 @@ namespace ICSharpCode.WpfDesign.XamlDom
catch (Exception ex) {
Debug.WriteLine("UpdateValueOnInstance() failed - Exception:" + ex.Message);
}
} else if (IsCollection) {
var list = ValueOnInstance as System.Collections.IList;
if (list != null) {
list.Clear();
foreach (var item in CollectionElements) {
var newValue = item.GetValueFor(propertyInfo);
list.Add(newValue);
}
}
}
}

Loading…
Cancel
Save