1

同じスペースを占める2つの別々のリストボックスに図形を表示しようとしています。背景を透明と{x:Null}の両方に設定しましたが、マウスクリックが一番上のリストボックスにキャプチャされているため、基になるリストボックスから図形を選択できません。

これが問題を再現するサンプルコードです。

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

これが私が今のところこの問題を解決した方法ですが、私は他の提案を受け入れるだけではありません:)グリッドでヒットテストを有効にし、両方のリストボックスで無効にしました。次に、代わりにイベントハンドラーでヒットテストを行いました

<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;
}
4

2 に答える 2

2

CompositeCollectionをアイテムソースとして使用することにより、複数のデータセットを単一のリストボックスにバインドできます。

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

次に、データテンプレートを使用して、さまざまなデータ型の外観を制御できます。暗黙的なデータテンプレートを使用することも、 ItemTemplateSelectorを使用することもできます。

CompositeCollectionの使用とバインドの詳細については、次のリソースを参照してください:CollectionContainerをビューモデルのコレクションにバインドするにはどうすればよいですか?

于 2011-07-01T15:46:11.813 に答える
0

ListBoxをVisibility変数にバインドできます。このようにして、下のボックスが選択され、上のボックスが折りたたまれている状況で下のボックスが表示され、上のボックスが選択される必要がある場合はビザが表示されます。

于 2011-07-01T14:11:26.450 に答える