Reputation: 343
I try to bind a bindig example:
settings.xaml
<labels:KeyValueLabel Key="User name:" Label="{Binding Name}" Margin="10"/>
KeyValueLabel.xaml
<UserControl x:Class="rodzic.Controls.Labels.KeyValueLabel"
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"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
d:DesignHeight="100" d:DesignWidth="480">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="KeyLabel" Text="{Binding Key}"/>
<TextBlock x:Name="ValueLabel" Text="{Binding Label}"/>
</Grid>
KeyValueLabel.xaml.cs
public partial class KeyValueLabel : UserControl
{
public string Key { get; set; }
public static readonly DependencyProperty LabelProperty = DependencyProperty.Register(
"Label",
typeof(String),
typeof(KeyValueLabel),
new PropertyMetadata("default", new PropertyChangedCallback(LabelChanged))
);
static void LabelChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
//e is always being set to ""
}
public string Label
{
get { return (string)GetValue(LabelProperty); }
set { SetValue(LabelProperty, value); }
}
public KeyValueLabel()
{
InitializeComponent();
}
}
If I pass value as a static text in settings.xaml it works but binding data from settings.xaml don't for key nor for label.
How to fix label binding? I want to make it work no matter how nested the binding is.
Upvotes: 0
Views: 66
Reputation: 37875
It appears that you want to create a Custom Control, not UserControl. These are two completely different things. With a custom control, you can create your dependency properties and then bind to them directly like you would any other control.
In the custom control, you then catch the setting of it and set the Text property of the TextBlock directly. Thus you don't need the double binding.
Upvotes: 1