0
<Window.Resources>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<EventTrigger RoutedEvent="PreviewGotKeyboardFocus">
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames
Storyboard.TargetProperty="(ListBoxItem.IsSelected)">
<DiscreteBooleanKeyFrame KeyTime="0" Value="True"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Opacity" Value="1.0" />
</Trigger>
</Style.Triggers>
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
Color="Blue"/>
</Style.Resources>
<Setter Property="Background" Value="Red" />
<Setter Property="MaxHeight" Value="75" />
</Style>
<DataTemplate x:Key="dtTeamInGame">
<WrapPanel >
<TextBox x:Name="txtPath" Text="{Binding Path = Number, Mode=TwoWay}" MinWidth="35" ></TextBox>
<TextBox x:Name="txtPath2" Text="{Binding Path = KnownName, Mode=TwoWay}" MinWidth="125" ></TextBox>
</WrapPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox x:Name="listBox" ItemTemplate="{DynamicResource dtTeamInGame}" HorizontalAlignment="Left" Height="100" Margin="29,184,0,0" VerticalAlignment="Top" Width="381">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_PreviewMouseLeftButtonDownHome" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="335,108,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
</Grid>
我有一個包含文本框的項目列表框,我想,當我點擊文本框的文本框是應該選擇的項目。添加之前:如何獲得一個ListBoxItem的click事件同時使用的ItemTemplate
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_PreviewMouseLeftButtonDownHome" />
</Style>
</ListBox.ItemContainerStyle>
如果我單擊項目內的文本框,將選擇列表框項目。如果我刪除上面的容器樣式,它會按預期工作,但我需要容器樣式,因爲如果用戶單擊已選擇的項目,我想執行一些任務。
如果您將ItemContainerStyle基於默認的ListBoxItem樣式,它有幫助嗎? '