我有一个附加属性,它似乎不允许我绑定(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/