locked
wpf listbox dynamic search RRS feed

  • Question

  • Hello,
    I need advice 1) how to make dynamic searches in the listbox, as shown and 2) I have a DataTemplate for listboxitem and I want to create a trigger so that when the user clicks the mouse double-click that comes to light Path. Thanks a lot 

    So when I attach source code My PlayList

    dynamic listbox search

    My PlayList


    • Edited by ORRNY66 Friday, September 4, 2015 5:55 PM
    • Moved by Kristin Xie Monday, September 7, 2015 6:23 AM
    Friday, September 4, 2015 5:49 PM

Answers

  • Please only ask one question per post.

    Your title should be the question.

    The forum is a searchable resource. If you follow the rules.

    .

    1)

    I would use a listcollectionview for filtering as explained here:

    http://social.technet.microsoft.com/wiki/contents/articles/26673.aspx

    You can match using .Contains

    https://msdn.microsoft.com/en-us/library/dy85x1sa%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    2)

    I don't follow what you mean. A single click will select an entry in a listbox and you could use that with a trigger to change the background colour if that's what you mean.

    Like this:

    http://stackoverflow.com/questions/28686752/changing-wpf-listbox-selecteditem-text-color-and-highlight-background-color-usin

    <Window x:Class="ListBoxStyle.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:src="clr-namespace:ListBoxStyle"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <Style x:Key="_ListBoxItemStyle" TargetType="ListBoxItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <Border Name="_Border"
                                    Padding="2"
                                    SnapsToDevicePixels="true">
                                <ContentPresenter />
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected" Value="true">
                                    <Setter TargetName="_Border" Property="Background" Value="Yellow"/>
                                    <Setter Property="Foreground" Value="Red"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Window.Resources>
        <Grid>
            <ListBox ItemContainerStyle="{DynamicResource _ListBoxItemStyle}"
                     Width="200" Height="250"
                     ScrollViewer.VerticalScrollBarVisibility="Auto"
                     ScrollViewer.HorizontalScrollBarVisibility="Auto">
                <ListBoxItem>Hello</ListBoxItem>
                <ListBoxItem>Hi</ListBoxItem>
            </ListBox>
        </Grid>
    </Window>

    If you really want to do it on double click then you could use an eventtrigger and a storyboard.

    http://www.wpf-tutorial.com/styles/trigger-datatrigger-event-trigger/



    • Edited by Andy ONeill Friday, September 4, 2015 6:21 PM
    • Proposed as answer by Xavier Xie-MSFT Tuesday, September 15, 2015 6:37 AM
    • Marked as answer by Xavier Xie-MSFT Wednesday, September 16, 2015 9:19 AM
    Friday, September 4, 2015 6:16 PM