[英]Clear selection of Listbox control items
I have a listbox which contains a user control 我有一个包含用户控件的列表框
<Grid>
<ListBox x:Name="myListBox"
ItemsSource="{Binding Path=_myControl}"
ScrollViewer.VerticalScrollBarVisibility="Auto">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<local:SearchUsercontrol />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
The user control contains 用户控件包含
<DataGrid
x:Name="dataGrid"
BorderThickness="0"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window,AncestorLevel=1}}"
IsReadOnly="True"
GridLinesVisibility="None"
local:DataGridColumnsBehavior.BindableColumns="{Binding ColumnCollection}"
AutoGenerateColumns="False"
ItemsSource="{Binding Path=recordsBusinessObject}"
CellStyle="{StaticResource CellStyle}"
ColumnHeaderStyle="{StaticResource HeaderSTyle}">
</DataGrid>
When I select a row in datagrid I want the other row selection to clear in other listbox items. 当我在datagrid中选择一行时,我希望其他行选择在其他列表框项中清除。
In my Listbox changed event i tried this 在我的列表框更改事件我试过这个
void myListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
myListBox.UnselectAll();
}
Thanks All 谢谢大家
myListBox.UnselectAll(); myListBox.UnselectAll(); Unselects all ListBoxItems in myListBox and does not unselect DataGridRows within each ListBoxItem.
取消选择myListBox中的所有ListBoxItems,并且不在每个ListBoxItem中取消选择DataGridRows。
This is a solution which works for a situation where you got two viewModels: 这是一个适用于您有两个viewModel的情况的解决方案:
This is the modified code of simple version of your xaml: 这是xaml简单版本的修改代码:
<ListBox x:Name="myListBox"
SelectionChanged="myListBox_SelectionChanged"
ItemsSource="{Binding Path=MasterItems}">
<ListBox.ItemTemplate>
<DataTemplate>
<DataGrid ItemsSource="{Binding Items}">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<!-- here we bind to IsSelected of ItemVm -->
<Setter Property="IsSelected" Value="{Binding IsSelected}"/>
<!-- datagrid row is Selected => select listBoxItem -->
<EventSetter Event="Selected" Handler="DataGrid_Selected"/>
</Style>
</DataGrid.RowStyle>
</DataGrid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And the code behind: 而背后的代码:
//This event handler makes sure when a ListBoxItem is unselected
// it automatically unselects all inner items
private void myListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.RemovedItems != null)
if (e.RemovedItems.Count > 0)
foreach (var item in e.RemovedItems)
{
if (item is MasterVm)
//implement this method
(item as MasterVm).UnselectAll();
}
}
//This event handler makes sure that when datagrid row is Selected
// it automatically selects listBoxItem
private void DataGrid_Selected(object sender, RoutedEventArgs e)
{
var dg = sender as DataGridRow;
var lbi = FindAncestor<ListBoxItem>(dg);
lbi.IsSelected = true;
}
You'll need a helper like this: 你需要一个像这样的帮手:
public static T FindAncestor<T>(DependencyObject dependencyObject)
where T : DependencyObject
{
var parent = VisualTreeHelper.GetParent(dependencyObject);
if (parent == null) return null;
var parentT = parent as T;
return parentT ?? FindAncestor<T>(parent);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.