我已经设置了这样的属性:
public static readonly DependencyProperty ValorRegistadoProperty = DependencyProperty.RegisterAttached(
"ValorRegistado",
typeof(string),
typeof(CampoInfo), new PropertyMetadata(new PropertyChangedCallback((d, e) =>
{
System.Diagnostics.Debug.WriteLine("Valor mudado");
})));
public static void SetValorRegistado(UIElement element, string value)
{
if (string.IsNullOrEmpty(value.Trim()))
throw new Exception("Por favor preencha este valor.");
element.SetValue(ValorRegistadoProperty, value);
}
public static string GetValorRegistado(UIElement element)
{
return (string)element.GetValue(ValorRegistadoProperty);
}
让控件声明如下:
<toolkit:NumericUpDown Value="{Binding (CampoInfo.ValorRegistado), Mode=TwoWay}">
我知道我希望使用转换器,但这并不会给我任何错误....
如何绑定自定义依赖项属性?
答案 0 :(得分:1)
你有什么应该工作,我有类似的工作,例如:
public static string GetId ( DependencyObject target )
{
return ( ( target.GetValue( IdProperty ) ) as string );
}
public static void SetId ( DependencyObject target, string value )
{
target.SetValue( IdProperty, value );
}
public static readonly DependencyProperty IdProperty = DependencyProperty.RegisterAttached(
"SamplePropertyName",
typeof( string ),
typeof( Translatable ),
new PropertyMetadata( IdPropertyChanged ) );
哪个绑定为:
<TextBlock TextWrapping="Wrap" mystuff:myclass.Id="{Binding FooBar.Id}"/>
我想尝试改变的绑定的唯一部分是:
<toolkit:NumericUpDown Value="{Binding CampoInfo.ValorRegistado, Mode=TwoWay"/>
我会检查绑定表达式是否正常工作,例如,您可能只需要在上面的示例中指定ValorRegistado。一种检查方法是尝试绑定到一个简单的TextBlock来证明值正在通过或绑定CampoInfo并查看完全限定的对象名称show。
尝试绑定依赖项属性的另一种方法是命名您正在使用的页面并使用元素绑定,例如:
在您的XAML页面定义中:
<UserControl
x:Name="myPageIdentifier"
x:Class="MyTest.MyPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
....
</UserControl>
然后将绑定作为:
<toolkit:NumericUpDown Value="{Binding ElementName=myPageIdentifier,Path=SamplePropertyName,Mode=TwoWay"/>
答案 1 :(得分:0)
我实现了以这种方式改变的属性:
public static readonly DependencyProperty ValorRegistadoProperty = DependencyProperty.RegisterAttached(
"ValorRegistado",
typeof(string),
typeof(CampoInfo), new PropertyMetadata(new PropertyChangedCallback((d, e) =>
{
UIElement uie = (UIElement)d;
SetValorRegistado(uie, e.NewValue.ToString());
})));
并且绑定:
local:CampoInfo.ValorRegistado="{Binding RelativeSource={RelativeSource Self}, Path=Value, Mode=TwoWay}"