I need to bind a property of a control to an attached property in XAML (so that the attached property becomes a source of the binding), and I can't figure out how to do it -- VS2015 gives me "Value does not fall within the expected range" error, and when I run the app, I get an exception.
The technique shown below worked perfectly in WPF.
Here is the sample app demonstrating the problem.
AttachedPropertyTest.cs:
namespace App7
{
public static class AttachedPropertyTest
{
public static readonly DependencyProperty FooProperty = DependencyProperty.RegisterAttached(
"Foo", typeof(string), typeof(AttachedPropertyTest), new PropertyMetadata("Hello world!"));
public static void SetFoo(DependencyObject element, string value)
{
element.SetValue(FooProperty, value);
}
public static string GetFoo(DependencyObject element)
{
return (string) element.GetValue(FooProperty);
}
}
}
MainPage.xaml:
<!-- Based on the default MainPage.xaml template from VS2015.
The only thing added is the TextBlock inside the Grid. -->
<Page x:Class="App7.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App7"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock Text="{Binding Path=(local:AttachedPropertyTest.Foo), RelativeSource={RelativeSource Self}}"
HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Page>
Instead of displaying "Hello world!" (which is a default value of the Foo attached property) on the TextBlock above, I get XamlParseException, thrown from InitializeComponent. As usual, the exception object does not contain any useful information.
Interestingly enough, this doesn't happen if I try to bind to any standard (built into the framework) attached property like (Grid.Row)
, so it seems that the XAML parser just doesn't let me use a custom attached property provider, which is ridiculous...
So what is the correct way of doing this?
x:Bind
fails to compile as soon as i setMode=OneWay
. One-time binding is not what I need... – Resh