Skip to main content

Microsoft Silverlight

Answered Question Can anyone explain this behavior?RSS Feed

(0)

Greg.Net
Greg.Net

Member

Member

34 points

47 Posts

Can anyone explain this behavior?

I'm relatively new to silverlight and I'm not sure why I'm getting this null reference exception

 UserControl A uses UserControl B in it's xaml

 UserControl A sets the itemssource of UserControl B's datagrid like so:

 

public ReportDistributionMain()
{
      InitializeComponent();
      UserControlB.Reports.ItemsSource = DistributionController.ReportDistributions;
}

 

And that works fine.  However then I made a UserControl C which is basically just a custom Border I made which I use to wrap UserControl B's datagrid to make it look pretty.

So the User Control B's xaml looks something like this:

<local:UserControlC Loaded="GlassWindow_Loaded" >
	<local:UserControlC.Child>
		<data:DataGrid  x:Name="ReportGrid" BorderThickness="0.5,0.5,0.5,0.5" SelectedIndex="-1" BorderBrush="{x:Null}" Style="{StaticResource DataGridStyle69}" AutoGenerateColumns="False" SelectionMode="Single" GridLinesVisibility="None" Margin="0,0,1,1">
		<data:DataGrid.Columns>
		<data:DataGridTemplateColumn Header="Reports"  Width="275" CellTemplate="{StaticResource DataTemplate1}" CanUserSort="True" HeaderStyle="{StaticResource DataGridColumnHeaderStyle1}" IsReadOnly="True" CanUserReorder="True" CellStyle="{StaticResource DataGridCellStyle1}">
		</data:DataGridTemplateColumn>
		</data:DataGrid.Columns>
		</data:DataGrid>
	</local:UserControlC.Child>
</local:UserControlC>

 

However when I wrap UserControlB's xaml inside of UserControlC like this, I get a null reference exception in UserControlA when it tries to set UserControlB's datagrid's itemsSource.

 

I know this sounds confusing I'm explaining it poorly.  Hopefully someone can help because I'm not sure why the datagrid would be null. 

 

vincracker
vincracker

Contributor

Contributor

3116 points

522 Posts

Answered Question

Re: Can anyone explain this behavior?

Hi,

You are accessing control inside another control.(Grid Inside Control C). And probably Child is a property which sets the Content of Control C(or any container control inside C). So when you want to access the Grid you got the exception. Try to do like this:

1. Add a name to UserControlC(like <local:UserControlC x:Name="ControlC" ...../>

2. To accessing the DataGrid, (Suppose your Control B has name "ControlB" on UserControl A.

    DataGrid dg = (DataGrid)ControlB.ControlC.Child;

and now you have the DataGrid's reference, do whatever you want to do.

(If I misunderstood you or still having problem then let me know otherwise mark reply as answer.) 

-Vinit
 

     

Greg.Net
Greg.Net

Member

Member

34 points

47 Posts

Re: Can anyone explain this behavior?

yes that makes sense.  hmmm I think I'm going about this the wrong way then.  For example if I used a regular Border control in my xaml I would still be able to reference my Datagrid the normal way right?  So is there any other way to make a usercontrol that wraps another control but doesn't "steal" the reference from the control being wrapped? 

 This is my code for UserControlC.  My custom border class. 

 

<UserControl
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
	mc:Ignorable="d"
	x:Class="EPRDistributionCenter.GlassWindow"
	d:DesignWidth="640" d:DesignHeight="480">

	<Grid x:Name="LayoutRoot">
			<Rectangle Stroke="#90FFFFFF" RadiusX="10" RadiusY="10" HorizontalAlignment="Stretch" Opacity="1" Visibility="Visible" Margin="9,6,7,28">
			<Rectangle.Fill>
				<SolidColorBrush Color="{StaticResource WindowDefault}"/>
			</Rectangle.Fill>
			<Rectangle.Effect>
				<DropShadowEffect BlurRadius="5"/>
			</Rectangle.Effect>
		</Rectangle>
		<Border x:Name="ContentWindow"  Margin="18,16,16,39" CornerRadius="4,4,4,4" BorderBrush="#FFFFFFFF" Background="#31000000" BorderThickness="1,1,1,1">
		</Border>
	</Grid>
</UserControl>
 

and in my codebehind I define a propery child which gets or sets ContentWindow's child element

 

        public UIElement Child
        {
            get { return ContentWindow.Child; }
            set { ContentWindow.Child = value; }
        }
 

 

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities