2011-07-01 4 views
1

동일한 공간을 차지하는 두 개의 별도의 목록 상자에 도형을 표시하려고합니다. Background를 Transparent와 {x : Null}로 설정했는데, mouseclick은 최상위 Listbox에 캡처되어 여전히 기본 ListBox에서 도형을 선택할 수 없습니다.두 개의 ListBox가 겹쳐져 있습니다. 선택 문제

다음은 문제를 재현하는 일부 샘플 코드입니다.

<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 장애인 사용 가능. 그럼 내가 대신 이벤트 처리기에서

<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> 

이벤트 핸들러와

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

그게 내 마지막이야. 내가지도 상단에 항목을 dispaly 해요 그리고 그들은 두 개의 ListBoxes를 사용하는 이유는 전혀 동일한 데이터를 공유하지 않습니다. 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

당신은 가시성 변수에리스트 박스를 결합 할 수 hittesting을 hittesting했다. 이렇게하면 아래쪽 상자를 선택해야하는 상황에있을 때 아래쪽 상자를 표시 할 수 있으며 맨 위 상자를 선택해야하는 경우 접힌 상태에서 맨 위와 비자가 나타납니다.

+0

문제는 항상 두 ListBox가 항상 표시된다는 것입니다. 덕분에 어쨌든 – George

관련 문제