c# - 无法绑定(bind)到 Windows 8 Store Apps 中的附加属性

标签 c# xaml windows-8 windows-runtime

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

    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)}"/>

最佳答案

您的 DataContext 是什么?也许您应该将 ElementName 属性添加到您的绑定(bind)中以将上下文设置为您的控件。

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

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>

关于c# - 无法绑定(bind)到 Windows 8 Store Apps 中的附加属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13676562/

相关文章:

c# - 文件流和编码

wpf - 如何使 WPF DataGrid 单元格右对齐而不使新行上的可选区域变小?

Silverlight 数据将一个属性绑定(bind)到另一个或多个属性的表达式

mobile - WinRT 和桌面应用程序 bundle ?

c# - WebAuthenticationBroker.AuthenticateAsync 抛出异常

windows-8 - 启动另一个 Windows 应用商店应用

c# - 发布时如何清除 Razor Page 模型上的绑定(bind)属性?

c# - 为扩展方法错误地(?)生成了 CS1720 警告

c# - 对包含数字和字母的字符串进行排序

c# - Style 对象不能影响它所应用的对象的 Style 属性