2012-07-09 32 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 
相關問題