Skip to content

Commit

Permalink
Merge pull request AvaloniaUI#3348 from pr8x/fix-TargetNullValue-xaml
Browse files Browse the repository at this point in the history
Adding TargetNullValue to BindingExtension
  • Loading branch information
MarchingCube authored Dec 13, 2019
2 parents 9e34731 + 2b59c86 commit a7ebcf8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public Binding ProvideValue(IServiceProvider serviceProvider)
StringFormat = StringFormat,
RelativeSource = RelativeSource,
DefaultAnchor = new WeakReference(GetDefaultAnchor(descriptorContext)),
TargetNullValue = TargetNullValue,
NameScope = new WeakReference<INameScope>(serviceProvider.GetService<INameScope>())
};
}
Expand Down Expand Up @@ -86,5 +87,7 @@ private static object GetDefaultAnchor(IServiceProvider context)
public string StringFormat { get; set; }

public RelativeSource RelativeSource { get; set; }

public object TargetNullValue { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,37 @@ public void BindingExtension_Binds_To_Source()
Assert.Equal("foobar", textBlock.Text);
}
}

[Fact]
public void BindingExtension_Binds_To_TargetNullValue()
{
using (StyledWindow())
{
var xaml = @"
<Window xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<Window.Resources>
<x:String x:Key='text'>foobar</x:String>
</Window.Resources>
<TextBlock Name='textBlock' Text='{Binding Foo, TargetNullValue={StaticResource text}}'/>
</Window>";

var loader = new AvaloniaXamlLoader();
var window = (Window)loader.Load(xaml);
var textBlock = window.FindControl<TextBlock>("textBlock");

window.DataContext = new FooBar();
window.Show();

Assert.Equal("foobar", textBlock.Text);
}
}

private class FooBar
{
public object Foo { get; } = null;
}

private IDisposable StyledWindow(params (string, string)[] assets)
{
Expand Down

0 comments on commit a7ebcf8

Please sign in to comment.