Windows phone 8 如何从mainpage.xaml更新UserControl textblock文本
我在usercontrol中有textblock。现在我想在mainpage.xaml中输入文本时更新textblock的文本。请帮助 我尝试了这个例子,但它不更新文本 UserControl.xamlWindows phone 8 如何从mainpage.xaml更新UserControl textblock文本,windows-phone-8,Windows Phone 8,我在usercontrol中有textblock。现在我想在mainpage.xaml中输入文本时更新textblock的文本。请帮助 我尝试了这个例子,但它不更新文本 UserControl.xaml <TextBlock x:Name="txtCartcount" Text="{Binding CartCount}" VerticalAlignment="Center" FontWeight="Bold" FontSize="
<TextBlock
x:Name="txtCartcount"
Text="{Binding CartCount}"
VerticalAlignment="Center"
FontWeight="Bold"
FontSize="15"
Foreground="#E4328A"
HorizontalAlignment="Center">
</TextBlock>
private string _CartCount;
public string CartCount
{
get { return _CartCount; }
set { _CartCount = value; NotifyPropertyChanged("CartCount"); }
}
CartCount=txtMessage.text;
我假设您的用户控件包含其他元素,而不仅仅是
TextBox
,如果不是这样,您可以将TextBox
直接放入MainPage.xaml
,因为这样更简单
根据该假设,您有两个选项来更改UserControl
中的文本:
- 无绑定:您应该在
中公开UserControl
属性的setter,以便您可以像这样从TextBox.Text
主页修改它
- 带绑定:您应该在用户控件内定义一个依赖项属性,该属性将包装您的
属性。然后在主页的xaml中,您可以绑定TextBox.Text
的这个新定义属性,就像它是UserControl
一样。以下是您如何做到这一点:TextBlock
UserControl
的代码隐藏中:
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有一个可绑定的文本
属性,您可以在主页面.xaml中使用该属性,如下所示:
<UserControl
Text = "{Binding PropertyToBindTo}"
</UserControl>
我认为您只能在属于xaml页面的codebehind中绑定控件。相反,您可以使用静态属性将值传递给UserControl页。在静态属性(App.Myproperty=mytext)中设置值,并从UserControl页(txtcartCount.text=App.mytext)获取该值嗨,paul,我在mainpage构造函数()中使用了此代码{IsolatedStorageSettings.ApplicationSettings[“State”]=totalQty;IsolatedStorageSettings.ApplicationSettings.Save();}并在usercontrol文本块页面中显示totalQty,如var item=IsolatedStorageSettings.ApplicationSettings[“状态”];txtCartcount.Text=Convert.ToString(项目);在mainpage.xaml中,我有按钮。如果我有单击按钮,则总数量将在usercontrol文本块中更新。如果我没有误解,如何从按钮单击事件重定向到usercontrol。。;当你点击更新按钮时,你想导航到用户控制页面…对吗?NavigationService.Navigate(新Uri(“UserControl.xaml”,UriKind.Relative));我想知道为什么你使用IsolatedStorage设置来传递值…即使应用程序关闭,你真的需要存储这个值吗?不,如果我使用了NavigationService.Navigate(新Uri(“UserControl.xaml”,UriKind.Relative));在更新按钮,我得到了设计问题