在适用于Windows Phone的VS 2010 Express中更改文本块UserControl上的文本

本文关键字:文本 UserControl Express Windows 适用于 Phone VS 2010 | 更新日期: 2023-09-27 18:25:34

我在VS2010 Express for Windows Phone中创建了具有文本块的UserControl,并将其添加到MainPage.xaml上。但是,我想在codeehind或.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>

这是我的主页.xaml:

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

在适用于Windows Phone的VS 2010 Express中更改文本块UserControl上的文本

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

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

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

所以你的用户控制理想情况下会有,

用户控制.xamlUserControl.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类。点击链接->