0

2つのテキストブロックを持つuserControlを作成しました。このコードを使用するために、xamlページのテキストを設定できます。

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

ただし、コードのテキストの値を設定したいと思います。誰かがこのタスクを達成する方法を教えてもらえますか?前もって感謝します。

私のuserControlがあります:

<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);
        }
    }
}

}

4

2 に答える 2

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";
于 2012-07-10T05:23:25.377 に答える
0

コードから次のようなことができます:

PageTtile.Text = "This is from code"; // where PageTile is the x:Name of the TextBlock in XAML
于 2012-07-09T22:54:16.697 に答える