Activación del evento de clic izquierdo del mouse del cuadro de lista MVVM WPF

I am building a WPF application with MVVM architecture. In one form I have 2 listboxes and I want to perform filter based search. I am using a common search textbox, so I have to differentiate the search based on which listbox is selected. Please find my sample listbox below:

<HeaderedContentControl Header="Visible Objects:" Height="120" Width="250" Margin="20,20,20,0">
    <ListBox Name="lstObjects" Height="100" Margin="5" ItemsSource="{Binding ProfileObjTypeToBind, Mode=OneWay}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <CheckBox Name="chkbxVisibleObjects" Grid.Column="1"
                          Content="{Binding Path=Value}" IsChecked="{Binding Path=flag,Mode=TwoWay}">
                </CheckBox>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</HeaderedContentControl>
<HeaderedContentControl Header="User Groups to View:" Height="120" Width="250" Margin="20,10,20,10">
    <ListBox Name="lstGroups" Height="100" Margin="5" ItemsSource="{Binding ProfileUserGrpToBind, Mode=OneWay}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <CheckBox Name="chkAllowedGroups" Content="{Binding Path=GroupName}" 
                              IsChecked="{Binding Path=flag,Mode=TwoWay}">
                </CheckBox>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</HeaderedContentControl>

All I want to do is identify the listbox selected and perform the filtering based on text entered in textbox. Please help me

Muchas gracias por adelantado.

preguntado el 28 de mayo de 14 a las 14:05

your sample is missing -

Can you show us the code you have tried so far? Including you XAML form and ViewModel.cs file, which I assume will be bound to that form. -

Sorry, I had miss edited the code... please help -

1 Respuestas

You can't have a selected ListBox AND be able to write stuff to a TextBox. You can save the reference to your last ListBox though using SelectionChanged or some other method

private ListBox SelectedListBox = null;
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    SelectedListBox = (sender as ListBox); 
}

once you have a reference to your last selected ListBox you can add TextChanged event to your TextBox:

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
    if (SelectedListBox == null)
        return;

    string searchText = (sender as TextBox).Text;
    SelectedListBox.Items.Filter = (i) => { return ((string)i).Contains(searchText); };  // Or any other condition required
}

contestado el 28 de mayo de 14 a las 15:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.