2014-10-10 74 views
1

這就是我正在做的。將LongListSelector內部的用戶控件綁定到當前項目

UserControl.xaml

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}"> 
    <phone:LongListSelector 
     Name="MainList" 
     ItemsSource="{Binding}"> 
     <phone:LongListSelector.ItemTemplate> 
      <DataTemplate> 
       <StackPanel> 
       <views:PostView 
        DataContext="{Binding ElementName=MainList, Path=ItemsSource}"> 
       </views:PostView> 
       </StackPanel> 
      </DataPanel> 

我想在我的PostView與當前的ItemsSource元素的屬性來綁定控件。但是最新發生的事情是,例如我有5個對象列表設置爲ItemsSource,我的Post視圖中的每個元素都獲得5個值。

所以我得到25個PostViews初始化,5元的ItemsSource對象,而不是1

這是我PostView.xaml是它有助於

PostView.xaml

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneBackgroundBrush}"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"></RowDefinition> 
     <RowDefinition Height="Auto"></RowDefinition> 
    </Grid.RowDefinitions> 
    <ItemsControl 
     ItemsSource="{Binding}"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <StackPanel> 
        <TextBlock Text="{Binding Path=User.Username, Mode=OneWay}" /> 
       </StackPanel> 

用戶是我綁定到的模型中的Object屬性。 我該如何解決它?

回答

1

拿了幫助從this post設置爲我的數據的相關性字段中PostView.xaml.csUserControl.xaml

2

您將用戶控件綁定到整個集合,而不僅僅是一個項目。相反,如果DataContext="{Binding ElementName=MainList, Path=ItemsSource}"只使用DataContext="{Binding ElementName=MainList, Path=SelectedItem}"或只需DataContext="{Binding}"應該工作。

+0

設置它我試着用'{結合}'更早,只是試圖'的DataContext =「{綁定的ElementName = MainList,路徑=的SelectedItem}「'。但它不工作。有了這個,PostView甚至沒有被初始化。 – PratPor 2014-10-10 14:42:35

+0

我剛剛更新了我的問題,給出了PostView.xaml的概念 – PratPor 2014-10-10 14:57:58

相關問題