繁体   English   中英

清除选择列表框控件项

[英]Clear selection of Listbox control items

我有一个包含用户控件的列表框

    <Grid>
    <ListBox x:Name="myListBox" 
             ItemsSource="{Binding Path=_myControl}" 
             ScrollViewer.VerticalScrollBarVisibility="Auto">

        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <local:SearchUsercontrol />
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</Grid>

用户控件包含

             <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>

当我在datagrid中选择一行时,我希望其他行选择在其他列表框项中清除。

在我的列表框更改事件我试过这个

   void myListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        myListBox.UnselectAll();
    }

谢谢大家

myListBox.UnselectAll(); 取消选择myListBox中的所有ListBoxItems,并且不在每个ListBoxItem中取消选择DataGridRows。

这是一个适用于您有两个viewModel的情况的解决方案:

  1. MasterVm(包含ItemVm类型的项集合)
  2. ItemVm(包含一个名为IsSelected的可绑定布尔值)

这是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>

而背后的代码:

//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;
}

你需要一个像这样的帮手:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM