Xaml et Dependency property
Bonjour,
je ai une erreur toute bête en essayant de comprendre les dependency properties.
On suivant les doc j'ai essayé de créer une property "Age" pour l'utiliser en xaml sur un userControl. Et là je n'arrive pas à lui passer une valeur et à l'affichage j'ai 0.
Voici mon code:
le xaml du UserControl:
Code:
1 2 3 4 5 6 7
|
<UserCOntrol [...]
<Grid Height="62" width="148">
<Label Margin ="28,22,0,12" Name="label1">Label</Label>
</Grid>
</UserControl> |
du code behind du control ou j'affecte la valeur age au label1:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
namespace DependencyPropertiesWpfProject
{
/// <summary>
/// Logique d'interaction pour UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
public static DependencyProperty ageProperty=
DependencyProperty.Register("Age", typeof(int), typeof(UserControl1));
public int Age
{
get
{
return (int)GetValue(ageProperty);
}
set
{
SetValue(ageProperty, value);
}
}
public UserControl1()
{
InitializeComponent();
label1.Content = Age;
}
}
} |
Le xaml de Window1:
Code:
1 2 3 4 5 6 7 8 9 10 11
| dow x:Class="DependencyPropertiesWpfProject.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:uc="clr-namespace:DependencyPropertiesWpfProject"
Title="Window1" Height="300" Width="300">
<Grid Width="271" Height="241">
<Canvas>
<uc:UserControl1 Height="59" Width="152" Canvas.Left="41" Canvas.Top="31" Age="12"></uc:UserControl1>
</Canvas>
</Grid>
</Window> |
Si quelqu'un a une piste...
merci