1

アタッチされたプロパティがあり、Windows8ではアタッチプロパティにバインドできないようです。Silverlight/WPFではこの方法を自由に使用できますが、なぜ許可されないのかわかりません。MSDNフォーラムの投稿によると、Windows 8リリースで修正されました。現在リリースされていますが、機能しません。「値が期待範囲内にない」と表示されます。

    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

あなたのDataContextは何ですか?おそらく、バインディングに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 に答える