当其内部 ComboBox 获得焦点时选择 ListBoxItem
我有一个 DataTemplate,它将是一个模板化的 ListBoxItem,这个 DataTemplate 有一个 其中的 ComboBox 当它具有焦点时我想要该模板的 ListBoxItem 代表被选中,这对我来说看起来很正确。但遗憾的是它不起作用 =(
所以这里真正的问题是在 DataTemplate 中是否可以获取或设置值 通过 DataTemplate.Trigger
获取 ListBoxItem.IsSelected
属性?
<DataTemplate x:Key="myDataTemplate"
DataType="{x:Type local:myTemplateItem}">
<Grid x:Name="_LayoutRoot">
<ComboBox x:Name="testComboBox" />
</Grid>
<DataTemplate.Triggers>
<Trigger Property="IsFocused" value="true" SourceName="testComboBox">
<Setter Property="ListBoxItem.IsSelected" Value="true" />
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
<ListBox ItemTemplate="{StaticResource myDataTemplate}" />
I have a DataTemplate that will be a templated ListBoxItem, this DataTemplate has a
ComboBox in it which when it has focus I want the ListBoxItem that this template
represents to become selected, this looks right to me. but sadly enough it doesn't work =(
So the real question here is within a DataTemplate is it possible to get or set the value
of the ListBoxItem.IsSelected
property via a DataTemplate.Trigger
?
<DataTemplate x:Key="myDataTemplate"
DataType="{x:Type local:myTemplateItem}">
<Grid x:Name="_LayoutRoot">
<ComboBox x:Name="testComboBox" />
</Grid>
<DataTemplate.Triggers>
<Trigger Property="IsFocused" value="true" SourceName="testComboBox">
<Setter Property="ListBoxItem.IsSelected" Value="true" />
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
<ListBox ItemTemplate="{StaticResource myDataTemplate}" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到了解决你问题的方法。
问题是,当您的列表框项上有一个控件并且单击该控件(例如输入文本或更改组合框的值)时,不会选择 ListBoxItem。
这应该可以完成工作:
-->使用此 FocusableListBox 代替 WPF 的默认 ListBox。
并使用这个ListBoxItem:
Treeview
也有这个问题,但是这个解决方案不适用于Treeview
,因为SelectedItem
ofTreeview
是只读
。因此,如果您能帮我解决树视图问题;-)
I found a solution for your problem.
The problem is that when you have a control on your listboxitem, and the control is clicked (like for inputting text or changing the value of a combobox), the ListBoxItem does not get selected.
this should do the job:
--> Use this FocusableListBox in stead of the default ListBox of WPF.
And use this ListBoxItem:
A
Treeview
does also have this problem, but this solution does not work for aTreeview
, 'causeSelectedItem
ofTreeview
isreadonly
.So if you can help me out with the Treeview please ;-)
我发现我更喜欢使用这个:
简单并且适用于所有列表框项目,无论里面有什么。
I found that I preferred to use this:
Simple and works for all the listboxitems, regardless of what's inside.
不知道为什么你的触发器不起作用。要捕获组合框(或列表框项内的任何控件)的获取焦点事件,您可以使用附加的路由事件。如果您在应用程序的其他部分需要这种行为,您也可以将代码放入派生列表框中。
XAML:
连接所有获得焦点事件的背后代码。
No idea why your trigger don't work. To catch the get focus event of the combo box (or any control inside a listbox item) you can use attached routed events. You could put the code also in a derived listbox if you need this behavior in other parts of your application.
XAML:
Code behind hooking up to all got focus events.