我有一个ListBox使用以下DataTemplate显示项目:
<DataTemplate x:Key="PersonTemplate" DataType="{x:Type DAL:ResultItem}" >
<StackPanel Width="280" >
<TextBox BorderThickness="0" IsReadOnly="True" Background="Transparent" Text="{Binding FullName1, Mode=OneWay}"/>
...
</StackPanel>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
我使用透明,只读,无边框TextBox而不是TextBlock,因为我希望用户能够选择要复制的文本.我应该采用不同的方式吗?我怎么写这个,这样当用户点击TextBox时,ListBoxItem也会被选中?
谢谢!
我已经添加了DataTemplate一个ListBox类来将我的集合绑定到:
<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>
Run Code Online (Sandbox Code Playgroud)
这完全符合我的要求.虽然当我点击时TextBox,ListBox不会自动设置关联ListItem的Selected.我可以在代码中执行此操作,但我更愿意将其用作组件(当时没有意外).
关于如何实现这一点的任何想法?
这似乎不起作用,它不会让我点击任何东西.我错过了什么.这是我的新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"> …Run Code Online (Sandbox Code Playgroud) 我有一个ListBox用ListBoxItems一个模板,以便它们包含TextBoxes

当TextBox聚焦时我希望ListBoxItem被选中.我发现的一个解决方案如下:
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True"></Setter>
</Trigger>
</Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)
这很有效,但是当TextBox失去焦点时,选择也是如此.
有没有办法防止这种情况发生?
我有一个列表框,在datatemplate中我有一个Expander.
如果我单击Expander Header,扩展器将展开内容区域,但不会使父ListBoxItem选中.
如果我单击Expander的Expanded Content Zone,则会选择父ListBoxItem.
单击expandderHeader时如何使内容扩展并且父列表框项被选中?
listbox ×4
wpf ×4
listboxitem ×2
selection ×2
textbox ×2
.net ×1
c# ×1
childcontrol ×1
datatemplate ×1
xaml ×1