2011-07-01 42 views
1

我試圖在佔據相同空間的兩個單獨的列表框中顯示形狀。我已將Background設置爲Transparent和{x:Null},但鼠標點擊仍被頂部列表框捕獲,因此我無法從底層ListBox中選擇任何圖形。兩個重疊的ListBoxes。選擇問題

下面是一些重現問題的示例代碼。

<Grid> 
    <!-- ListBox 1 --> 
    <ListBox Background="{x:Null}"> 
     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <Canvas Background="{x:Null}"/> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Grid Background="Transparent"> 
        <Ellipse Width="100" Height="100" Stroke="Blue" StrokeThickness="10"/> 
       </Grid> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
     1 
    </ListBox> 

    <!-- ListBox 2 --> 
    <ListBox Background="{x:Null}"> 
     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <Canvas Background="{x:Null}"/> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 
     <ListBox.ItemContainerStyle> 
      <Style TargetType="ListBoxItem"> 
       <Setter Property="Canvas.Left" Value="100"/> 
       <Setter Property="Canvas.Top" Value="100"/> 
      </Style> 
     </ListBox.ItemContainerStyle> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Ellipse Width="100" Height="100" Stroke="Blue" StrokeThickness="10"/> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
     1 
    </ListBox> 
</Grid> 

這是我如何解決了這個問題,現在,但我比開放更多的其他建議:)我啓用hittesting在網格和殘疾人它兩個ListBox。然後我做了hittesting在事件處理程序,而不是

<Grid MouseDown="Grid_MouseDown" 
     IsHitTestVisible="True" 
     Background="Transparent"> 

    <!-- ListBox 1 --> 
    <ListBox Background="Transparent" 
      IsHitTestVisible="True" 
      ..> 
    </ListBox> 

    <!-- ListBox 2 --> 
    <ListBox Background="Transparent" 
      IsHitTestVisible="True" 
      ..> 
    </ListBox> 
</Grid> 

事件處理程序,並hittesting

private void Grid_MouseDown(object sender, MouseButtonEventArgs e) 
{ 
    Grid grid = sender as Grid; 
    Point ptCurrent = e.GetPosition(grid); 
    VisualTreeHelper.HitTest(grid, null, new HitTestResultCallback(HitTestCallback), new PointHitTestParameters(ptCurrent)); 
} 
public HitTestResultBehavior HitTestCallback(HitTestResult htrResult) 
{ 
    ListBoxItem listBoxItem = GetVisualParent<ListBoxItem>(htrResult.VisualHit); 
    if (listBoxItem != null) 
    { 
     listBoxItem.IsSelected = true; 
     return HitTestResultBehavior.Stop; 
    } 
    return HitTestResultBehavior.Continue; 
} 
public T GetVisualParent<T>(object child) where T : Visual 
{ 
    DependencyObject c = child as DependencyObject; 
    while ((c != null) && !(c is T)) 
    { 
     c = VisualTreeHelper.GetParent(c); 
    } 
    return c as T; 
} 
+0

你爲什麼不有一個列表框中的所有形狀?雙列表框方法似乎不太可能讓你得到你想要的。 – antlersoft

+0

這是我最後一次了......我在地圖上顯示項目,並且他們根本不共享相同的數據,所以這就是我使用兩個ListBox的原因。 ItemsSource是DataTables – George

回答

2

可以通過使用一個CompositeCollection作爲項目源多組數據綁定到單個列表框:

<ListBox ...> 
    <ListBox.ItemsSource> 
    <CompositeCollection> 
     <CollectionContainer Collection={Binding ...}" /> 
     <CollectionContainer Collection={Binding ...}" /> 
    </CompositeCollection> 
    </ListBox.ItemsSource> 
</ListBox> 

然後可以使用數據模板來控制不同的數據類型的外觀。您可以使用隱式數據模板,也可以使用ItemTemplateSelector

有關使用和結合於CompositeCollection的更多信息,請參閱該資源:How do you bind a CollectionContainer to a collection in a view model?

+0

謝謝,這就是我一直在尋找! – George

0

則可以將列表框綁定到一個可見的變量。通過這種方式,您可以在底部選框處於可選狀態時將底部框顯示出來,並且當您需要選擇頂部框時,頂部框會摺疊並簽名。

+0

問題是,我的ListBoxes都是可見的,無論如何謝謝 – George