C# XAML:访问用户控件内的控件

C# XAML:访问用户控件内的控件,c#,silverlight,xaml,windows-phone,C#,Silverlight,Xaml,Windows Phone,我有这样的userControl: <Grid x:Name="LayoutRoot" Background="Transparent"> <Image Source="/Images/btn1_normal@2x.png" Stretch="Fill" /> <TextBlock x:Name="Text" Text="Button" Foreground="Black" VerticalAlignment="Cent

我有这样的userControl:

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Image Source="/Images/btn1_normal@2x.png" Stretch="Fill" />
    <TextBlock x:Name="Text" Text="Button" Foreground="Black"
               VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
<MyControlls:MyButton Width="90" Height="55"/>
<MyControlls:MyButton Width="90" Height="55">
    <MyButton.Text Text="Hello World!" />        
</MyControlls:MyButton>`
<MyUserControl:MyButton Width="90" Height="55" Margin="10 0 0 0" Title="Hello Wold!" />

我在另一个XAML中使用此userControl,如下所示:

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Image Source="/Images/btn1_normal@2x.png" Stretch="Fill" />
    <TextBlock x:Name="Text" Text="Button" Foreground="Black"
               VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
<MyControlls:MyButton Width="90" Height="55"/>
<MyControlls:MyButton Width="90" Height="55">
    <MyButton.Text Text="Hello World!" />        
</MyControlls:MyButton>`
<MyUserControl:MyButton Width="90" Height="55" Margin="10 0 0 0" Title="Hello Wold!" />

现在,我如何访问此XAML中的textBlock命名文本并更改其文本(在Windows phone 8中)? 大概是这样的:

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Image Source="/Images/btn1_normal@2x.png" Stretch="Fill" />
    <TextBlock x:Name="Text" Text="Button" Foreground="Black"
               VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
<MyControlls:MyButton Width="90" Height="55"/>
<MyControlls:MyButton Width="90" Height="55">
    <MyButton.Text Text="Hello World!" />        
</MyControlls:MyButton>`
<MyUserControl:MyButton Width="90" Height="55" Margin="10 0 0 0" Title="Hello Wold!" />

`

谢谢。

要完成此任务,一个解决方案可以是在
控件中声明一个隐藏在以下代码后面的代码:

  public string ButtonText
  {
    get { return (string )this.GetValue(ButtonTextProperty); }
    set { this.SetValue(ButtonTextProperty, value); } 
  }
  public static readonly DependencyProperty ButtonTextProperty = DependencyProperty.Register(
    "ButtonText", typeof(string), typeof(MyButton),new PropertyMetadata(false));
然后必须将其绑定到xaml代码中:

  <YourControl x:Name="MyButtonName">
    ... // some xaml code
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Image Source="/Images/btn1_normal@2x.png" Stretch="Fill" />
        <TextBlock x:Name="Text" Text="Button" Foreground="Black"
                   VerticalAlignment="Center" HorizontalAlignment="Center"
                   Text="{Binding Path=ButtonText, ElementName=MyButtonName}"/>
    </Grid>
 </YourControl>

你不能直接这么做。这里有两种选择

解决方案1:使用ViewModel 使用视图模型类存储需要在自定义控件中显示的特性

class MyButtonViewModel
{
    public string Text { get;set; }
}
MyButton.xaml:

<TextBlock Text={Binding Text} ...
这要复杂得多。看

选择哪一个? 如果要在几个不同的项目中使用控件,并且需要能够更改控件的外观,请选择
UserControl

否则,请使用
ViewModel
,并最终在项目的剩余部分应用MVVM模式。

我找到了解决方案

<UserControl x:Class="Becel.Controls.MyButton"
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}"
x:Name="MyUserControll">

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Image x:Name="myImage" Source="/Images/btn1.9_normal@2x.png" Stretch="Fill"
MouseEnter="Image_MouseEnter" MouseLeave="Image_MouseLeave" />
    <TextBlock x:Name="textTitle" Text="{Binding Title, ElementName=MyUserControll}" VerticalAlignment="Center" HorizontalAlignment="Center" TextAlignment="Center" Foreground="Black" FontSize="25"  MouseEnter="Image_MouseEnter" MouseLeave="Image_MouseLeave"/>

</Grid>
</UserControl>
然后在任何地方你都可以这样使用:

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Image Source="/Images/btn1_normal@2x.png" Stretch="Fill" />
    <TextBlock x:Name="Text" Text="Button" Foreground="Black"
               VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
<MyControlls:MyButton Width="90" Height="55"/>
<MyControlls:MyButton Width="90" Height="55">
    <MyButton.Text Text="Hello World!" />        
</MyControlls:MyButton>`
<MyUserControl:MyButton Width="90" Height="55" Margin="10 0 0 0" Title="Hello Wold!" />

谢谢您的回答,但我开发的是Windows Phone 8应用程序(不是WPF)。有没有办法在WP8中创建CustomControl?请参阅(为XAML Metro风格的应用程序构建可部署的自定义控件)