123 456 789 0
123 456 789 0

Reputation: 10865

Unable to bind to an attached property in Windows 8 Store Apps

I have a attached property and it seems that it won't let me bind to an attach property in Windows 8. I can freely use this way in Silverlight/WPF but I don't know why it won't let me. A post in MSDN Forum said it was fixed through Windows 8 Release, I have the release now but it won't work. It says "Value does not fall within the expected range"..

    public static class CountHelper
{
    public static readonly DependencyProperty TitleProperty =
        DependencyProperty.RegisterAttached("Title", typeof (string), typeof (CountHelper), new PropertyMetadata(default(string)));

    public static void SetTitle(UIElement element, string value)
    {
        element.SetValue(TitleProperty, value);
    }

    public static string GetTitle(UIElement element)
    {
        return (string) element.GetValue(TitleProperty);
    }
}

XAML file

    <TextBlock local:CountHelper.Title="Hello"  Text="{Binding Path=(local:CountHelper.Title)}"/>

Upvotes: 1

Views: 1287

Answers (1)

Filip Skakun
Filip Skakun

Reputation: 31724

What is your DataContext? Perhaps you should add ElementName property to your binding to set the context to your control.

*EDIT - this works fine for me:

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace App75
{
    public static class CountHelper
    {
        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.RegisterAttached("Title", typeof(string), typeof(CountHelper), new PropertyMetadata(default(string)));

        public static void SetTitle(UIElement element, string value)
        {
            element.SetValue(TitleProperty, value);
        }

        public static string GetTitle(UIElement element)
        {
            return (string)element.GetValue(TitleProperty);
        }
    }

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }
    }
}


<Page
    x:Class="App75.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App75"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid
        Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <TextBlock
            x:Name="tb"
            local:CountHelper.Title="Hello"
            Text="{Binding Path=(local:CountHelper.Title), ElementName=tb}" />
    </Grid>
</Page>

Upvotes: 4

Related Questions