2011-06-17 57 views
0

我有一個列表框,它使用ItemsPanel中的一個畫布佈置使用綁定在邊緣上的項目,這一切都很好。然而,單擊列表框只會觸發一次,並始終返回列表框中的最後一項。WP7 ListBox ItemPanel SelectChanged選擇lastItem

驗證碼:

<Grid> 
    <ListBox Name="lstItems" ItemsSource="{Binding Itemss}" Width="Auto" Height="497" Margin="0,40,0,10" SelectionChanged="ListBox_SelectionChanged"> 
    <ListBox.ItemsPanel> 
     <ItemsPanelTemplate> 
     <Canvas Name="cnvItems"> 
     </Canvas> 

     </ItemsPanelTemplate> 
    </ListBox.ItemsPanel> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
     <Grid Margin="{Binding XYMargin}"> 
      <Border BorderBrush="Silver" BorderThickness="5" Height="{Binding XYWidth}" Width="{Binding XYWidth}" HorizontalAlignment="Left" Margin="0,0,0,0" Name="border5" VerticalAlignment="Top" Background="#81FFFFFF" CornerRadius="10" /> 
      <StackPanel Margin="5,5,0,0" HorizontalAlignment="Center" Orientation="Horizontal"> 
       <TextBlock Margin="0,0,0,0" Width="{Binding XYWidth}" Text="{Binding Label1}" TextAlignment="Left" FontSize="30" /> 
      </StackPanel> 
     </Grid> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
    </ListBox> 

</Grid> 

任何想法,爲什麼我只得到列表中的最後一個項目?

+0

你有你的射擊代碼 'ListBox_SelectionChanged'? – 2011-06-17 09:15:17

回答

1

如果您使用畫布進行佈局,它將項目放置在ItemContainerStyle之一上面,因此最後放置在畫布上的項目將是唯一可見的項目。想象一下將一組玻璃面板放在另一面上,並且每件新玻璃上的物品都會越來越低。即使該物品在底部繪製,您仍然只能觸摸頂部玻璃。

解決方案:

嘗試移動

保證金= 「{結合XYMargin}」

你的數據模板,並把到您的ItemContainerStyle。

例子:(簡化爲清楚起見):

<ListBox.ItemContainerStyle> 
    <Style TargetType="ListBoxItem"> 
     <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ListBoxItem"> 
       <Border x:Name="LayoutRoot" 
        Margin="{Binding XYMargin}">            
        <ContentControl 
         x:Name="ContentContainer" 
         ContentTemplate="{TemplateBinding ContentTemplate}" 
         Content="{TemplateBinding Content}" 

       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
     </Setter> 
    </Style> 
</ListBox.ItemContainerStyle> 
+0

這是非常好的,它工作正常。正是我想要的。謝謝你的幫助。 – 2011-06-18 05:44:12