在Datagrid WPF MVVM中取消选择一行(显示窗口后)

时间:2013-09-20 10:52:34

标签: c# wpf xaml mvvm catel

我在其中创建了一个带有DataGrid的窗口,该窗口与ObservableCollection绑定:

<GroupBox Header="Kunden" Grid.Column="0">
            <DataGrid AutoGenerateColumns="False"
                          Height="Auto"
                          HorizontalAlignment="Stretch"
                          x:Name="customersDataGrid"
                          VerticalAlignment="Top" Width="Auto"
                          ItemsSource="{Binding Path=Customers, Mode=TwoWay}"
                          IsReadOnly="True"
                          CanUserResizeColumns="False"
                          ClipboardCopyMode="IncludeHeader"
                          CanUserAddRows="False"
                          SelectionMode="Single"
                          ColumnHeaderStyle="{DynamicResource ResourceKey=DataGridColumnHeaderBold}"
                          GridLinesVisibility="None"
                          Background="White"
                          IsSynchronizedWithCurrentItem="True"                              
                          FontFamily="Century Gothic"
                          SelectedItem="{Binding Path=SelectedCustomer,
                                                 Mode=TwoWay,
                                                 UpdateSourceTrigger=PropertyChanged}">
                <!--Trigger-Verhalten-->
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SelectionChanged">
                        <catel:EventToCommand Command="{Binding CustomerSelectionChangedCmd}"
                                                        DisableAssociatedObjectOnCannotExecute="False"
                                                        PassEventArgsToCommand="True" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>

                <DataGrid.Columns>
                    <DataGridTextColumn Header="Id"
                                        Binding="{Binding Path=CustomerId}"
                                        FontSize="14" Width="Auto" />
                    <DataGridTextColumn Header="Name"
                                        Binding="{Binding Path=CustomerName}"
                                        FontSize="14"  Width="Auto"/>
                </DataGrid.Columns>
            </DataGrid>
        </GroupBox>

Customers = ObservableCollection(客户的属性:CustomerId,CustomerName)

当我在ViewModel中将SelectedCustomer设置为null时,将取消选择数据网格。 但是我需要在窗口启动后保持未选中的数据网格。 我试图在ViewModel的构造函数中设置SelectedCustomer,但它不起作用。 它只适用于我在后面的代码中执行此操作:customersDatagrid.SelectedItem = null。

有没有任何解决方案以MVVM方式执行此操作?

提前致谢并致以最诚挚的问候,

胡志明

3 个答案:

答案 0 :(得分:1)

嗨大家非常感谢你的答案,特别是Geert的建设性提示。删除代码隐藏文件后,我可以解决这个问题。

为什么? 这可能就是答案:

从XAML文件中,在编译期间创建了两个文件:

  1. MainWindow.g.cs - MainWindow类所在的位置。该类加载第二个文件 -

  2. MainWindow.Baml是我们在进行某种编译后的XAML(它实际上是预标记化 - 提前解析文件,以便在运行时加载比加载未解析的XML文件更快)

  3. 这两个文件的加载和连接正在MainWindow.g.cs中的InitializeComponent方法中执行,只是...现在我们已经摆脱了CodeBehind无人调用这个方法。发生的事情是没有任何东西加载BAML文件,因此一切都保持完全为空。

    我是从这里得到的:http://www.codeproject.com/Articles/377094/How-to-make-WPF-behave-as-if-MVVM-is-supported-out

    我认为在构造ViewModel之后正在调用InitializeComponent()。这就是为什么我看到在由vm构造函数将其设置为null后第二次设置了SelectedCustomer属性。

答案 1 :(得分:0)

我认为视图模型上的Initialize方法是个不错的选择。加载视图时会调用此方法,如果默认选择一个项目,则该方法是取消选择项目的完美方式。

答案 2 :(得分:0)

我不建议您绑定网格的SelectedItem

而是将其作为参数发送到您的命令中:

 <i:Interaction.Triggers>
    <i:EventTrigger EventName="SelectionChanged">
        <i:InvokeCommandAction Command="{Binding CustomerSelectionChangedCmd}"  CommandParameter="{Binding ElementName=customersDataGrid, Path=SelectedItem}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>

在viewModel中,使用所选项目

处理该命令
    CustomerSelectionChangedCmd= new DelegateCommand<object>(selectedItem => 
    {
        var selected = selectedItem as Customer;

        // Do whatever you want with selected customer ...

    });

并在xaml中的Grid定义中,将initial selectedItem设置为null

 SelectedItem="{x:Null}"

这种方式直到第一次触发SelectionChanged事件,SelectedItem将为空。