[英]MVVM WPF listbox mouse leftclick event firing
I am building a WPF application with MVVM architecture. 我正在使用MVVM体系结构构建WPF应用程序。 In one form I have 2 listboxes and I want to perform filter based search.
在一种形式中,我有2个列表框,我想执行基于过滤器的搜索。 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
请帮我
Thanks a lot in advance. 非常感谢。
You can't have a selected ListBox AND be able to write stuff to a TextBox. 您无法选择ListBox,也无法将内容写入TextBox。 You can save the reference to your last ListBox though using SelectionChanged or some other method
您可以使用SelectionChanged或其他方法将对最后一个ListBox的引用保存到
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: 一旦有了对最后选择的ListBox的引用,就可以将TextChanged事件添加到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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.