单击按钮时 ListViewItem 未选择
我有一个 ListView,其中 ListView 项目有按钮和文本块....
Senario:
我可以在不选择 ListView 项目的情况下单击按钮,即选择最后一个项目,然后如果我尝试单击第一次未选择第一项(在 DataGrid 中它确实选择)。
我无法使用 DataGrid,因为我在 ListView 中使用 CustomView。
如果您需要我的代码作为问题的参考,我会发布它。
在这方面的任何帮助都会很棒
My ListView :
<ListView Name="lv"
Grid.Row="1"
DisplayMemberPath="Name"
IsTextSearchEnabled="True"
ItemsSource="{Binding}"
KeyboardNavigation.DirectionalNavigation="Cycle"
SelectionMode="Single"
TextSearch.TextPath="{Binding Path=Person.Name}"
View="{Binding Path=SelectedItem,
ElementName=viewComboBox}" />
我的 CustomViews 的 DataTemplates :
<Style x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type CustomView:PlainView},
ResourceId=ImageView}"
BasedOn="{StaticResource {x:Type ListBox}}"
TargetType="{x:Type ListView}">
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="BorderThickness" Value=".5" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="ItemContainerStyle" Value="{Binding (ListView.View).ItemContainerStyle, RelativeSource={RelativeSource Self}}" />
<Setter Property="ItemTemplate" Value="{Binding (ListView.View).ItemTemplate, RelativeSource={RelativeSource Self}}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border Name="bd"
Margin="{TemplateBinding Margin}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ScrollViewer Margin="{TemplateBinding Padding}">
<WrapPanel KeyboardNavigation.DirectionalNavigation="Cycle"
Width="{Binding ActualWidth,
RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
MinWidth="{Binding (ListView.View).MinWidth,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type ListView}}}"
IsItemsHost="True"
ItemWidth="{Binding (ListView.View).ItemWidth,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type ListView}}}" Orientation="Vertical"
Height="{Binding ActualHeight,
RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type CustomView:PlainView},
ResourceId=ImageViewItem}"
BasedOn="{StaticResource {x:Type ListBoxItem}}"
TargetType="{x:Type ListViewItem}">
<Setter Property="Padding" Value="3" />
<Setter Property="Margin" Value="5" />
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="BorderThickness" Value="2" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
<DataTemplate x:Key="centralTile">
<StackPanel Width="80" Height="40" KeyboardNavigation.AcceptsReturn="True">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button x:Name="tempabc" Command="{Binding Path=Launch}" KeyboardNavigation.AcceptsReturn="True" >
<TextBlock Text="{Binding Path=Name}" FocusManager.IsFocusScope="True"></TextBlock>
</Button>
<Image Grid.Column="1" Source="Water lilies.jpg"/>
</Grid>
<TextBlock
HorizontalAlignment="Center"
FontSize="13"
Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
<CustomView:PlainView x:Key="plainView"
ItemTemplate="{StaticResource ResourceKey=centralTile}"
ItemWidth="100" />
<GridView x:Key="myGridView">
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button>
<TextBlock Text="{Binding Path=Name}" />
</Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
I have a ListView in which the ListView Items have button and textblock....
Senario :
I am able to click the button with out selecting the ListView Item i.e is the selection the last Item and then if i try to click the button of the first item the first time is not selected (In DataGrid it does select).
I Cannot use DataGrid as i am using CustomView in ListView.
If you need my code for reference of the problem i'll post it..
Any help in this regard would be great
My ListView :
<ListView Name="lv"
Grid.Row="1"
DisplayMemberPath="Name"
IsTextSearchEnabled="True"
ItemsSource="{Binding}"
KeyboardNavigation.DirectionalNavigation="Cycle"
SelectionMode="Single"
TextSearch.TextPath="{Binding Path=Person.Name}"
View="{Binding Path=SelectedItem,
ElementName=viewComboBox}" />
My DataTemplates for CustomViews :
<Style x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type CustomView:PlainView},
ResourceId=ImageView}"
BasedOn="{StaticResource {x:Type ListBox}}"
TargetType="{x:Type ListView}">
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="BorderThickness" Value=".5" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="ItemContainerStyle" Value="{Binding (ListView.View).ItemContainerStyle, RelativeSource={RelativeSource Self}}" />
<Setter Property="ItemTemplate" Value="{Binding (ListView.View).ItemTemplate, RelativeSource={RelativeSource Self}}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border Name="bd"
Margin="{TemplateBinding Margin}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ScrollViewer Margin="{TemplateBinding Padding}">
<WrapPanel KeyboardNavigation.DirectionalNavigation="Cycle"
Width="{Binding ActualWidth,
RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
MinWidth="{Binding (ListView.View).MinWidth,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type ListView}}}"
IsItemsHost="True"
ItemWidth="{Binding (ListView.View).ItemWidth,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type ListView}}}" Orientation="Vertical"
Height="{Binding ActualHeight,
RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type CustomView:PlainView},
ResourceId=ImageViewItem}"
BasedOn="{StaticResource {x:Type ListBoxItem}}"
TargetType="{x:Type ListViewItem}">
<Setter Property="Padding" Value="3" />
<Setter Property="Margin" Value="5" />
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="BorderThickness" Value="2" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
<DataTemplate x:Key="centralTile">
<StackPanel Width="80" Height="40" KeyboardNavigation.AcceptsReturn="True">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button x:Name="tempabc" Command="{Binding Path=Launch}" KeyboardNavigation.AcceptsReturn="True" >
<TextBlock Text="{Binding Path=Name}" FocusManager.IsFocusScope="True"></TextBlock>
</Button>
<Image Grid.Column="1" Source="Water lilies.jpg"/>
</Grid>
<TextBlock
HorizontalAlignment="Center"
FontSize="13"
Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
<CustomView:PlainView x:Key="plainView"
ItemTemplate="{StaticResource ResourceKey=centralTile}"
ItemWidth="100" />
<GridView x:Key="myGridView">
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button>
<TextBlock Text="{Binding Path=Name}" />
</Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与大多数事情一样,有多种方法可以做到这一点。这是我在一分钟内拼凑在一起的一个......
给定以下模型:
我希望向用户显示它们的集合并通过按钮选择一个。这意味着我的 ViewModel 中需要三样东西:
我创建 ViewModel 并将这些项目添加到其中。请注意,我更喜欢让我的 ViewModel 扩展 DependencyObject,而不是与 INPC 混淆。
最后,我将我的 UI 与一个基本的 ListView 组合在一起。
这一切都非常简单。我省略了 ICommand 实现,因为它很简单。
As with most things, there are a number of ways to do this. Here's one I just threw together in a minute...
Given the following model:
I wish to display a collection of them to the user and select one via a button. This means I need three things in my ViewModel:
I create my ViewModel and add these items to it. Please note, I prefer making my ViewModels extend DependencyObject rather than mess with INPC.
Lastly, I slap together my UI with a rudimentary ListView.
Its all pretty straight forward. I'm leaving out the ICommand implementation as it is trivial.