0

一个值我已经设置好的属性是这样的:绑定的自定义扶养属性的Silverlight

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

我知道我HEVE用户转换器,但这不会给我任何错误......无论如何...

你如何绑定自定义依赖属性?

回答

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