1

我有一个附加属性,它似乎不允许我绑定到 Windows 8 中的附加属性。我可以在 Silverlight/WPF 中自由使用这种方式,但我不知道为什么它不会让我这样做。MSDN 论坛上的一篇帖子说它是通过 Windows 8 Release 修复的,我现在有这个版本,但它不会工作。它说“价值不在预期范围内”..

    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 文件

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

1 回答 1

4

你的数据上下文是什么?也许您应该将 ElementName 属性添加到您的绑定中,以将上下文设置为您的控件。

*编辑 - 这对我来说很好:

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>
于 2012-12-03T04:44:43.157 回答