CheckBox Checked事件引发NullReferenceException_

时间:2012-04-11 06:44:00

标签: c# wpf xaml checkbox

我有一个奇怪的情况,我不知道如何处理...在我的项目中,我有以下TreeView,它在TreeViewItem中保存复选框:

<DataGrid x:Name="dataGridVirtualMachinesInfo" ItemsSource="{Binding Path=SelectedItem.VMImagesInfo, ElementName=comboBoxUserName}"
                      Style="{DynamicResource RoundDataGridStyle}" RowDetailsVisibilityChanged="dataGridVirtualMachinesInfo_RowDetailsVisibilityChanged"
                      MouseLeftButtonUp="DataGrid_MouseLeftButtonUp" CanUserAddRows="False" Margin="24" AutoGenerateColumns="False" ClipToBounds="True"
                      AreRowDetailsFrozen="false" CanUserResizeRows="False" RowDetailsTemplate="{DynamicResource VMRowDetailsDataTemplate}"
                      Visibility="Visible" Background="White" VerticalAlignment="Top">
                <DataGrid.Resources>
                    <DataTemplate x:Key="VMRowDetailsDataTemplate">
                        <Border BorderThickness="2">
                            <Border.BorderBrush>
                                <SolidColorBrush Color="{DynamicResource ColorBorder}"/>
                            </Border.BorderBrush>
                            <Grid x:Name="gridRowDetail" Height="450" Width="Auto" Margin="0" Background="White" VerticalAlignment="Stretch" >
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="2*"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="10*"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="5*"/>
                                </Grid.RowDefinitions>
                                <Grid x:Name="gridCurrentVersion" Margin="0" Grid.RowSpan="1" Grid.Row="0">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition/>
                                        <ColumnDefinition/>
                                    </Grid.ColumnDefinitions>
                                    <Label x:Name="lableCurrentVersion" Content="Current Version : " HorizontalAlignment="Left"
                                           Margin="5,0,0,0" VerticalAlignment="Center" Width="Auto" FontWeight="Bold"/>
                                    <TextBox x:Name="textBoxCurrentVersion" Text="{Binding CurrentVersion}" HorizontalAlignment="Left"
                                             Margin="105,0,0,0" TextWrapping="Wrap"  VerticalAlignment="Center" RenderTransformOrigin="0.432,0.365"
                                             Width="100" Height="Auto" Grid.Column="0" AcceptsReturn="True"/>
                                    <Button x:Name="buttonRestoreDefaults" Content="Restore Defaults" HorizontalAlignment="Right"
                                            Margin="0,2,95,2" VerticalAlignment="Center" Width="90" RenderTransformOrigin="0.213,-0.401" Grid.Column="0"
                                            d:LayoutOverrides="HorizontalAlignment, GridBox" Height="30" Grid.ColumnSpan="2"/>
                                    <Button x:Name="buttonSaveInfo" Content="Save Info" Click="buttonSaveInfo_Click" Width="90"
                                            Background="#FF2DD420"  Margin="0,2" HorizontalAlignment="Right" Height="30"
                                            VerticalAlignment="Center" RenderTransformOrigin="1.125,0.599" Grid.Column="1"/>
                                </Grid>
                                <GridSplitter x:Name="gridSplitterCurrVerAndSaveInfoButton" 
                                    ResizeDirection="Rows" 
                                    Grid.Column="0" 
                                    Grid.ColumnSpan="2" 
                                    Grid.Row="1" 
                                    Width="Auto" 
                                    Height="3" 
                                    HorizontalAlignment="Stretch" 
                                    VerticalAlignment="Stretch" 
                                    Margin="0">
                                    <GridSplitter.Background>
                                        <SolidColorBrush Color="{DynamicResource ColorBorder}"/>
                                    </GridSplitter.Background>
                                </GridSplitter>
                                <Grid x:Name="gridFeaturesAndProps" Margin="0" Grid.Row="2">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="3*"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid x:Name="gridFeatures">
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="2*"/>
                                            <RowDefinition Height="15*"/>
                                        </Grid.RowDefinitions>
                                        <Label x:Name="lableFeaturesSelection" Content="Features Selection" HorizontalAlignment="Left"
                                               VerticalAlignment="Top" FontWeight="Bold" Grid.Row="0" Margin="5,0,0,0"/>
                                        <TreeView x:Name="GroupViewFeaturesList" ItemsSource="{Binding Path=InstallPackage.Features.FeaturesTree}"
                                              ItemContainerStyle="{DynamicResource  TreeViewItemStyle}"
                                              Margin="5,0,0,5" FontFamily="Pescadero" BorderThickness="0" FontSize="13.333"
                                              Width="Auto" Height="Auto" Grid.Row="1" VerticalAlignment="Stretch" d:LayoutOverrides="GridBox">
                                            <TreeView.Resources>
                                                <Style x:Key="TreeViewItemStyle" TargetType="{x:Type TreeViewItem}">
                                                    <!--<Setter Property="Visibility" Value="{Binding Path=IsVisible, Mode=TwoWay, Converter={StaticResource boolToVis}}" />-->
                                                    <Setter Property="IsExpanded" Value="True" />
                                                </Style>
                                                <HierarchicalDataTemplate DataType="{x:Type nsMsiObjects:Feature}" ItemsSource="{Binding SubFeatures}">
                                                    <CheckBox x:Name="checkBoxFeatureSelection" Content="{Binding DisplayName}" Checked="checkBoxFeatureSelection_Checked"/>
                                                </HierarchicalDataTemplate>
                                            </TreeView.Resources>
                                            <TreeView.Background>
                                                <LinearGradientBrush EndPoint="1.204,0.5" StartPoint="0.056,0.5">
                                                    <GradientStop Color="White" Offset="1"/>
                                                    <GradientStop Color="#FFD4D7DB"/>
                                                </LinearGradientBrush>
                                            </TreeView.Background>
                                        </TreeView>
                                    </Grid>
                                    <GridSplitter x:Name="gridSplitterFeaturesAndInstallProps" 
                                        ResizeDirection="Rows" 
                                        Grid.Column="1" 
                                        Grid.RowSpan="2" 
                                        Grid.Row="0" 
                                        Width="3" 
                                        Height="Auto" 
                                        HorizontalAlignment="Stretch" 
                                        VerticalAlignment="Stretch" 
                                        Margin="4,0">
                                        <GridSplitter.Background>
                                            <SolidColorBrush Color="{DynamicResource ColorBorder}"/>
                                        </GridSplitter.Background>
                                    </GridSplitter>
                                    <Grid x:Name="gridInstallPropsAndExpendAdditionals" Margin="0" Grid.Column="2">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition/>
                                            <ColumnDefinition Width="Auto"/>
                                        </Grid.ColumnDefinitions>
                                        <Grid x:Name="gridInstallProps" Grid.Column="0" Margin="0" Width="Auto">
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="2*"/>
                                                <RowDefinition Height="15*"/>
                                            </Grid.RowDefinitions>
                                            <Label x:Name="lableInstallationProperties" Content="Installation Properties" HorizontalAlignment="Left"
                                                   Margin="0" d:LayoutOverrides="GridBox" Grid.Row="0" Grid.Column="0" FontWeight="Bold" VerticalAlignment="Top"/>
                                            <ScrollViewer Grid.Row="1">
                                                <StackPanel x:Name="StackPanelInstallProps" Width="Auto" Height="Auto">
                                                    <!--<nsProject:ExpanderInstallProperties x:Name="expanderInstallProps" />-->

                                                </StackPanel>
                                            </ScrollViewer>

                                        </Grid>
                                    </Grid>
                                </Grid>
                                <GridSplitter x:Name="gridSplitterFeaturesAndComments" 
                                    ResizeDirection="Rows" 
                                    Grid.Column="0" 
                                    Grid.ColumnSpan="2" 
                                    Grid.Row="3" 
                                    Width="Auto" 
                                    Height="3" 
                                    HorizontalAlignment="Stretch" 
                                    VerticalAlignment="Stretch" 
                                    Margin="0">
                                    <GridSplitter.Background>
                                        <SolidColorBrush Color="{DynamicResource ColorBorder}"/>
                                    </GridSplitter.Background>
                                </GridSplitter>
                                <Grid x:Name="gridComments" HorizontalAlignment="Stretch" Margin="0" Width="Auto" Grid.Row="4">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="2*"/>
                                        <RowDefinition Height="10*"/>
                                    </Grid.RowDefinitions>
                                    <Label x:Name="lableComments" Content="Comments :" HorizontalAlignment="Left"
                                           VerticalAlignment="Top" Width="Auto" FontWeight="Bold" Grid.Row="0" Grid.RowSpan="1"
                                           Margin="5,0,33.866,-200" d:LayoutOverrides="Width"/>
                                    <TextBox x:Name="textBoxComments" Text="{Binding UserComments}" Margin="5" TextWrapping="Wrap"
                                             VerticalAlignment="Stretch" Height="Auto" VerticalScrollBarVisibility="Auto" Grid.Row="1"
                                             BorderThickness="3" AcceptsReturn="True" MaxWidth="{Binding ActualWidth, ElementName=gridComments}"/>
                                </Grid>
                            </Grid>
                        </Border>
                    </DataTemplate>
                </DataGrid.Resources>
                <DataGrid.RowBackground>
                    <LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
                        <GradientStop Color="#FFB7CADE"/>
                        <GradientStop Color="#FF5F6264" Offset="1"/>
                    </LinearGradientBrush>
                </DataGrid.RowBackground>
                <DataGrid.Columns>
                    <DataGridTextColumn x:Name="dgTextColumnServerIP" Binding="{Binding VMServerIP}" Header="Server IP" IsReadOnly="True"/>
                    <DataGridTextColumn x:Name="dgTextColumnVMName" Binding="{Binding Name}" Header="Virtual Machine Name" IsReadOnly="True"/>
                    <DataGridTextColumn x:Name="dgTextColumnInstalledVersion" Binding="{Binding InstalledVersion}" Header="Installed Version"/>
                    <DataGridTemplateColumn x:Name="dgTemplateColumnUpgradeMajorVersion" Header="Upgrade Major Version">
                        <DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <ComboBox x:Name="comboBoxMajorVersion" Height="Auto"
                                        SelectedIndex="0"
                                        ItemsSource="{Binding Path=InstallPackage.Versions,UpdateSourceTrigger=PropertyChanged}"
                                    />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellEditingTemplate>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox x:Name="comboBoxMajorVersion" Height="Auto"
                                          SelectedIndex="0"
                                    ItemsSource="{Binding Path=InstallPackage.Versions}"
                                    />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn x:Name="dgTemplateColumnUpgradeVersion" Header="Upgrade Version">
                        <DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <ComboBox x:Name="comboBoxMinorVersion"
                                  SelectedIndex="0"
                                  ItemsSource="{Binding Path=SelectedItem.MinorBuildVersions, ElementName=comboBoxMajorVersion, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                  DisplayMemberPath="Version"
                                  SelectedValue="Version"
                                  SelectedValuePath="Version" 
                                  Margin="15,0,0,0" VerticalAlignment="Center" IsEditable="True" IsReadOnly="True"
                                  ToolTip="Choose user to display his virtual machines" Grid.Column="3" Width="220" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellEditingTemplate>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox x:Name="comboBoxMinorVersion"
                                  SelectedIndex="0"
                                  ItemsSource="{Binding Path=SelectedItem.Users, ElementName=comboBoxTeamName, UpdateSourceTrigger=PropertyChanged}"
                                  DisplayMemberPath="Name"
                                  SelectedValuePath="Name" 
                                  Margin="15,0,0,0" VerticalAlignment="Center" IsEditable="True" IsReadOnly="True"
                                  ToolTip="Choose user to display his virtual machines" Grid.Column="3" Width="220" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn x:Name="dgTemplateColumnUpgradeTypr" Header="Upgrade Type">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox Height="Auto" 
                                    ItemsSource="{StaticResource UpgradeTypeList}"
                                    SelectedItem="{Binding UpgradeType}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn x:Name="dgTemplateColumnExecuteUpgrade" Header="Execute Upgrade">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button Style="{StaticResource GlassButton}" Content="Go!" Width="25" Height="25" Background="#FF2DD420"  Margin="2"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>

然而,当我突然扩展我的数据网格的行时,抛出NullReferenceException而没有源代码,因此我无法看到它从哪里被抛出。eveny函数是空的所以它仍然没有到达那里我还想到了每个事件导致同样的事情我认为由于某种原因它在我设置TreeView ItemsSource时创建复选框之前触发了事件

这是用于设置TreeView ItemsSource的附加代码:

/// <summary>
    /// This function occur when selecting one of the rows from dataGridVirtualMachinesInfo
    /// opening its row details and setting its inner controls
    /// </summary>
    private void dataGridVirtualMachinesInfo_RowDetailsVisibilityChanged(object sender, DataGridRowDetailsEventArgs e)
    {
        string path = "Some Path.msi";
        TreeView featuresTree = dataGridVirtualMachinesInfo.GetRowDetailsInnerControl<TreeView>("GroupViewFeaturesList");
        if (featuresTree.Items.IsEmpty)
        {
            // get the selected row of dataGridVirtualMachinesInfo
            DataGridRow dgRow = dataGridVirtualMachinesInfo.GetSelectedRow(dataGridVirtualMachinesInfo.SelectedItem);
            VMImageInfo selectedRowVmInfo = dgRow.DataContext as VMImageInfo;
            selectedRowVmInfo.InstallPackage = new InstallationPackage();
            selectedRowVmInfo.InstallPackage.SetFeatures(path);          DisplayFeaturesGroupedByFeatures(selectedRowVmInfo.InstallPackage.Features.DisplayedFeaturesList);
        }
    }

我该如何处理?我整天都在寻找答案,找不到与之相关的东西

谢谢!

更新 我已经按照你的要求添加了所有DataGrid及其详细信息 你也是对的我不需要在The Code中设置TreeView,所以我从那里删除它并更改了xaml中的绑定路径。 treeView是DataGridRowDetails模板的一部分 异常消息是: “你调用的对象是空的。” 由于源不可用,因此没有堆栈跟踪,可能发生在xaml

再次感谢!

0 个答案:

没有答案