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

时间:2013-10-21 10:24:27

标签: 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="Center" HorizontalAlignment="Center"/>
</Grid>

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

<MyControlls:MyButton Width="90" Height="55"/>

现在我如何在此XAML中访问名为Text的textBlock并更改其文本(在Windows Phone 8中)? 像这样:

<MyControlls:MyButton Width="90" Height="55">
    <MyButton.Text Text="Hello World!" />        
</MyControlls:MyButton>`

谢谢。

3 个答案:

答案 0 :(得分:7)

我找到了解决方案。

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

在C#代码中:

public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof (String), typeof(MyButton),null);

    public String Title
    {
        get { return (String)GetValue(TitleProperty); }
        set { SetValue(TitleProperty, value); }
    }

然后在任何地方你可以这样使用:

<MyUserControl:MyButton Width="90" Height="55" Margin="10 0 0 0" Title="Hello Wold!" />

答案 1 :(得分:4)

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

解决方案1:使用ViewModel

使用视图模型类存储需要在自定义控件中显示的属性。

class MyButtonViewModel
{
    public string Text { get;set; }
}

MyButton.xaml:

<TextBlock Text={Binding Text} ...

MainWindow.xaml

<Window.Resources>
    <MyButtonViewModel x:Key="myButtonVm" Text="Hello!" />
</Window.Resources>

<MyButton DataContext={StaticResource myButtonVm} />

这使用MVVM模式。如果您从未使用过,请参阅WPF Apps With The Model-View-ViewModel Design Pattern

解决方案2:使用CustomControl

UserControl替换为CustomControl。在这种情况下,Text可以是依赖属性,因此您可以编写。

<MyButton Text="Hello !" />

这要复杂得多。请参阅How to Create a WPF Custom Control

选择哪个?

如果您打算在多个不同的项目中使用您的控件,并且您需要能够更改控件的外观,请选择UserControl

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

答案 2 :(得分:1)

要完成此任务,一种解决方案可能是将Dependency Property声明为Control(这似乎被称为“MyButton”)代码:

  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>

最后,您可以使用“MyButton”Control

<MyControlls:MyButton Width="90" Height="55" ButtonText="Hello World!">
</MyControlls:MyButton>