C# Datagrid combobox selectionchanged事件参数在两台pc之间不同';s

C# Datagrid combobox selectionchanged事件参数在两台pc之间不同';s,c#,wpf,combobox,datagrid,C#,Wpf,Combobox,Datagrid,代码: 在这种情况下,我看到sender参数包含第一个组合框的静态列表。但是在这个不起作用的例子中,发送者似乎是一个空的组合框。这导致“row”变量被设置为null,当我在if语句中检查该变量的项时,我得到一个System.NullReferenceException 我尝试的是: 首先,我确保安装了正确的.Net版本。这没有效果 我尝试了一个修补程序,在将发送者的itemsource强制转换为正确的数据类型后,尝试将其设置为热修复程序,但这也会引发NullReferenceException

代码:

在这种情况下,我看到sender参数包含第一个组合框的静态列表。但是在这个不起作用的例子中,发送者似乎是一个空的组合框。这导致“row”变量被设置为null,当我在if语句中检查该变量的项时,我得到一个
System.NullReferenceException

我尝试的是:

  • 首先,我确保安装了正确的.Net版本。这没有效果
  • 我尝试了一个修补程序,在将发送者的itemsource强制转换为正确的数据类型后,尝试将其设置为热修复程序,但这也会引发
    NullReferenceException
  • 两台计算机上运行的windows版本相同

我想我在NullReferenceException和添加
DataGridRow row=newDataGridRow()时遇到了类似的问题行之前,code>解决了此问题。

在尝试执行任何操作之前,请检查
组合框是否已加载。您还应该避免强制转换为除
框架元素之外的任何其他元素:

DataGridRow row = (DataGridRow)dataGrid.ContainerFromElement((DependencyObject)comboBox);
xyEntry newValues = null;

if (row.Item != null){
    newValues = (xyEntry)row.Item;
}
else {
    return;
}

谢谢你的建议,但这并没有解决我的问题。它仍然给了我同样的错误。@TJasinski:同样的错误?您从何处获得
NullReferenceException
?如果在调试期间检查时(row.items!=null),当我尝试分配转换的发送方时,行变为null。因此,当我检查它的项目时,会发生错误@mm8@TJasinski:dataGrid.ContainerRomeElement(comboBox)
返回什么?我发现了问题。datacontext未在不工作的计算机上正确设置。decl之前的手动设置解决了转换问题。你知道为什么这会发生在一台电脑上而不是另一台电脑上吗?或者我应该为此提出一个新问题?无论如何,谢谢你的支持
<DataGrid SelectionUnit="FullRow" x:Name="dataGrid" Margin="10,110,162,15" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" AutoGenerateColumns="False" CanUserAddRows="False" CanUserReorderColumns="False">
    <DataGrid.Columns>

        <DataGridTextColumn Header="X Coord" Binding="{Binding xCoord}" IsReadOnly="True" Width="*" />
        <DataGridTextColumn Header="Y Coord" Binding="{Binding yCoord}" IsReadOnly="True" Width="*"/>
        <DataGridTextColumn Header="Rotation" Binding="{Binding Rotation}" IsReadOnly="True" Width="*"/>
        <DataGridTextColumn Header="RefDes" Binding="{Binding RefDes}" IsReadOnly="True" Width="*"/>
        <DataGridTextColumn Header="P/N" Binding="{Binding Number}" IsReadOnly="True" Width="*"/>
        <DataGridTextColumn Header="Package" Binding="{Binding Package}" IsReadOnly="True" Width="*"/>
        <DataGridTextColumn Header="FactorX %" Binding="{Binding FactorX}" IsReadOnly="False" Width="*"/>
        <DataGridTextColumn Header="FactorY %" Binding="{Binding FactorY}" IsReadOnly="False" Width="*"/>
        <DataGridCheckBoxColumn Header="Use factors" Binding="{Binding UseFactors}" IsReadOnly="False" Width="*"/>
            <DataGridTemplateColumn Header="Category" Width="*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid>
                            <ComboBox SelectedValue="{Binding Category, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding CategoryChoices}" SelectionChanged="updateSeconderyCombobox" >
                                <ComboBox.Style>
                                    <Style TargetType="ComboBox">
                                        <Setter Property="Visibility" Value="Hidden"/>
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridRow}, Path=IsSelected}"
                                             Value="True">
                                                    <Setter Property="Visibility" Value="Visible" />
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </ComboBox.Style>
                                </ComboBox>
                                <TextBlock Text="{Binding Category}" >
                                    <TextBlock.Style>
                                        <Style TargetType="TextBlock">
                                            <Setter Property="Visibility" Value="Visible"/>
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridRow}, Path=IsSelected}"
                                             Value="True">
                                                    <Setter Property="Visibility" Value="Hidden" />
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </TextBlock.Style>
                                </TextBlock>
                            </Grid>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Size" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Grid>
                                <ComboBox SelectedValue="{Binding Size, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding SizeChoices}">
                                    <ComboBox.Style>
                                        <Style TargetType="ComboBox">
                                            <Setter Property="Visibility" Value="Hidden"/>
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridRow}, Path=IsSelected}"
                                             Value="True">
                                                    <Setter Property="Visibility" Value="Visible" />
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </ComboBox.Style>
                                </ComboBox>
                                <TextBlock Text="{Binding Size}">
                                    <TextBlock.Style>
                                        <Style TargetType="TextBlock">
                                            <Setter Property="Visibility" Value="Visible"/>
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridRow}, Path=IsSelected}"
                                             Value="True">
                                           <Setter Property="Visibility" Value="Hidden" />
                                       </DataTrigger>
                                   </Style.Triggers>
                               </Style>
                           </TextBlock.Style>
                       </TextBlock>
                   </Grid>
               </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>
DataGridRow row = (DataGridRow)dataGrid.ContainerFromElement((DependencyObject)comboBox);
xyEntry newValues = null;

if (row.Item != null){
    newValues = (xyEntry)row.Item;
}
else {
    return;
}
private void updateSeconderyCombobox(object sender, SelectionChangedEventArgs e)
{
    FrameworkElement comboBox = sender as FrameworkElement;
    if (comboBox == null || !comboBox.IsLoaded)
        return;

    DataGridRow row = (DataGridRow)dataGrid.ContainerFromElement(comboBox);
    ...
}