我如何从 mainpage.xaml 更新 UserControl 文本块文本 [英] How do i update UserControl textblock text from mainpage.xaml

查看:34
本文介绍了我如何从 mainpage.xaml 更新 UserControl 文本块文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 usercontrol 中有 textblock.现在我想在 mainpage.xaml 中输入文本时更新 textblock 的文本.请帮忙

I have textblock inside usercontrol.Now i want to update the text of textblock when i enter the text in mainpage.xaml.Please help

我试过这个例子,但它没有更新文本.

i tried this example but it does not update text.

UserControl.xaml

<TextBlock 
    x:Name="txtCartcount" 
    Text="{Binding CartCount}"      
    VerticalAlignment="Center" 
    FontWeight="Bold" 
    FontSize="15" 
    Foreground="#E4328A"   
    HorizontalAlignment="Center">
</TextBlock>

MainPage.xaml

private string _CartCount;

public string CartCount
{
    get { return _CartCount; }
    set { _CartCount = value; NotifyPropertyChanged("CartCount"); }
}

CartCount=txtMessage.text;

推荐答案

我假设您的用户控件包含其他元素而不仅仅是 TextBox,如果不是这种情况,您可以将您的TextBox 直接添加到您的 MainPage.xaml 中,因为它更容易.

I'll suppose your user control contains other elements and not just the TextBox, if it's not the case, you can put your TextBox directly into your MainPage.xaml as it's easier.

鉴于该假设,您有两种选择可以更改 UserControl 中的文本:

Given that assumption, you have two options to change the text insideyour UserControl:

  • 无绑定:您应该在 UserControl 中公开公开您的 TextBox.Text 属性的 setter,以便您可以从您的MainPage 像这样
  • Without Binding: You should publicly expose a setter of your TextBox.Text property inside your UserControl so that you can modify it from your MainPage like this

myUserControl.SetText("MyText");

myUserControl.SetText("MyText");

  • 使用绑定:您应该在用户控件中定义一个依赖属性,该属性将包装您的 TextBox.Text 属性.然后在主页的 xaml 中,您可以绑定 UserControl 的这个新定义的属性,就好像它是一个 TextBlock.这样做的方法如下:
    • With binding: You should define a dependency property inside your user control that will wrap your TextBox.Text property. Then in the xaml of your main page you can bind this new defined property of your UserControl as if it was a TextBlock. Here's how you'd do this:
    • 在您的 UserControl 的代码隐藏中:

      In your UserControl's codebehind:

      public static readonly DependencyProperty TextProperty = 
          DependencyProperty.Register(
          "Text", typeof(String),typeof(UserControl), null
          );
      
      //This dependency property defined above will wrap the TextBlock's Text property
      public String Text
      {
          get { return (String)GetValue(txtCartcount.Text); }
          set { SetValue(txtCartcount.Text, value); }
      }
      

      现在您的 UserControl 有一个 Bindable Text 属性,您可以像这样在 MainPage.xaml 中使用它:

      Now your UserControl have a Bindable Text property that you can use in your MainPage.xaml like this:

      <UserControl
          Text = "{Binding PropertyToBindTo}"
      </UserControl>
      

      所以现在,如果您正确设置了绑定,当您更改绑定属性并触发 NotifyChanged 事件时,UserControl 定义的 Text 属性 将收到通知并调用它的 setter,后者将设置您的 txtCartcount 的真实 Text 属性.

      So now if you set up your binding correctly, when you change the bound property and fire up the NotifyChanged event, the defined Text property of your UserControl will get the notification and will call its setter, which will set the real Text property of your txtCartcount.

      希望这会有所帮助.

      这篇关于我如何从 mainpage.xaml 更新 UserControl 文本块文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆