我有这样的用户控件:
<Grid x:Name="LayoutRoot" Background="Transparent">
<Image Source="/Images/[email protected]" 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>`
谢谢。
我找到了解决方案。
<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/[email protected]" 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!" />
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)