我需要将控件的属性绑定到XAML中的附加属性(以便附加属性成为绑定的源),我无法弄清楚如何做到 - VS2015给了我" 价值不会下降在预期范围内 "错误,当我运行应用程序时,我得到一个例外.
下面显示的技术在WPF中完美运行.
以下是演示此问题的示例应用程序.
AttachedPropertyTest.cs:
namespace App7
{
public static class AttachedPropertyTest
{
public static readonly DependencyProperty FooProperty = DependencyProperty.RegisterAttached(
"Foo", typeof(string), typeof(AttachedPropertyTest), new PropertyMetadata("Hello world!"));
public static void SetFoo(DependencyObject element, string value)
{
element.SetValue(FooProperty, value);
}
public static string GetFoo(DependencyObject element)
{
return (string) element.GetValue(FooProperty);
}
}
}
Run Code Online (Sandbox Code Playgroud)
MainPage.xaml中:
<!-- Based on the default MainPage.xaml template from VS2015.
The only thing added is the TextBlock inside the Grid. -->
<Page x:Class="App7.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App7"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" …Run Code Online (Sandbox Code Playgroud)