2012-02-22 5 views
0

Windows Phone用のVS2010 Expressにtextblockを持つUserControlを作成し、MainPage.xamlに追加しました。しかし、私はtextbeock上のテキストをcodebehindまたは.xamlファイルに設定したいと思います。誰かが私に例を示したりリンクしたりするだろうか?少し早いですがお礼を。VS2010 Express for Windows Phoneのテキストブロックを変更します。

<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>   

答えて

0

のUserControlを書くときに注意するには、2つのものがあります。

  1. は、ユーザーコントロールのプロパティを定義するクラスを作成します。

  2. Windowsの電話アプリケーションで公開または設定するプロパティにバインドします。

だからあなたのユーザーコントロールは、理想的には、あなたがCSファイルで、あなたのクラスを記述

UserControl.xaml UserControl.xaml.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 PhoneユーザーコントロールクラスでGoogle。 リンクをクリックしてください - >

+0

私の質問にお答えいただきありがとうございますが、私は新しくて本当に分かりません。新しい.csファイルを作成してクラスを作成するか、UserControl.csに配置しますか?クラスのプロパティは次のようになります – user819774

+0

http://chris.59north.com/post/Creating-custom-controls-in-Silverlight-part-1.aspx この記事を読むことができますか?基本はちょうどここに収まる大きすぎます.... – Ajai

+0

私の質問に答えるためにありがとう、しかし私はまだ暗闇の中で。私は私のUserControl.xaml.csにクラスを追加する場合。 MainPage.xamlにプロパティを設定するにはどうすればよいですか?次のコードは、私のControl.xaml.cs Public TitleControl(Example e){title = e; InitializeComponent();}公開ストリングキャプション{get {return title.DisplayText;} – user819774

関連する問題