2013-04-23 76 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> 

所以你實際上是亂放他們...