Reputation: 53
I have setted a property like this:
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);
}
And have the control declared like this:
<toolkit:NumericUpDown Value="{Binding (CampoInfo.ValorRegistado), Mode=TwoWay}">
I know I heve to user a converter, but this doens't give me any error anyway....
How do you bind custom dependency properties?
Upvotes: 0
Views: 361
Reputation: 1827
What you have should work, I have similar working, for instance:
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 ) );
Which is bound as:
<TextBlock TextWrapping="Wrap" mystuff:myclass.Id="{Binding FooBar.Id}"/>
The only part of your binding I would try and change is:
<toolkit:NumericUpDown Value="{Binding CampoInfo.ValorRegistado, Mode=TwoWay"/>
I would check that the binding expression is working, for example, maybe you only need to specify ValorRegistado in the above example. One way of checking is to try binding to a simple TextBlock to prove that a value is coming through or to bind CampoInfo and see a fully qualified object name show.
Another approach to try binding the dependency property is to name the page you are using and use element binding, for example:
In your XAML page definition:
<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>
Then do the binding as:
<toolkit:NumericUpDown Value="{Binding ElementName=myPageIdentifier,Path=SamplePropertyName,Mode=TwoWay"/>
Upvotes: 1
Reputation: 53
I implemented the property changed this way:
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());
})));
And binding:
local:CampoInfo.ValorRegistado="{Binding RelativeSource={RelativeSource Self}, Path=Value, Mode=TwoWay}"
Upvotes: 0