2011-03-17 130 views
0

嘿, 我寫了一個自定義的控制我的應用程序,所以事情變得對我來說更容易一點,到目前爲止,它的工作很好,現在我想將一些數據綁定到包裹的內容,但輸出說我有一個綁定錯誤,我的「項目」屬性搜索到的「CLIENT.UI.SinglePageControl」而不是「CLIENT.MainPage」 ......自定義控件和

<phone:PhoneApplicationPage 
x:Class="CLIENT.MainPage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:ui="clr-namespace:CLIENT.UI" 

mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" 
FontFamily="{StaticResource PhoneFontFamilyNormal}" 
FontSize="{StaticResource PhoneFontSizeNormal}" 
Foreground="{StaticResource PhoneForegroundBrush}" 
SupportedOrientations="PortraitOrLandscape" Orientation="Portrait" 
shell:SystemTray.IsVisible="True"> 
<Grid> 
    <ui:SinglePageControl HeaderTitle="Connections"> 
     <ui:SinglePageControl.PageContent> 

      <ListBox x:Name="MainListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}" Background="Blue" SelectionChanged="MainListBox_SelectionChanged"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Margin="0,0,0,17" Width="432" Orientation="Horizontal"> 
          <Image Source="UI/PICS/list_connection.png"/> 
          <TextBlock Text="{Binding ItemText}" TextWrapping="Wrap" Foreground="Black"/> 

         </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 

     </ui:SinglePageControl.PageContent> 
    </ui:SinglePageControl> 

</Grid> 

回答

2

試着給你的控制X :名稱值,然後在你的綁定聲明中包括的ElementName = < X:名稱>

<phone:PhoneApplicationPage 
    x:Name="pa" 
    x:Class="CLIENT.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:ui="clr-namespace:CLIENT.UI" 
    mc:Ignorable="d" 
    d:DesignWidth="480" 
    d:DesignHeight="768" 
    FontFamily="{StaticResource PhoneFontFamilyNormal}" 
    FontSize="{StaticResource PhoneFontSizeNormal}" 
    Foreground="{StaticResource PhoneForegroundBrush}" 
    SupportedOrientations="PortraitOrLandscape" 
    Orientation="Portrait" shell:SystemTray.IsVisible="True"> 
    <Grid> 
     <ui:SinglePageControl HeaderTitle="Connections"> 
      <ui:SinglePageControl.PageContent> 
       <ListBox x:Name="MainListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items, ElementName=pa}" Background="Blue" SelectionChanged="MainListBox_SelectionChanged"> 
        <ListBox.ItemTemplate>     
         <DataTemplate> 
          <StackPanel Margin="0,0,0,17" Width="432" Orientation="Horizontal"> 
           <Image Source="UI/PICS/list_connection.png"/> <TextBlock Text="{Binding ItemText, ElementName=pa}" TextWrapping="Wrap" Foreground="Black"/> 
          </StackPanel>      </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 
       </ui:SinglePageControl.PageContent> 
     </ui:SinglePageControl> 
    </Grid>
0

沒有看到你是如何設定數據上下文等很難說,但你給出的細節來看,你ListBox數據方面的水平是SinglePageControl.PageContent。通常情況下,父母(在MainPage)的數據上下文將被繼承了下來可視樹,所以實際上它不是在這種情況下,意味着SinglePageControl.PageContent正在建立它自己的數據上下文。如果你不需要它,然後只需刪除被設置它和數據上下文將被繼承的代碼(this.DataContext = this;爲例)。

如果你有充分的理由在頁面內容上設置數據上下文(這似乎是完全合理的),那麼你需要提供一種傳遞信息的方式,但我們需要了解更多關於什麼數據來自何處,以便提供一個良好的解決方案。