调整WPF列表框选择框 [英] Resize WPF ListBox selection box

查看:188
本文介绍了调整WPF列表框选择框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关一个项目,我已经实现了一个小型的智能感知像控制这只不过是一个的ListBox 。它的的DataTemplate 包含一个的StackPanel 持一份图像和一个的TextBlock 。没有其他的。正如你可以在我的控制的第一个截图中看到,ListBox的选择框中选择整个项目(这通常正是人们所期望的):

For a project I have implemented a small IntelliSense like control which is nothing than a ListBox. Its DataTemplate consist of a StackPanel holding one Image and one TextBlock. Nothing else. As you can see in the first screenshot of my control, the selection box of the ListBox selects the whole item (which usually is exactly what one would expect):

不过我偷从VS11图标低质量的,所以我想调整如Visual Studio的选择呢:

However my "stolen" icons from VS11 are low-quality so I wanted to adjust the selection like Visual Studio does:

Visual

您可以看到只有文本被选中(视觉表现确实忽略图像/图标),我想知道如何我可以实现这一行为,也

You can see that only the text is selected (the visual representation does ignore the image/icon) and I want to know how I can implement this behavior, too.

编辑:的图标只是GIF文件具有透明背景。我会用更好的取代他们,但尽管如此,我在如何获得所需的行为利益。220。

The icons are just GIF files with a transparent background. I will replace them with better ones, but nevertheless I am interestes in how to get the desired behavior.

推荐答案

下面是一个解决方案通过更换ListBoxItem中控制模板。在我的测试中,我只显示两个文本字符串,用第二个突出

Here's a solution by replacing the ListBoxItem control template. In my test I just display two text strings, with the second one highlighted.

<Page.Resources>
    <Style x:Key="ItemStyle" TargetType="ListBoxItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <ContentPresenter/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="Selected" TargetType="{x:Type TextBlock}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=IsSelected, 
                         RelativeSource={RelativeSource Mode=FindAncestor, 
                         AncestorType={x:Type ListBoxItem}}}" Value="True">
                <Setter Property="Background" 
                        Value="{x:Static SystemColors.HighlightBrush}"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

    <DataTemplate x:Key="ItemTemplate" DataType="{x:Type ViewModel:DataItem}">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto" SharedSizeGroup="c1"/>
                <ColumnDefinition Width="auto" SharedSizeGroup="c2"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Image Source="../Images/Collapse.png"/>
            <TextBlock Grid.Column="1" Text="{Binding Name}" Margin="0,0,5,0"/>
            <TextBlock Style="{StaticResource Selected}" 
                       Grid.Column="2" Text="{Binding Description}"/>
        </Grid>
    </DataTemplate>
</Page.Resources>

<Page.DataContext>
    <Samples:Page3ViewModel/>
</Page.DataContext>

<Grid>
    <ListBox 
        Grid.IsSharedSizeScope="True"
        SelectedIndex="2"
        HorizontalContentAlignment="Stretch"
        ItemsSource="{Binding Items}" 
        ItemContainerStyle="{StaticResource ItemStyle}"
        ItemTemplate="{StaticResource ItemTemplate}"/>
</Grid>



视图模型包含简单的数据项的集合

The view model contains a collection of simple data items

public class Page3ViewModel
{
    public Page3ViewModel()
    {
        Items = new List<DataItem>
            {
                new DataItem{Name = "One", Description = "First"},
                new DataItem{Name = "Two", Description = "Second"},
                new DataItem{Name = "Three", Description = "Third"},
                new DataItem{Name = "Four", Description = "Fourth"},
            };
    }
    public IEnumerable<DataItem> Items { get; private set; }
}



捐赠

Giving

这篇关于调整WPF列表框选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆