So I'm trying to make an application with 2 user controls side by side, that slide left and right when you switch between them. The problem is, that one of my user controls (AddReferencePage), when added to the MainWindow, causes the opening tag on my MainWindow to become underlined with a NullReferenceException Error. The designer also shows an error:
NullReferenceException: Object reference not set to an instance of an object.
StackTrace:
at ARC.AddReferencePage.ReferenceDetails()
at ARC.AddReferencePage.AddReferencePage_Loaded(Object sender, RoutedEventArgs e)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
at System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent)
at System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(Object root)
at MS.Internal.LoadedOrUnloadedOperation.DoWork()
at System.Windows.Media.MediaContext.FireLoadedPendingCallbacks()
at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler
The code to display my user controls is:
<DockPanel x:Name="SlideGrid" VerticalAlignment="Stretch" Width="{Binding ActualWidth, ElementName=MainGrid, Converter={StaticResource DoubleConverter}}" Margin="0,28,0,0" ClipToBounds="True">
<ItemsControl DockPanel.Dock="Left">
<local:MainPage/>
</ItemsControl>
<ItemsControl DockPanel.Dock="Right">
<local:AddReferencePage/>
</ItemsControl>
</DockPanel>
The MainPage works fine, no errors what so ever. Its the AddReferencePage that causes the errors. I have checked through each of the user controls respective code behinds, and they are for all intents and purposes identical.
Code behind for MainPage:
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.Collections.Specialized
Imports System.Windows.Threading
Imports System.Windows.Media.Animation
Imports System.Globalization
Public Class MainPage
Private StartPoint As Point
Private Sub MainPage_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
'Dim PW As MainWindow = Window.GetWindow(MainPage)
'If PW IsNot Nothing Then
' ReferenceList.ItemsSource = PW.ReferenceCollection
' Dim Alphabetical As SortDescription = New SortDescription("PropLastName", ListSortDirection.Ascending)
' ReferenceList.Items.SortDescriptions.Add(Alphabetical)
'End If
RefreshReferences()
End Sub
Public Sub RefreshReferences()
Dim PW As MainWindow = Window.GetWindow(MainPage)
If PW IsNot Nothing Then
ReferenceList.ItemsSource = PW.ReferenceCollection
Dim Alphabetical As SortDescription = New SortDescription("PropLastName", ListSortDirection.Ascending)
ReferenceList.Items.SortDescriptions.Add(Alphabetical)
End If
End Sub
Private Sub ListBoxSearch_TextChanged(sender As Object, e As TextChangedEventArgs) Handles ListBoxSearch.TextChanged
If ListBoxSearch.Text <> "Search..." Then
Dim PW As MainWindow = Window.GetWindow(MainPage)
If PW IsNot Nothing Then
PW.view = CollectionViewSource.GetDefaultView(PW.ReferenceCollection)
PW.view.Filter = New Predicate(Of Object)(AddressOf PW.FilterList)
End If
End If
End Sub
Private Sub ListBoxSearch_MouseLeftButtonUp(sender As Object, e As MouseButtonEventArgs) Handles ListBoxSearch.PreviewMouseDown
If ListBoxSearch.Text = "Search..." Then
ListBoxSearch.FontStyle = FontStyles.Normal
ListBoxSearch.Clear()
End If
End Sub
Private Sub ListBoxSearch_LostFocus(sender As Object, e As RoutedEventArgs) Handles ListBoxSearch.LostFocus
If ListBoxSearch.Text = "" Then
ListBoxSearch.FontStyle = FontStyles.Italic
ListBoxSearch.Text = "Search..."
End If
End Sub
Private Sub ListViewClick(sender As Object, e As RoutedEventArgs) Handles ButtonListView.Click
If ReferenceList.ItemTemplate Is FindResource("GridViewTemplate") Then
ViewBackground.BeginStoryboard(DirectCast(Me.Resources("ViewRightToLeft"), Storyboard))
ReferenceList.ItemTemplate = DirectCast(Me.FindResource("ListViewTemplate"), DataTemplate)
ButtonListView.Content = FindResource("NoListView")
ButtonGridView.Content = FindResource("NoGridView")
End If
End Sub
Private Sub GridViewClick(sender As Object, e As RoutedEventArgs) Handles ButtonGridView.Click
If ReferenceList.ItemTemplate Is FindResource("ListViewTemplate") Then
ViewBackground.BeginStoryboard(DirectCast(Me.Resources("ViewLeftToRight"), Storyboard))
ReferenceList.ItemTemplate = DirectCast(Me.FindResource("GridViewTemplate"), DataTemplate)
ButtonListView.Content = FindResource("NoListView")
ButtonGridView.Content = FindResource("NoGridView")
End If
End Sub
Private Sub ListViewEnter(sender As Object, e As RoutedEventArgs) Handles ButtonListView.MouseEnter
End Sub
Private Sub GridViewEnter(sender As Object, e As RoutedEventArgs) Handles ButtonGridView.MouseEnter
End Sub
Private Sub ListViewLeave(sender As Object, e As RoutedEventArgs) Handles ButtonListView.MouseLeave
End Sub
Private Sub GridViewLeave(sender As Object, e As RoutedEventArgs) Handles ButtonGridView.MouseLeave
End Sub
Private Sub EditListBoxItem(sender As Object, e As RoutedEventArgs)
Dim PW As MainWindow = Window.GetWindow(MainPage)
If PW IsNot Nothing Then
PW.ChangeSlide(sender, 1)
PW.EditItem = ReferenceList.SelectedItem
PW.NewItem = False
End If
End Sub
Private Sub DeleteListBoxItem(sender As Object, e As RoutedEventArgs)
Dim PW As MainWindow = Window.GetWindow(MainPage)
If PW IsNot Nothing Then
PW.ReferenceCollection.Remove(ReferenceList.SelectedItem)
End If
End Sub
Private Sub NewListBoxItem(sender As Object, e As RoutedEventArgs)
Dim PW As MainWindow = Window.GetWindow(MainPage)
If PW IsNot Nothing Then
PW.ChangeSlide(sender, 1)
PW.NewItem = True
End If
End Sub
Private Sub ReferenceList_Loaded(sender As Object, e As RoutedEventArgs) Handles ReferenceList.Loaded
RefreshReferences()
End Sub
Private Sub ReferenceList_PreviewMouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs) Handles ReferenceList.PreviewMouseLeftButtonDown
Dim PW As MainWindow = Window.GetWindow(MainPage)
StartPoint = e.GetPosition(Nothing)
PW.Resizing = False
End Sub
Private Sub ReferenceList_PreviewMouseMove(sender As Object, e As MouseEventArgs) Handles ReferenceList.PreviewMouseMove
Dim PW As MainWindow = Window.GetWindow(MainPage)
If PW.Resizing = False Then
Dim MousePosition As Point = e.GetPosition(Nothing)
Dim Difference As Vector = StartPoint - MousePosition
Dim StopDrop As Boolean
If e.LeftButton = MouseButtonState.Pressed AndAlso (Math.Abs(Difference.X) > SystemParameters.MinimumHorizontalDragDistance Or Math.Abs(Difference.Y) > SystemParameters.MinimumVerticalDragDistance) Then
Dim LB As ListBox = sender
Dim UIE As UIElement = LB.InputHitTest(MousePosition)
If UIE IsNot Nothing Then
Dim Data As Object = DependencyProperty.UnsetValue
While Data Is DependencyProperty.UnsetValue And UIE IsNot Nothing
Data = LB.ItemContainerGenerator.ItemFromContainer(UIE)
If Data Is DependencyProperty.UnsetValue Then
UIE = VisualTreeHelper.GetParent(UIE)
End If
If UIE Is LB Then
StopDrop = True
End If
End While
If Data IsNot DependencyProperty.UnsetValue Then
StopDrop = False
End If
Else
StopDrop = True
End If
PW.TempItem = LB.SelectedItem
Dim FN As String = PW.TempItem.PropLastName & ", " & PW.TempItem.PropFirstName.Substring(0, 1)
Dim TT As String = PW.TempItem.PropTitle
Dim YR As String = PW.TempItem.PropYear.ToString
Dim ReferenceText As String = FN & " " & YR & ", " & TT
Dim DragData As DataObject = New DataObject(DataFormats.StringFormat, ReferenceText)
If DragData IsNot Nothing And StopDrop = False Then
DragDrop.DoDragDrop(sender, DragData, DragDropEffects.Copy)
End If
End If
End If
End Sub
End Class
XAML for MainPage:
<UserControl x:Class="MainPage" x:Name="MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ARC"
mc:Ignorable="d"
VerticalAlignment="Stretch" HorizontalAlignment="Stretch" d:DesignWidth="400" d:DesignHeight="600" SnapsToDevicePixels="True">
<UserControl.Resources>
<local:ImageConverter x:Key="ImageConverter"/>
<Image x:Key="BrownBook" Source="Images\Icons\BrownBook.png" Height="64" Width="64"/>
<Storyboard x:Key="ViewRightToLeft"
AccelerationRatio=".5"
DecelerationRatio=".5">
<ThicknessAnimation Storyboard.TargetName="ViewBackground" Storyboard.TargetProperty="Margin" Duration="0:0:0.15" To="0,0,28,0"/>
</Storyboard>
<Storyboard x:Key="ViewLeftToRight"
AccelerationRatio=".5"
DecelerationRatio=".5">
<ThicknessAnimation Storyboard.TargetName="ViewBackground" Storyboard.TargetProperty="Margin" Duration="0:0:0.15" To="0,0,-28,0"/>
</Storyboard>
</UserControl.Resources>
<Grid Background="#FFF1F1F1">
<Grid>
<DockPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<DockPanel Margin="0,0,0,0" VerticalAlignment="Top" HorizontalAlignment="Stretch" Height="28" DockPanel.Dock="Top" Background="#E5E5E5">
<Button BorderThickness="0" x:Name="AddNewReference" Style="{DynamicResource NoChromeButton}" DockPanel.Dock="Right" HorizontalAlignment="Right" Click="ListViewClick" Height="28" Width="28">
<DynamicResource ResourceKey="SearchBar"/>
</Button>
<Button BorderThickness="0" x:Name="Settings" Style="{DynamicResource NoChromeButton}" DockPanel.Dock="Right" HorizontalAlignment="Right" Click="ListViewClick" Height="28" Width="28">
<DynamicResource ResourceKey="SearchBar"/>
</Button>
<Grid Margin="0,0,0,0" DockPanel.Dock="Right" Width="56" Height="28" VerticalAlignment="Top" HorizontalAlignment="Stretch">
<Button Height="28" Width="28" Margin="0,0,28,0" BorderThickness="0" Style="{DynamicResource NoChromeButton}" x:Name="ViewBackground" Background="#3E3B3B"/>
<Button Margin="0,0,0,0" BorderThickness="0" x:Name="ButtonGridView" Style="{DynamicResource NoChromeButton}" HorizontalAlignment="Right" Click="GridViewClick" Height="28" Width="28">
<DynamicResource ResourceKey="NoGridView"/>
</Button>
<Button Margin="0,0,28,0" BorderThickness="0" x:Name="ButtonListView" Style="{DynamicResource NoChromeButton}" HorizontalAlignment="Right" Click="ListViewClick" Height="28" Width="28">
<DynamicResource ResourceKey="NoListView"/>
</Button>
</Grid>
<Button BorderThickness="0" x:Name="SearchBarIcon" Style="{DynamicResource NoChromeButton}" DockPanel.Dock="Left" HorizontalAlignment="Right" Click="ListViewClick" Height="28" Width="23">
<DynamicResource ResourceKey="SearchBar"/>
</Button>
<TextBox x:Name="ListBoxSearch" Style="{DynamicResource SearchBoxTemplate}" Text="Search..." FontSize="16" HorizontalAlignment="Stretch" FontStyle="Italic" BorderThickness="0" Foreground="Gray" Background="Transparent" VerticalContentAlignment="Top"/>
</DockPanel>
<ListBox AlternationCount="2" DockPanel.Dock="Top" Margin="0,0,0,0" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding ReferenceCollection, ElementName=local:MainWindow}" TextSearch.TextPath="SearchString" x:Name="ReferenceList" HorizontalContentAlignment="Stretch" ScrollViewer.CanContentScroll="False" SnapsToDevicePixels="True" AllowDrop="True" BorderThickness="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ListBox.Background>
<LinearGradientBrush StartPoint="1,1" EndPoint="1,0">
<GradientStop Offset="0.0" Color="#190000FF"/>
<GradientStop Color="#000000FF" Offset="1.0"/>
</LinearGradientBrush>
</ListBox.Background>
<ListBox.ItemTemplate>
<StaticResource ResourceKey="ListViewTemplate"/>
</ListBox.ItemTemplate>
<ListBox.ContextMenu>
<ContextMenu Style="{StaticResource ListBoxContextMenu}">
<MenuItem x:Name="EditContextMenuItem" Header="Edit" Click="EditListBoxItem" Style="{StaticResource ListBoxContextMenuItem}"/>
<MenuItem x:Name="DeleteContextMenuItem" Header="Delete" Click="DeleteListBoxItem" Style="{StaticResource ListBoxContextMenuItem}"/>
<MenuItem x:Name="NewContextMenuItem" Header="New" Click="NewListBoxItem" Style="{StaticResource ListBoxContextMenuItem}"/>
</ContextMenu>
</ListBox.ContextMenu>
</ListBox>
</DockPanel>
</Grid>
</Grid>
</UserControl>
Code behind for AddReferencePage:
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.Collections.Specialized
Imports System.Windows.Threading
Imports System.Windows.Media.Animation
Imports System.Globalization
Public Class AddReferencePage
Private SelectImage As Boolean = False
Private Sub ReturnToReferences(sender As Object, e As RoutedEventArgs)
Dim PW As MainWindow = Window.GetWindow(AddReferencePage)
If PW IsNot Nothing Then
If ReferenceAuthor.Text <> "Author" Or ReferenceDate.Text <> "Date" Or ReferenceTitle.Text <> "Title" Then
Dim CN As String() = Nothing
Dim FN As String = Nothing
Dim LN As String = Nothing
Dim YR As String = Nothing
Dim TT As String = Nothing
If ReferenceAuthor.Text = "Author" Then
ReferenceAuthor.Text = "Unknown"
FN = Nothing
LN = "Unknown"
Else
If ReferenceAuthor.Text.Contains(" ") Then
CN = ReferenceAuthor.Text.Split(" ")
FN = CN.First
LN = CN.Last
Else
FN = Nothing
LN = ReferenceAuthor.Text
End If
End If
If ReferenceDate.Text = "Date" Then
ReferenceDate.Text = "Unknown"
YR = "Unknown"
Else
YR = ReferenceDate.Text
End If
If ReferenceTitle.Text = "Title" Then
ReferenceTitle.Text = "Unknown"
TT = "Unknown"
Else
TT = ReferenceTitle.Text
End If
If PW.NewItem = False Then
PW.ReferenceCollection.Remove(PW.EditItem)
PW.AddReferenceItem(FN, LN, TT, YR, "Book")
Else
PW.AddReferenceItem(FN, LN, TT, YR, "Book")
End If
End If
End If
PW.ChangeSlide(sender, 0)
End Sub
Private Sub ButtonsMouseEnter(sender As Button, e As RoutedEventArgs)
Select Case sender.Name
Case "ReturnReferences"
sender.Content = FindResource("BackArrowHover")
End Select
End Sub
Private Sub ButtonsMouseLeave(sender As Button, e As RoutedEventArgs)
Select Case sender.Name
Case "ReturnReferences"
sender.Content = FindResource("BackArrowNormal")
End Select
End Sub
Private Sub ButtonsMouseDown(sender As Button, e As RoutedEventArgs)
Select Case sender.Name
Case "ReturnReferences"
sender.Content = FindResource("BackArrowClick")
End Select
End Sub
Public Sub SaveReferenceSub(sender As Object, e As RoutedEventArgs)
Dim PW As MainWindow = Window.GetWindow(AddReferencePage)
Dim CN As String() = ReferenceAuthor.Text.Split(" ")
Dim FN As String = CN.First()
Dim LN As String = CN.Last()
Dim YR As Integer = CInt(ReferenceDate.Text)
Dim TT As String = ReferenceTitle.Text
If PW.NewItem = False Then
PW.ReferenceCollection.Remove(PW.EditItem)
PW.AddReferenceItem(FN, LN, TT, YR, "Book")
Else
PW.AddReferenceItem(FN, LN, TT, YR, "Book")
End If
End Sub
Private Sub ReferenceDetails()
Dim PW As MainWindow = Window.GetWindow(AddReferencePage)
If PW.NewItem = False Then
ReferenceAuthor.Text = PW.EditItem.PropFirstName & " " & PW.EditItem.PropLastName
ReferenceTitle.Text = PW.EditItem.PropTitle
ReferenceDate.Text = PW.EditItem.PropYear.ToString
End If
End Sub
Private Sub AddReferencePage_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
ReferenceDetails()
End Sub
Private Sub FieldEnter(sender As TextBox, e As RoutedEventArgs)
Select Case sender.Name
Case "ReferenceAuthor"
If sender.Text = "Author" Then
sender.Clear()
End If
Case "ReferenceTitle"
If sender.Text = "Title" Then
sender.Clear()
End If
Case "ReferenceDate"
If sender.Text = "Date" Then
sender.Clear()
End If
End Select
End Sub
Private Sub FieldLeave(sender As TextBox, e As RoutedEventArgs)
If sender.Text = "" Then
Select Case sender.Name
Case "ReferenceAuthor"
sender.Text = "Author"
Case "ReferenceTitle"
sender.Text = "Title"
Case "ReferenceDate"
sender.Text = "Date"
End Select
End If
End Sub
Private Sub FieldChanged(sender As TextBox, e As RoutedEventArgs)
If sender.Text <> "Author" And sender.Text <> "Date" And sender.Text <> "Title" Then
sender.Foreground = Brushes.Black
Else
sender.Foreground = Brushes.Gray
End If
End Sub
Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
If SelectImage = False Then
SelectImageBox.BeginStoryboard(DirectCast(Me.Resources("LeftToRight"), Storyboard))
SelectImage = True
Else
SelectImageBox.BeginStoryboard(DirectCast(Me.Resources("RightToLeft"), Storyboard))
SelectImage = False
End If
End Sub
Private Sub SelectImageBox_LostFocus(sender As Object, e As RoutedEventArgs) Handles SelectImageBox.LostFocus
'If SelectImage = True Then
' SelectImageBox.BeginStoryboard(DirectCast(Me.Resources("RightToLeft"), Storyboard))
' SelectImage = False
'End If
End Sub
End Class
XAML for AddReferencePage:
<UserControl x:Class="AddReferencePage" x:Name="AddReferencePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ARC"
mc:Ignorable="d"
VerticalAlignment="Stretch" SnapsToDevicePixels="True" HorizontalAlignment="Stretch" d:DesignWidth="400" d:DesignHeight="600">
<UserControl.Resources>
<Storyboard x:Key="RightToLeft"
AccelerationRatio=".5"
DecelerationRatio=".5">
<DoubleAnimation Storyboard.TargetName="ReferenceInfo" Storyboard.TargetProperty="(Canvas.Left)" Duration="0:0:0.3" To="0"/>
<DoubleAnimation Storyboard.TargetName="SelectImageBox" Storyboard.TargetProperty="Opacity" Duration="0:0:0.3" From="1.0" To="0.0"/>
</Storyboard>
<Storyboard x:Key="LeftToRight"
AccelerationRatio=".5"
DecelerationRatio=".5">
<DoubleAnimation Storyboard.TargetName="ReferenceInfo" Storyboard.TargetProperty="(Canvas.Left)" Duration="0:0:0.3" To="{Binding ActualWidth, ElementName=AnimInfo}"/>
<DoubleAnimation Storyboard.TargetName="SelectImageBox" Storyboard.TargetProperty="Opacity" Duration="0:0:0.3" From="0.0" To="1.0"/>
</Storyboard>
</UserControl.Resources>
<Grid Background="#FFF1F1F1">
<Button Style="{DynamicResource NoChromeButton}" x:Name="ReturnReferences" Click="ReturnToReferences" MouseDown="ButtonsMouseDown" MouseEnter="ButtonsMouseEnter" MouseLeave="ButtonsMouseLeave" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="5,5,0,0" Height="48" Width="48">
<DynamicResource ResourceKey="BackArrowNormal"/>
</Button>
<TextBlock Text="Edit References" FontFamily="Segoe UI Light" FontSize="36" Margin="72,3,0,0" VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
<TextBlock x:Name="AnimInfo" FontFamily="Segoe UI Light" FontSize="36" Margin="72,3,60,0" VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
<ListView x:Name="SelectImageBox" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="72,59,60,20" Background="Transparent" ScrollViewer.HorizontalScrollBarVisibility="Disabled" BorderThickness="0" Opacity="0">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" IsItemsHost="True"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<Button Style="{DynamicResource NoChromeButton}" Height="48" Width="48">
<DynamicResource ResourceKey="Book"/>
</Button>
<Button Style="{DynamicResource NoChromeButton}" Height="48" Width="48">
<DynamicResource ResourceKey="Book"/>
</Button>
<Button Style="{DynamicResource NoChromeButton}" Height="48" Width="48">
<DynamicResource ResourceKey="Book"/>
</Button>
<Button Style="{DynamicResource NoChromeButton}" Height="48" Width="48">
<DynamicResource ResourceKey="Book"/>
</Button>
<Button Style="{DynamicResource NoChromeButton}" Height="48" Width="48">
<DynamicResource ResourceKey="Book"/>
</Button>
<Button Style="{DynamicResource NoChromeButton}" Height="48" Width="48">
<DynamicResource ResourceKey="Book"/>
</Button>
</ListView>
<Canvas x:Name="InfoCanvas" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="72,60,0,0" Opacity="1">
<DockPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="0,0,0,0" x:Name="ReferenceInfo" Canvas.Left="0" Canvas.Top="0" Width="{Binding ActualWidth, ElementName=InfoCanvas}" Height="{Binding ActualHeight, ElementName=InfoCanvas}">
<TextBox x:Name="ReferenceAuthor" GotFocus="FieldEnter" TextChanged="FieldChanged" LostFocus="FieldLeave" FontSize="16" FontFamily="Segoe UI Light" Text="Author" Foreground="Gray" Background="Transparent" DockPanel.Dock="Top" VerticalAlignment="Top" HorizontalAlignment="Stretch" BorderThickness="0" Margin="0,2,0,2"/>
<TextBox x:Name="ReferenceTitle" GotFocus="FieldEnter" TextChanged="FieldChanged" LostFocus="FieldLeave" FontSize="16" FontFamily="Segoe UI Light" Text="Title" Foreground="Gray" Background="Transparent" DockPanel.Dock="Top" VerticalAlignment="Top" HorizontalAlignment="Stretch" BorderThickness="0" Margin="0,2,0,2"/>
<TextBox x:Name="ReferenceDate" GotFocus="FieldEnter" TextChanged="FieldChanged" LostFocus="FieldLeave" FontSize="16" FontFamily="Segoe UI Light" Text="Date" Foreground="Gray" Background="Transparent" DockPanel.Dock="Top" VerticalAlignment="Top" HorizontalAlignment="Stretch" BorderThickness="0" Margin="0,2,0,2"/>
</DockPanel>
</Canvas>
<Button Style="{DynamicResource NoChromeButton}" Margin="5,60,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="48" Height="48" VerticalContentAlignment="Center" Click="Button_Click_1">
<DynamicResource ResourceKey="Book"/>
</Button>
</Grid>
</UserControl>
I know its a lot of code :D but I really need to fix this problem! Thanks all! Let me know if you need more details. All my resources and templates are defined in Application.xaml which I have not included, due to the irrelevance. I can include if needed.
According to the error stack trace, the error occurs in the sub ReferenceDetails()
, which is in your second to last code block in the question. I think the error exists in these lines of code,
Dim PW As MainWindow = Window.GetWindow(AddReferencePage)
If PW.NewItem = False Then
I haven't worked with WPF, only Winforms. But if the designers are similar, its possible that MainWindow
could be Nothing
at design time. You could try something like this and see if it fixes the error.
Dim PW As MainWindow = Window.GetWindow(AddReferencePage)
If PW IsNot Nothing AndAlso PW.NewItem = False Then
That way if PW comes back as Nothing
it won't cause a NullReferenceException
. Also (again I assume WPF designers work similar to Winfors), be sure to rebuild after changing the code, before you try adding it to the main window again.