0

I am developing an application in WPF and from some reason Visual studio display in error window this exception: Object reference not set to an instance of an object

When I was building application this errors are still visible, but I application works correct

this is a my XAML:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <StackPanel Grid.Row="0" Grid.Column="0" Margin="5"
                HorizontalAlignment="Stretch" VerticalAlignment="Top" Orientation="Vertical">
        <CheckBox Name="cbPersonType" Content="Person Type" IsChecked="{Binding Path=IsPersonTypeVisible, Mode=TwoWay}" />
    </StackPanel>

    <StackPanel Grid.Row="0" Grid.Column="1" Margin="5"
                HorizontalAlignment="Stretch" VerticalAlignment="Top" Orientation="Vertical">
        <DataGrid Name="dgPersons">
            <DataGrid.Columns>
                <DataGridTextColumn Width="*" Header="Person Type"
                                    Visibility="{Binding Path=IsChecked, Source={x:Reference Name=cbPersonType}, Converter={StaticResource BooleanToVisibilityConverter}}"
                                        />
            </DataGrid.Columns>
        </DataGrid>
    </StackPanel>
</Grid>

this exception is on DataGridTextColumn. Does some one know why and what is this error?

2
  • 1
    The XAML editor is buggy. Sometimes it shows error, underlines correct lines of code for no reason. You can try exiting VS and opening it again. I accept to live with that, it's a bit annoying but at least much better than Notepad or even Notepad++ Commented Sep 29, 2015 at 16:23
  • I tried off and on again studio, but without result. But I'm sure you're right, it's much better than to write something exotic like Notepad Commented Sep 29, 2015 at 16:32

2 Answers 2

2

This is just a theory from my part trying to explain the errormessage.

Right now I'm struggling with a similar case. However, I have my checkbox in a contextmenu displaying the columns in a datagrid. The user can check the columns he/she wants to see in the datagrid.

Problem is the checkboxes seem not to be initlized until the user actually right-click to open the contextmenu. Until then the datagrid just shows empty rows with no columns at all.

It seems the x:Reference does not kick in until the checkboxes are loaded in the contextmenu.

A simplified XAML of the contextmenu and datagrid is below.

<DataGrid ItemsSource="{Binding OrderCollection}" AutoGenerateColumns="False"  IsReadOnly="True" >
    <DataGrid.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Columnsetup">
                <CheckBox x:Name="chkHeadOrderNo" Content="Ordernumber" IsChecked="{Binding columnVisibilityHeadOrderNo}"/>
            </MenuItem>
        </ContextMenu>
    </DataGrid.ContextMenu>
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding OrderNo}" Header="Ordernumber" 
                            Visibility="{Binding Source={x:Reference chkHeadOrderNo}, Path=IsChecked, Converter={StaticResource convertVisibility}}"/>
    </DataGrid.Columns>
</DataGrid> 
Sign up to request clarification or add additional context in comments.

Comments

-1

I would suggest to replace Source={x:Reference Name=cbPersonType} with ElementName=cbPersonType. This works fine both in design and runtime modes

1 Comment

This doesn't work if your binding source is outside the scope of your datagrid. Using x:Reference is required in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.