【问题标题】:Change the text on textblock UserControl in VS 2010 Express for Windows Phone在 VS 2010 Express for Windows Phone 中更改文本块 UserControl 上的文本
【发布时间】:2012-02-22 00:27:26
【问题描述】:

我在 VS2010 Express for Windows Phone 中创建了具有文本块的 UserControl,并将其添加到 MainPage.xaml。但是,我想在代码隐藏或 .xaml 文件中设置文本块上的文本。任何人都会向我展示或给出示例或链接。提前致谢。

<UserControl x:Class="PhoneApp1.TitleControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480">  

<Grid x:Name="LayoutRoot">
 <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,10,28">
  <TextBlock x:Name="ApplicationTitle"  Style="{StaticResource appTitleStyle}"/>
  <Grid  Height="45" Style="{StaticResource pageTitleBackgroudStyle}">
   <TextBlock x:Name="PageTitle"     Margin="9,-7,0,17" Style="{StaticResource pageTitleStyle}"/>
        </Grid>
    </StackPanel>
</Grid>

这是我的 MainPage.xaml:

<StackPanel x:Name="Titleqq" Grid.Row="0" Margin="12,17,0,28" Grid.ColumnSpan="2">
  <local:TitleControl x:name="Title" />
</StackPanel>         

【问题讨论】:

    标签: c# visual-studio-2010 windows-phone-7


    【解决方案1】:

    编写用户控件时有两点需要注意。

    1. 编写一个在用户控件中定义属性的类。

    2. 将其绑定到您希望在 Windows Phone 应用程序中公开或设置的属性。

    所以您的 Usercontrol 理想情况下应该有,

    UserControl.xaml UserControl.xaml.cs

    你在cs文件中写你的类。将其绑定到您要在应用程序中设置的属性。所以在这种情况下,你做这样的事情,

    <UserControl x:Class="PhoneApp1.TitleControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    d:DesignHeight="480" d:DesignWidth="480"
    x:Name="parent"
    >  
    <Grid x:Name="LayoutRoot"
          DataContext="{Binding ElementName=parent}">
     <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,10,28">
      <TextBlock x:Name="ApplicationTitle"  Style="{StaticResource appTitleStyle}"/>
      <Grid  Height="45" Style="{StaticResource pageTitleBackgroudStyle}">
       <TextBlock x:Name="PageTitle"  Text="{Binding Path=Text"}   Margin="9,-7,0,17" Style="{StaticResource pageTitleStyle}"/>
            </Grid>
        </StackPanel>
    </Grid>
    

    现在在你的代码后面编写一个继承自 UserControl 的类并绑定它... Link1 和谷歌在 msdn Windows 手机 UserControl 类上。 点击链接->

    【讨论】:

    • 感谢您回答我的问题,但我是新手,不太了解。是创建一个新的 .cs 文件来创建一个类还是将其放在 UserControl.cs 上?类的属性是不是像这样的
    • chris.59north.com/post/… 可以试试看这篇文章吗?基础知识太大了,不适合在这里......
    • 感谢您回答我的问题,但我仍然一无所知。如果我在我的 UserControl.xaml.cs 上添加类。如何在 MainPage.xaml 上设置属性?以下代码在我的 Control.xaml.cs public TitleControl(Example e) {title=e; InitializeComponent();}public string Caption{get {return title.DisplayText;}
    • 我也找到了另一个例子来帮助我。有链接alan.beech.me.uk/2011/04/25/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 1970-01-01
    相关资源
    最近更新 更多