XAML:访问用户控件内的控件 [英] XAML: access to controls inside user control
本文介绍了XAML:访问用户控件内的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的 userControl:
I have userControl like this:
<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,如下所示:
I use this userControl in another XAML like this:
<MyControlls:MyButton Width="90" Height="55"/>
现在我如何访问此 XAML 中名为 Text 的 textBlock 并更改其文本(在 Windows 手机 8 中)?像这样:
Now how I can access to textBlock named Text in this XAML and change his text (in Windows phone 8)? Something like this:
<MyControlls:MyButton Width="90" Height="55">
<MyButton.Text Text="Hello World!" />
</MyControlls:MyButton>`
谢谢.
推荐答案
我找到了解决方案.
<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); }
}
然后在任何地方都可以像这样使用:
And then in everywhere you can use like this:
<MyUserControl:MyButton Width="90" Height="55" Margin="10 0 0 0" Title="Hello Wold!" />
这篇关于XAML:访问用户控件内的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文