0

我有ListBox一个DataTemplate。模板上有一个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>

当您在命令中单击时Button传递属性值SelectedItem。当您单击ListItem然后单击Button- 一切都很好。当我立即单击Button命令中的 - 时,将传递 null。当您按下位于此的按钮时,它ListItem不会获得焦点ListItem。如何解决这个问题呢?

4

1 回答 1

0

您可以检查触发器中的IsKeyboardFocusWithin属性ListBoxItems,以确定子项(如您的按钮)是否具有焦点,IsSelected如果是这种情况则设置为 true。

你可以这样设置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>
于 2013-03-29T18:25:18.903 回答