Ber*_* L. 1 wpf listview textbox selecteditem itemtemplate
我有以下ListView(简化):
<ListView Name="lvwNotes" KeyUp="lvwNotes_KeyUp">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<DockPanel Background="LightGray">
<TextBlock DockPanel.Dock="Right" Text="{Binding Path=Author}" />
<TextBlock Text="{Binding Path=Timestamp}" />
</DockPanel>
<TextBox Text="{Binding Path=Text}"
GotFocus = "lvwNotes_TextBox_GotFocus"
TextWrapping="Wrap" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)
通过单击更改所选项目仅在用户使用TextBlocks单击DockPanel时有效,但在单击TextBox时则无效.我想要实现的是将所选项目设置为包含用户单击的TextBox的项目.
我设法通过与TextBox相关的ListViewItem:
private void lvwNotes_TextBox_GotFocus(object sender, RoutedEventArgs e) {
DependencyObject o = Tools.GetAncestorByType((DependencyObject)sender, typeof(ListViewItem));
if (!o.Equals(null)) {
// code to select this ListViewItem
}
}
Run Code Online (Sandbox Code Playgroud)
但是设定
lvwNotes.SelectedIten = o ;
Run Code Online (Sandbox Code Playgroud)
仍然没有效果.我也尝试过Dispatcher.BeginInvoke的一些技巧,但说实话,我并不确切知道我在那里做什么.
将其添加到您的代码中
<ListView.Resources>
<Style TargetType="ListViewItem">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
</ListView.Resources>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3501 次 |
| 最近记录: |