清除选择列表框控件项

时间:2015-02-16 07:17:16

标签: c# wpf xaml

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

    <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();
    }

全部谢谢

1 个答案:

答案 0 :(得分:0)

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

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

  1. MasterVm(包含ItemVm类型的项目集合)
  2. ItemVm(包含名为IsSelected的可绑定布尔值)
  3. 这是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);
    }