仅列表框项目中的文本可选择,文本之外的空间不可选择

发布于 2024-12-05 13:52:36 字数 462 浏览 1 评论 0原文

我有一个 WPF ListBox,其项目是 TextBlocks。当我单击文本时,SelectionChanged 处理程序将按预期调用。但是,如果我在项目内部单击,但不是直接在文本上单击,则不会调用处理程序。当文本项的长度变化很大时,这一点更加明显。如果我有两个项目:

foo
感叹号

“foo”项目右侧有很多空间,无法响应点击

<DataTemplate x:Key="NameTemplate">
  <TextBlock Text="{Binding Name}"/>
</DataTemplate>

...

<ListBox SelectionChanged="ListItemSelected" ItemTemplate="{StaticResource NameTemplate}"/>

I have a WPF ListBox whose items are TextBlocks. When I click on the text the SelectionChanged handler is called as expected. However, if I click inside the item, but not directly over the text the handler is not called. This is more apparent when the text items are of widely varying lengths. If I have two items:

foo
exclamation

The "foo" item has a lot of space to the right which doesn't respond to the click

<DataTemplate x:Key="NameTemplate">
  <TextBlock Text="{Binding Name}"/>
</DataTemplate>

...

<ListBox SelectionChanged="ListItemSelected" ItemTemplate="{StaticResource NameTemplate}"/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枕花眠 2024-12-12 13:52:36

我发现以下内容有效,但看起来相当冗长......

<ListBox SelectionChanged="ListItemSelected" ItemTemplate="{StaticResource NameTemplate}">
  <ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
       <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    </Style>
  </ListBox.ItemContainerStyle>
</ListBox>

关于如何更简洁地做到这一点有什么想法吗?或者将其放入 ItemTemplate 中的方法?我找不到在模板中执行相同操作的方法。

没有它的原版只是:

<ListBox SelectionChanged="ListItemSelected" ItemTemplate="{StaticResource NameTemplate}"/>

I found that the following works, but it seems rather verbose...

<ListBox SelectionChanged="ListItemSelected" ItemTemplate="{StaticResource NameTemplate}">
  <ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
       <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    </Style>
  </ListBox.ItemContainerStyle>
</ListBox>

Any ideas on how to do this more concisely? Or a way to put this in the ItemTemplate? I couldn't find a way to do the same in the template.

The orig without that was just:

<ListBox SelectionChanged="ListItemSelected" ItemTemplate="{StaticResource NameTemplate}"/>
等风也等你 2024-12-12 13:52:36

尝试。您可以去掉背景颜色,但这会显示 TextBlock 有多大。

     Background="Beige" HorizontalAlignment="Stretch"

Try. You can take away the background color but that will show you how big the TextBlock is.

     Background="Beige" HorizontalAlignment="Stretch"
甜味超标? 2024-12-12 13:52:36

您确定您单击的额外空白区域在您的ListBox“内部”吗?您确定您的 ListBox 跨越了那么大的宽度吗?

因为在我的情况下似乎没有发生......(以下 ListBoxWindow 的子级)

 <Window x:Class="WpfApplicationPathToImage.Window4"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window4" Height="100" Width="100">
    <ListBox SelectionChanged="ListBox_SelectionChanged">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Text}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.ItemsSource>
            <x:Array Type="{x:Type TextBlock}">
                <TextBlock Text="Text1"/>
                <TextBlock Text="Text2"/>
                <TextBlock Text="Text3"/>
                <TextBlock Text="Text4"/>
                <TextBlock Text="Text5"/>
                <TextBlock Text="Text6"/>
            </x:Array>
        </ListBox.ItemsSource>
    </ListBox>
 </Window>

我的 ListBox_SelectionChanged 甚至被正确调用如果我单击项目级别 TextBlock 边界之外的空白区域(前提是我实际上单击 ListBox内部某处)。

Are you sure the extra white space you are clickin on is "inside" your ListBox. Are you sure your ListBox is spanned across that much width?

Coz it doesnt seem to happen in my case.... (following ListBox is a child of a Window)

 <Window x:Class="WpfApplicationPathToImage.Window4"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window4" Height="100" Width="100">
    <ListBox SelectionChanged="ListBox_SelectionChanged">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Text}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.ItemsSource>
            <x:Array Type="{x:Type TextBlock}">
                <TextBlock Text="Text1"/>
                <TextBlock Text="Text2"/>
                <TextBlock Text="Text3"/>
                <TextBlock Text="Text4"/>
                <TextBlock Text="Text5"/>
                <TextBlock Text="Text6"/>
            </x:Array>
        </ListBox.ItemsSource>
    </ListBox>
 </Window>

My ListBox_SelectionChanged is correctly called even if I click the white space outside the item level TextBlock boundaries (provided that I am actually clicking somewhere inside the ListBox).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文