동일한 공간을 차지하는 두 개의 별도의 목록 상자에 도형을 표시하려고합니다. 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;
}
왜 당신은 하나의 목록 상자에있는 모든 모양을 가지고 있지 않은거야? 두 목록 상자 접근 방식으로 원하는 것을 얻지 못할 것 같습니다. – antlersoft
그게 내 마지막이야. 내가지도 상단에 항목을 dispaly 해요 그리고 그들은 두 개의 ListBoxes를 사용하는 이유는 전혀 동일한 데이터를 공유하지 않습니다. ItemsSource는 DataTables입니다. – George