2012-07-09 69 views
0

我创建了具有两个文本块的userControl。我可以在xaml页面上设置使用此代码的文本。如何在后面的代码上设置绑定文本

<my:Title Title TitleCaption="test On XMAL" /> 

但是我想设置代码上的文本的值。有人会告诉我如何完成这项任务?提前致谢。

有我的用户:

<UserControl x:Name="TitleSection" x:Class="CMSPhoneApp.Title" 
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" Style="{StaticResource GridTitleStyle1}" > 

    <StackPanel> 
    <TextBlock x:Name="ApplictionTtile" Width="350" Text="MyAppTitle " HorizontalAlignment="Left" FontSize="20"> 
     <TextBlock.Foreground> 
      <SolidColorBrush Color="#FFFFFF"/> 
     </TextBlock.Foreground> 
    </TextBlock> 
     <TextBlock x:Name="PageTtile" Style="{StaticResource TitleTextBlockStyle}" 
       Text="{Binding Path=TitleCaption, Mode=TwoWay, ElementName=TitleSection }"  
        > 


     </TextBlock> 
    </StackPanel> 
</Grid> 

以下是此页面的后台代码:

namespace CMSPhoneApp 
{ 
    public partial class Title : UserControl 
    { 
    public Title() 
    { 
     InitializeComponent(); 
    } 

    public static DependencyProperty TitleCaptionProperty = 
DependencyProperty.Register("TitleCaption", typeof(string), typeof(Title), null); 

    public string TitleCaption 
    { 
     get 
     { 
      return (string)GetValue(TitleCaptionProperty); 
     } 
     set 
     { 
      SetValue(TitleCaptionProperty, value); 
     } 
    } 
} 

}

回答

1

它可以在一个更简单的方法来完成,你的财产应该是:

public string TitleCaption 
    { 
     get 
     { 
      return PageTitle.Text; 
     } 
     set 
     { 
      PageTitle.Text=value; 
     } 
    } 

现在,当您创建该控件的名称是:

<my:Title Name="myTitle" TitleCaption="test On XMAL" /> 

现在,您可以通过更改代码的PageTitle以下代码:

myTitle.TitleCaption="your Text Goes Here"; 
+0

谢谢。有用。 – user819774 2012-07-10 14:47:28

+0

你应该upvote和标记最好的帮助你的答案。这有助于你的助手获得声望:)。你不受欢迎的方式.... – naqvitalha 2012-07-10 19:47:22

0

从你可以做一些类似的代码:

PageTtile.Text = "This is from code"; // where PageTile is the x:Name of the TextBlock in XAML 
相关问题