WPF:当您按下此ListItem上的按钮时,ListItem不会获得焦点

[英]WPF: ListItem does not receive focus when you press the button located on this ListItem


I have a ListBox with a DataTemplate. The template has a Button on it. Each item in the list shows the entity.

我有一个带有DataTemplate的ListBox。模板上有一个Button。列表中的每个项目都显示实体。

<ListBox Grid.Column="0"
             x:Name="ThemesList"
             ItemsSource="{Binding Themes}"
             HorizontalAlignment="Left"
             VerticalAlignment="Top"
             SelectedItem="{Binding SelectedTheme}" 
             ItemTemplate="{StaticResource ThemeListTemplate}"/>

<DataTemplate x:Key="ThemeListTemplate">
    <Grid Grid.Column="1"
          Grid.Row="0"
          HorizontalAlignment="Right" 
          Margin="10">

          <Grid.RowDefinitions>
              <RowDefinition/>
              <RowDefinition/>
          </Grid.RowDefinitions>

          <Button Grid.Row="0" 
                  HorizontalAlignment="Left"
                  Style="{StaticResource ElementButton}"
                  Command="{Binding Path=DataContext.ThemeEditorViewModel.OpenThemeEditorCommand, ElementName=ThemesBacklog}"
                  CommandParameter="{Binding Path=SelectedItem, ElementName=ThemesList}">

                <TextBlock Text="Edit"/>
          </Button>

        <Button Grid.Row="1" 
                HorizontalAlignment="Left"
                Style="{StaticResource ElementButton}"
                Command="{Binding Path=DataContext.ThemeDeleteCommand, ElementName=ThemesBacklog}"
                CommandParameter="{Binding Path=SelectedItem, ElementName=ThemesList}">

                <TextBlock Text="Delete"/>
        </Button>
    </Grid>
</DataTemplate>

When you click on Button in the command is passed the property value SelectedItem. When you click on ListItem and then click Button - all fine. When I at once click on the Button - in the command is passed null. That is ListItem does not receive focus when you press the button located on this ListItem. How to solve this problem?

当您在命令中单击Button时传递属性值SelectedItem。当您单击ListItem然后单击按钮 - 一切正常。当我立刻点击按钮 - 在命令中传递null。也就是说当按下位于此ListItem上的按钮时,ListItem不会获得焦点。如何解决这个问题呢?

1 个解决方案

#1


0  

You could examine the IsKeyboardFocusWithin property on the ListBoxItems in a trigger, to find out whether a child (like your button), has focus, and set IsSelected to true if that is the case.

您可以检查触发器中ListBoxItems上的IsKeyboardFocusWithin属性,以查明子项(如您的按钮)是否具有焦点,并在这种情况下将IsSelected设置为true。

You do this by setting the ItemContainerStyle like this:

你可以这样设置ItemContainerStyle:

<ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}">
        <Style.Triggers>
            <Trigger Property="IsKeyboardFocusWithin" Value="True">
                <Setter Property="IsSelected" Value="True" />
            </Trigger>
        </Style.Triggers>
    </Style>
</ListBox.ItemContainerStyle>
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2013/03/29/54d102b593c1326a39ab15425626f120.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告