2013-04-23 75 views
-1

我创建了一个带有TextBlock的UserControl,问题是我无法从使用此用户控件的MainPage中更改此UserControl的文本。无法访问UserControl中的子元素

Plz帮助新来Metro风格应用程序基本上是Windows手机开发者。检查下面的usercontrol的来源。

<UserControl 
x:Class="Version1forMainMenu.MyUserControl1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:Version1forMainMenu" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
d:DesignHeight="300" 
d:DesignWidth="400" x:Name="MyUserControl1"> 

<Grid x:Name="grid_amorti" PointerEntered="gride_quickestimate_PointerEntered" PointerExited="gride_quickestimate_PointerExited" PointerPressed="gride_quickestimate_PointerPressed" PointerReleased="gride_quickestimate_PointerReleased" Tapped="gride_quickestimate_Tapped"> 
    <Grid.Background> 
     <ImageBrush ImageSource="Image/inside_menu_normal.png"/>    
    </Grid.Background> 


    <TextBlock Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="3" x:Name="edit" Text="" FontSize="30" FontWeight="Bold" VerticalAlignment="Center" FontFamily="Global User Interface" Margin="10,112,29,111" ></TextBlock> 
    <Image Grid.Row="1" x:Name="img" Grid.Column="5" Source="Image/small_arrow.png" /> 


</Grid> 

我无法从在使用此用户控件的网页访问名为“编辑”的文本块。

+0

为什么不使用[x:FieldModifier指令](http://msdn.microsoft.com/zh-cn/库/ aa970905.aspx)? – 2013-04-23 17:50:52

+0

这不会很干净,控件的名称不是外部元素应该知道的东西。国际海事组织最好的办法就是暴露有用的属性,就像正常的控制一样。另外,它允许您在声明和使用UserControl时数据绑定这些属性。 – 2013-04-24 07:44:20

回答

0

这是完全正常的,因为您在usercontrol中的控件不能公开访问。你应该defin公共财产在你的用户控件,使这些控件可用...

+0

如何为在XAML中创建的控件定义公共属性。 – GY1 2013-04-23 12:45:04

+0

@ GY1这是一个非常好的问题,我很遗憾无法回答:-) – 2013-04-23 12:47:15

0

看起来你缺少你Grid.ColumnDefinition S和Grid.RowDefinition小号

<Grid x:Name="grid_amorti" ...> 
    <Grid.Background> 
     <ImageBrush ImageSource="Image/inside_menu_normal.png"/>    
    </Grid.Background> 
    <Grid.ColumnDefintion> 

    </Grid.ColumnDefinition> 
    <Grid.RowDefiniton> 

    </Grid.RowDefinition> 
</Grid> 

所以你实际上是乱放他们...