質問

ListBoxItemに、ListBoxを含む外部のUserControlからキャッチできる2つのイベントをトリガーしたいと思います。これが私がこれまでに得たものです:

<ListBox 
            Background="Black"
            Selected="listbox_selected"
            x:Name="listBox">

            <ListBox.Resources>
                <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsMouseOver,RelativeSource={RelativeSource Self}}" 
                         Value="True">
                            <Setter Property="IsSelected" Value="True" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ListBox.Resources>
        </ListBox>

これで、これは私のlistbox_selectedイベントを呼び出します。私が欲しいのは、IsMouseOverのときに別のイベントまたはプロパティを呼び出すことです。それを明確にするために、私はListBoxItem自体の背景/前景またはその他のプロパティを変更する方法を知っています。しかし、私は祖父母の何かを変えたいです。

役に立ちましたか?

解決

あなたはすでにそのイベントを持っています...からの静的なルーティングイベントを処理します ListBoxItem 祖先に「選択された」(および「選択されていない」)と呼ばれるクラスは、子孫の木のどこにでも「選択」イベントを処理しないことを条件としています...

  <Window x:Class="...."
          ...
          ListBoxItem.Selected="OnListBoxSelected">  
   <Grid>
      <ListBox ItemsSource="{Binding Employees}"
               DispalyMemberPath="Name"
               selectedValuePath="ID" >
        <ListBox.Resources>
            <Style TargetType="ListBoxItem"
                   BasedOn="{StaticResource
                                {x:Type ListBoxItem}}">
                <Style.Triggers>
                   <DataTrigger Binding="{Binding IsMouseOver,
                                            RelativeSource={RelativeSource
                                              Self}}"
                                Value="True">
                         <Setter Property="IsSelected"
                                 Value="True" />
                  </DataTrigger>
               </Style.Triggers>
           </Style>
       </ListBox.Resources> 
      </ListBox>
   </Grid>
 </Window>

そして背後にあるコードで...

    private void OnListBoxSelected(object sender, RoutedEventArgs e)
    {
        var window = sender as Window;
        var listBoxItem = e.OriginalSource as ListBoxItem;
        var selectedItem = listBoxItem.DataContext;
    }

お役に立てれば...

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top