Ibraheem.Yan
Ibraheem.Yan

Reputation: 1

Set User Control Dependency Property value to a lable control

I am creating a user control, inside the control I have a Label Control. (for example: an email control, contains a label and a text box).

In the email address control, I defined a Title property, which when the user changes the property value, I want to set the string value to the Label.

bellow is the XAML and Code:

XAML:

<UserControl x:Class="SIMind.ClinicManagement.GUI.Controls.CommonControl.EmailAddressCtrl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" Height="32" MinHeight="32" MaxHeight="32" Width="386" MinWidth="386">
    <Grid>
      <Grid.RowDefinitions>
         <RowDefinition />
      </Grid.RowDefinitions>
      <StackPanel HorizontalAlignment="Stretch" Margin="0" Name="stackPanel1" VerticalAlignment="Stretch" Orientation="Horizontal">
         <Label Content="Email :" Height="24" HorizontalContentAlignment="Right" Name="lblEmailCaption" Tag="PhoneType" Width="140" />
         <TextBox MaxLength="100" Name="txtEmail" Text="[email protected]" Width="240" Margin="1" Height="23" />
      </StackPanel>
   </Grid>
</UserControl>

Code:


namespace SIMind.ClinicManagement.GUI.Controls.CommonControl
{
/// <summary>
/// Interaction logic for EmailAddressCtrl.xaml
/// </summary>
public partial class EmailAddressCtrl : UserControl
{
    public EmailAddressCtrl()
   {
       InitializeComponent();
   }

   #region Dependency Property

   #region Title Property

   /// <summary>
   /// Title property
   /// </summary>
   public static readonly DependencyProperty TitleProperty =
      DependencyProperty.Register("Title", typeof(String),
      typeof(EmailAddressCtrl), new PropertyMetadata("Phone Number :"));

   /// <summary>
   /// Gets and Sets the main label of the control.
   /// </summary>
   public string Title
   {
       get { return (string)GetValue(TitleProperty); }
       set { 
          SetValue(TitleProperty, value);
          lblEmailCaption.Content = value; 
       }
   }

   #endregion


   #endregion

   }
}


But it seems not working the way I wanted it to be: The dependency property is set, but the label is not refreshed to be the property set.

Any one got a good answer? :-)

Upvotes: 0

Views: 701

Answers (1)

Jeetendra.Sharma
Jeetendra.Sharma

Reputation: 194

Hope this will work for you

<Label Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type EmailAddressCtrl}},Path=Title}" Height="24" HorizontalContentAlignment="Right" Name="lblEmailCaption" Tag="PhoneType" Width="140" /> 

Upvotes: 1

Related Questions