1
votes

I am writing a Windows Phone 8.1 Silverlight App. I made a user control NotificationsIconUserControl. It just contains a BELL/ALARM icon and textblock to display number of unread notifications.

I want to update this textblock text from mainpage.xaml

How to do this?

I tried using usercontrol expose properties but its the opposite thing. Also tried help from this question. how to use dependency property. Please edit the code below:

Usercontrol XAML:

<Grid x:Name="LayoutRoot" 
      Background="Transparent"
      Height="Auto"
      Width="Auto">

    <Image
            Name="Alarm_Icon" 
            Source="/Images/Status/Notification_Icon_1.png">
    </Image>

    <Ellipse 
                Name="Counter_Icon"
                Height="45"
                Width="45" 
                Margin="60,14,-6,50"
                StrokeThickness="0" 
        Fill="{StaticResource DefaultTheme_IndianRedColor}">
    </Ellipse>

    <TextBlock
        Name="Counter_Label"
        Foreground="{StaticResource DefaultTheme_LightColor}"
        FontSize="30"
        HorizontalAlignment="Center" 
        VerticalAlignment="Center" 
        TextAlignment="Center" 
       Margin="75,20,8,58"/>
</Grid>

Mainpage XAML part:

        xmlns:MyUserControls="clr-namespace:Project.Custom.UserControls">

Mainpage .cs part:

   private void ConfigureNotificationsIcon()
    {
        int NotificationsCounter = 4;
        NotificationsIconUserControl NotificationsIconUserControlObject = new NotificationsIconUserControl();
        NotificationsIconUserControlObject.Counter_Label.Text = NotificationsCounter.ToString();
    }
1

1 Answers

0
votes

I checked your code and its working completely.... ANd for the part of adding a Dependency property, write the following in the .cs file of UserControl

public partial class NotificationIconUserControl : UserControl

 {

    public NotificationIconUserControl()
    {
        InitializeComponent();

    }

    public string NotificationLabel
    {
        get { return (string)GetValue(NotificationLabelProperty); }
        set { SetValue(NotificationLabelProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Spacing.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty NotificationLabelProperty =
        DependencyProperty.Register("NotificationLabel", typeof(string), typeof(NotificationIconUserControl), new PropertyMetadata("hellllo"));
}

After that you can use TemplateBinding to do your job