如果 ItemTemplate 中的 TextBox 获得焦点,则选择 ListBoxItem [英] Select ListBoxItem if TextBox in ItemTemplate gets focus
问题描述
我已将 DataTemplate
添加到 ListBox
类以将我的集合绑定到:
I have added a DataTemplate
to a ListBox
class to bind my collection to:
<ListBox x:Name="lstEmails" Height="259" Margin="12,0,12,41" Width="276"
SelectionChanged="lstEmails_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Visibility="Hidden" Content="{Binding ID}"></Label>
<TextBox Width="200" Text="{Binding EmailAddress}"></TextBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这正是我想要的.虽然当我单击 TextBox
时,ListBox
不会自动将关联的 ListItem
设置为 Selected
.我可以在代码中做到这一点,但我更愿意将它用作一个组件(那就不足为奇了).
This does exactly what I want it to do. Although when I click on the TextBox
, the ListBox
does not automatically set the associated ListItem
as Selected
. I could do this in code, but I would prefer to use this as a component (no surprises there then).
关于如何实现这一点的任何想法?
Any ideas on how to achieve this?
这似乎不起作用,它不会让我点击任何东西.我是不是错过了什么.这是我的新 XAML.
That doesn't seem to work, it won't let me click on anything. Have I missed something. Here is my new XAML.
<UserControl.Resources>
<!--<TextBox x:Key="TB" x:Name="TextBoxInsideListBoxItemTemplate">
<TextBox.Style>-->
<Style TargetType="{x:Type TextBox}">
<Setter Property="IsHitTestVisible" Value="False" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListBoxItem}, AncestorLevel=1}}"
Value="True">
<Setter Property="IsHitTestVisible" Value="True" />
</DataTrigger>
</Style.Triggers>
</Style>
<!--</TextBox.Style>
</TextBox>-->
</UserControl.Resources>
<Grid>
<ListBox x:Name="lstEmails" Height="259" Margin="12,0,12,41" Width="276" SelectionChanged="lstEmails_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<!--<Label Visibility="Hidden" Content="{Binding ID}"></Label>-->
<TextBox Width="220" Text="{Binding EmailAddress}" >
</TextBox>
<!--<TextBox Width="220" Text="{Binding EmailAddress}" GotFocus="TextBox_GotFocus"></TextBox>-->
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Width="20" Margin="12,0,0,12" Name="btnAdd" VerticalAlignment="Bottom" Click="btnAdd_Click" Height="23" HorizontalAlignment="Left">+</Button>
<Button Width="20" HorizontalAlignment="Left" Margin="30,0,0,12" Name="btnRemove" VerticalAlignment="Bottom" Click="btnRemove_Click" Height="23">-</Button>
<Button Height="23" HorizontalAlignment="Right" Margin="0,0,12,12" Name="btnApply" VerticalAlignment="Bottom" Width="49" Click="btnApply_Click">Apply</Button>
</Grid>
我认为点击两次是很好的功能.
I think the click twice bit is good functionality.
推荐答案
如果你有多个 ListBox
的实例,那么你可以考虑使用你的自定义列表框(通过从 ListBox代码>).请参阅此处的说明.
If you have multiple instance of ListBox
then you may consider using your custom listbox (by deriving it from ListBox
). See the explanation here.
或者,如果您只有 1 个(或只有少量)这样的 ListBox
并且不想为此创建单独的类,请使用此 hack:
Or, use this hack if you have only 1 (or only small number of) such ListBox
and don't want to create a separate class for that:
<TextBox x:Name="TextBoxInsideListBoxItemTemplate" ... >
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="IsHitTestVisible" Value="False" />
<Style.Triggers>
<DataTrigger
Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ListBoxItem}, AncestorLevel=1}}"
Value="True">
<Setter Property="IsHitTestVisible" Value="True" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
请注意,您必须再次单击才能编辑 TextBox
中的文本(在我看来这真的很酷).
Note that you'll have to click once again to edit text in the TextBox
(which is actually cool according to me).
这篇关于如果 ItemTemplate 中的 TextBox 获得焦点,则选择 ListBoxItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!