【问题标题】:Update UserControl Textblock text from mainpage.xaml从 mainpage.xaml 更新 UserControl Textblock 文本
【发布时间】:2015-01-05 11:46:35
【问题描述】:

我正在编写一个 Windows Phone 8.1 Silverlight 应用程序。我做了一个用户控件 NotificationsIconUserControl。它只包含一个 BELL/ALARM 图标和文本块来显示未读通知的数量。

我想从 mainpage.xaml 更新这个文本块文本

如何做到这一点?

我尝试使用usercontrol expose properties,但结果恰恰相反。还尝试了this question 的帮助。如何使用依赖属性。请编辑以下代码:

用户控件 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>

主页 XAML 部分:

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

主页 .cs 部分:

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

【问题讨论】:

    标签: xaml windows-phone-8 user-controls


    【解决方案1】:

    我检查了您的代码,它的工作原理完全...... 并且对于添加依赖属性的部分,在UserControl的.cs文件中写入以下内容

    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"));
    }
    

    之后你可以使用 TemplateBinding 来完成你的工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-12
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      • 2018-11-26
      • 1970-01-01
      • 2011-11-03
      相关资源
      最近更新 更多