How to get the parent page inside a frame? - c#

I got a page, in which there's a frame (that contains 2 input forms, that can be switched to with 2 buttons above the form) and a listView which contains the inputs from the user. When the User finishes the input, I want to pass an Object from the frame to the parent page.
I tried getting the Parent with the Parent property, but the parent property is for some reason null. I also tried to search for a solution on the web, but I wasn't able to find anything helpful. Does anyone of you know my problem can be solved?
Parent Page:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="20"/>
</Grid.ColumnDefinitions>
<Border Grid.Row="1" Grid.Column="1" BorderThickness="5" BorderBrush="DarkGray">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Grid.Column="0" Content="Form1"/>
<Button Grid.Row="0" Grid.Column="1" Content="Form2"/>
<Frame x:Name="inputFrame" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Background="Black"/>
</Grid>
</Border>
</Grid>
Frame Inside the Parent Page:
<Border Grid.Column="1" Grid.Row="2" Padding="10" Background="AliceBlue" BorderThickness="5" BorderBrush="DarkSlateGray" KeyUp="textInput">
<Grid VerticalAlignment="Center" HorizontalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="40"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Label Content="Beschreibung:" Grid.Column="1" Grid.Row="2" HorizontalAlignment="Right" Margin="0,0,10,0"/>
<TextBox x:Name="inputDesc" Grid.Column="2" Grid.Row="2" Grid.ColumnSpan="3" Width="150" TextAlignment="Center" VerticalAlignment="Center"/>
<Label Content="Zeit:" Grid.Column="1" Grid.Row="3" HorizontalAlignment="Right" Margin="0,10,10,10"/>
<TextBox x:Name="inputHours" Grid.Column="2" Grid.Row="3" Margin="0,10,0,10" TextAlignment="Center" VerticalAlignment="Center"/>
<Label Content=":" Grid.Column="3" Grid.Row="3" HorizontalAlignment="Center" Margin="5,10,5,10"/>
<TextBox x:Name="inputMins" Grid.Column="4" Grid.Row="3" Margin="0,10,0,10" TextAlignment="Center" VerticalAlignment="Center"/>
<Button x:Name="addBtn" IsEnabled="False" Content="Hinzufügen" Grid.Column="1" Grid.Row="4" Grid.ColumnSpan="4" Margin="0, 10, 0, 10" Click="addBtnClick"/>
<TextBlock x:Name="info" Grid.Column="1" Grid.Row="5" Grid.ColumnSpan="4" Grid.RowSpan="3" Margin="0,10,0,0" MaxWidth="150" TextWrapping="WrapWithOverflow" TextAlignment="Center" Height="40"/>
<ListView ItemsSource="{Binding items}" Name="listView" Grid.Column="3" Grid.Row="1" Grid.RowSpan="9" SelectionChanged="itemSelected" Background="WhiteSmoke">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Desc}" Header="Beschreibung"/>
<GridViewColumn DisplayMemberBinding="{Binding Time}" Header="Zeit"/>
<GridViewColumn DisplayMemberBinding="{Binding Date}" Header="Datum"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Border>

I found out how this can be solved:
You just give a reference of the parent page to the page in the frame through the constructor, like this:
private void changeToForm1(object sender, RoutedEventArgs e)
{
inputFrame.Content = new Form1(this);
}
Now, you can just save the reference into a variable and call methods to transfer the data to the parent page.

Related

Prism mvvm - TabControl does not display data

I'm using Prism. I have a ContextControl region, and a view associated with that region. Within the view is a TabControl. The controls are displaying data with no problem, but the controls within the TabControl are not displaying any data. I have tried setting the DataContext and ItemsSource(for the TabControl) , without success. The data to be displayed is from the SelectedSession class.
xaml
<!--session data grid-->
<Grid x:Name="SessionDataGrid"
Grid.Column="2"
Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ContentControl
Grid.Row="0"
Margin="0 0 0 0"
prism:RegionManager.RegionName="DataRegion"/>
</Grid>
View
<!--row 0-->
<Label x:Name="labelSessionData"
Content="Session Data"
Style="{DynamicResource LabelGeneric}"
Grid.Column="0"
Grid.Row="0"
HorizontalContentAlignment="Center"
Margin="1,2,0,0"/>
<!--row 1-->
<Label x:Name="labelSessionDataIdentifier"
Content="Identifier: "
Grid.Column="0"
Grid.Row="1"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Right"
Style="{DynamicResource LabelGeneric}"/>
<TextBox x:Name="textBoxSessionDataIdentifier"
Grid.Column="1"
Grid.Row="1"
HorizontalAlignment="Left"
Style="{DynamicResource textBoxDataN}"
Text="{Binding SelectedSession.Identifier, Mode=OneWay}"
Width="230"
Margin="0 4 0 3"/>
<Label x:Name="labelSessionDataCamera"
Content="Camera: "
Grid.Column="2"
Grid.Row="1"
HorizontalAlignment="Right"
HorizontalContentAlignment="Right"
Style="{DynamicResource LabelGeneric}"/>
<ComboBox x:Name="comboboxSessionDataCamera"
Grid.Column="3"
Grid.Row="1"
FlowDirection="LeftToRight"
HorizontalAlignment="Left"
HorizontalContentAlignment="Left"
IsEditable="True"
ItemsSource="{Binding Cameras}"
DisplayMemberPath="CameraName"
SelectedValue="{Binding SelectedSession.CameraId, Mode=TwoWay}"
SelectedValuePath="CameraId"
Style="{DynamicResource ComboboxData}"
Width="230"
Margin="0 0 0 0"/>
<!--row 3-->
<TabControl x:Name="tabControlSessionData"
Grid.Column="0"
Grid.ColumnSpan="4"
Grid.Row="3"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Margin="0,0,10,0"
Visibility="Hidden">
<TabItem Header="Analog">
<Grid Background="{StaticResource MainBackgroundColor}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="83"/>
<ColumnDefinition Width="8"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="10"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<!--row 0-->
<Label x:Name="labelSessionAnalogFilm"
Content="Film: "
Grid.Column="0"
Grid.ColumnSpan="2"
Grid.Row="0"
Style="{DynamicResource LabelGeneric}"
Margin="1,1,0,0"/>
<ComboBox x:Name="comboboxSessionAnalogFilm"
Grid.Column="2"
Grid.ColumnSpan="2"
Grid.Row="0"
ItemsSource="{Binding Films}"
DisplayMemberPath="FilmName"
SelectedValue="{Binding SelectedSession.FilmId, Mode=TwoWay}"
SelectedValuePath="FilmID"
Style="{DynamicResource ComboboxData}"
Margin="0 0 0 0"/>
<Label x:Name="labelSessionAnalogISO"
Content="ISO: "
Grid.Column="4"
Grid.Row="0"
HorizontalAlignment="Right"
HorizontalContentAlignment="Right"
Style="{DynamicResource LabelGeneric}"
Margin="2,1,0,0"/>
<TextBox x:Name="textBoxSessionAnalogFilmISO"
Grid.Column="5"
Grid.Row="0"
HorizontalAlignment="Left"
Style="{DynamicResource textBoxDataN}"
Text="{Binding FilmISO, Mode=TwoWay}"
Width="50"
Margin="0,4"/>
Any help would be appreciated.
I was getting the data from a DataGrid. Once I defined the columns for the data it started working.

C#/.xaml Having problem with WPF pages, grid vertical expansion

So here's my problem, when I expand the main window the pages presenter frame seems to expand too but only horizontaly. (see photo #1) The 5 buttons on the bottom stay at the original resolution position verticaly but expand horizontaly, I've put them between stars related columns and rows. It worked well for my wlecome pages an this methos seems to work horizontaly. I'm wonderind if the problem comes from previoulsy setuped proprieties. Problem is related to fifthline but can be post problems.
Images
====================================
Original size see 2 border buttons
Expanded window
Grid setup
Expand working on homepage *Grid setup
====================================
<Border Padding="1">
<StackPanel>
<!---FirstLine-->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="445"/>
<ColumnDefinition Width="365"/>
<ColumnDefinition Width="390"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="28"/>
</Grid.RowDefinitions>
<TextBlock Text="Prénom" Margin="30,5,165,0" Grid.Column="0" FontWeight="Black"/>
<TextBox Name="ClientPage_BoxPrenom" Text="Jean" Margin="100,-1,5,1" Padding="0,5" FontSize="12"/>
<TextBlock Text="Nom" Margin="5,4,245,1" Grid.Column="1" FontWeight="Black"/>
<TextBox Name="ClientPage_BoxNom" Text="Dujardins" Margin="60,-1,0,1" Grid.Column="1" Padding="0,5"/>
</Grid>
<!---SecondLine-->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="433"/>
<ColumnDefinition Width="472"/>
<ColumnDefinition Width="275"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="46"/>
</Grid.RowDefinitions>
<TextBlock Text="Ville" Margin="30,20,0,0" Grid.Column="0" FontWeight="Black"/>
<ComboBox Name="ClientPage_ComboVille" Margin="100,20,0,0" Padding="0,5" FontSize="12"/>
<CheckBox Name="ClientPage_CehckResponsable" Content="Responsable" Margin="10,25,361,0" Grid.Column="1" FontWeight="Medium"/>
</Grid>
<!---ThirdLine-->
<Grid Height="79">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="393"/>
<ColumnDefinition Width="787"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="74"/>
</Grid.RowDefinitions>
<TextBlock Text="Membre depuis:" Margin="30,50,0,0" Grid.Column="0"/>
<DatePicker Name="ClientPage_DatepickDatedebut" Text="Membre depuis:" Margin="130,50,770,0" Grid.ColumnSpan="2"/>
</Grid>
<!---FourthLine-->
<Grid Height="191">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="217"/>
<ColumnDefinition Width="373"/>
<ColumnDefinition Width="590"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="191"/>
</Grid.RowDefinitions>
<GroupBox Name="ClientPage_GroupExp" Header="Années d'expériences" Margin="14,12,6,8" Grid.Column="0">
<StackPanel Margin="0,0,-2,0">
<RadioButton Content="Moins d'un an" Margin="50,10,0,10"/>
<RadioButton Content="1 à 4 ans" Margin="50,10,0,10"/>
<RadioButton Content="5 à 9 ans" Margin="50,10,0,10"/>
<RadioButton Content="10 ans et plus" Margin="50,10,0,10"/>
</StackPanel>
</GroupBox>
<ListBox Name="ClientPage_List" Margin="26,22,144,8" Grid.Column="1"/>
<Button x:Name="ClientPage_Buttonnouveau_Copy1" Content="OriginalFrame
Top right" Grid.Column="2" Margin="471,-150,-1,287" Click="ClientPage_Buttonnouveau_Copy1_Click" />
<Button x:Name="ClientPage_Buttonnouveau_Copy" Content="OriginalFrame
Bottom Left" Margin="3,617,94,-480" Click="ClientPage_Buttonnouveau_Copy1_Click" />
<!---FifthLine-->
</Grid>
<Grid Height="484">
<Grid.RowDefinitions>
<RowDefinition Height="149*"/>
<RowDefinition Height="227*"/>
<RowDefinition Height="45"/>
<RowDefinition Height="63*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="433*"/>
<ColumnDefinition Width="251*"/>
<ColumnDefinition Width="65"/>
<ColumnDefinition Width="64"/>
<ColumnDefinition Width="15"/>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="120"/>
</Grid.ColumnDefinitions>
<Button x:Name="ClientPage_Buttoneffacer" Content="Effacer" Grid.Row="2" Grid.Column="0" Margin="0,0,2,4" IsEnabled="False"/>
<Button x:Name="ClientPage_Buttonprecedent" Content="<--" Grid.Row="2" Grid.Column="3" Margin="0,0,2,4" />
<Button x:Name="ClientPage_Buttonesuivant" Content="-->" Grid.Row="2" Grid.Column="4" Margin="0,0,1,4" />
<Button x:Name="ClientPage_Buttonajouter" Content="Ajouter" Grid.Row="2" Grid.Column="6" Margin="0,0,2,4" />
<Button x:Name="ClientPage_Buttonnouveau" Content="Nouveau" Grid.Row="2" Grid.Column="7" Margin="0,0,0,4" />
</Grid>
</StackPanel>
</Border>
The main problem you have is that you're using a StackPanel for your root layout panel. This is preventing your layout from expanding vertically.
You might want to consider using a Grid or a DockPanel instead.

WPF - Infinite datagrid height

I am using Syncfusion's SfDataGrid instead of standard one, but this question is more about XAML, not the control itself, I think.
So, I have a window with DataGrid. If there is for example 60 records, then the window gets really tall. I want the window not to change its size at all. And really don't know why it's happening.
This is my XAML:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel Margin="10,0,10,0" Grid.Row="0" DataContext="{Binding Info}">
<TextBlock Text="Amount:"/>
<sf:CurrencyTextBox Value="{Binding Amount}" Margin="0,0,0,10"/>
<!-- some other controls -->
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" Style="{StaticResource LeftLabel}"
Text="Count:"/>
<sf:UpDown Grid.Column="1" Grid.Row="0" Margin="0,0,0,5"
Value="{Binding Count}"/>
<TextBlock Grid.Column="0" Grid.Row="1" Style="{StaticResource LeftLabel}"
Text="Year count:"/>
<sf:UpDown Grid.Column="1" Grid.Row="1"
Value="{Binding YearCount}"/>
</Grid>
</StackPanel>
<Button Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,10,10,5"
Content="Simulate >>"
Command="{Binding SimulateCommand}"/>
</Grid>
<!-- This is right part of the window with datagrid -->
<DockPanel Grid.Column="1">
<WrapPanel DockPanel.Dock="Top">
<TextBlock Margin="10" Text="Total amount:" />
<TextBlock Text="{Binding TotalAmount"/>
</WrapPanel>
<!-- and the datagrid -->
<sf:SfDataGrid AutoGenerateColumns="False"
AllowDeleting="False"
AllowEditing="False"
IsReadOnly="False"
AllowGrouping="False"
AllowFiltering="False"
ItemsSource="{Binding History}">
<sf:SfDataGrid.Columns>
<sf:GridDateTimeColumn DisplayBinding="{Binding Date}" MappingName="PaymentDay" HeaderText="Date"/>
<sf:GridCurrencyColumn DisplayBinding="{Binding Amount}" MappingName="PayAmount" HeaderText="Amount"/>
</sf:SfDataGrid.Columns>
</sf:SfDataGrid>
</DockPanel>
</Grid>
So what is wrong with it?

Shared grid splitter

I have a grid that contains one ItemsControl with another grid. I would like to align the two splitters in the two grids. I have implement Grid.SharedSizeScope but this option shared only the column of the grid is located. Do you have any suggestions?
This is my xaml code.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Col1"/>
<ColumnDefinition Width="5" SharedSizeGroup="Col2"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="{StaticResource GridRowHeight}"/>
<RowDefinition Height="{StaticResource GridRowHeight}"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<GridSplitter Grid.Column="1" Grid.RowSpan="2" HorizontalAlignment="Left" Width="5" VerticalContentAlignment="Stretch" IsTabStop="False"/>
<TextBlock Name="ID" Grid.Row="0" Grid.Column="0" Text="ID"/>
<TextBox Name="TxtID" Grid.Row="0" Grid.Column="2" TabIndex="0" Text={Binding ID}/>
<TextBlock Name="Description" Grid.Row="1" Grid.Column="0" Text="Description"/>
<TextBox x:Name="TxtDescription" Grid.Row="1" Grid.Column="2" Grid.RowSpan="1" Text="{Binding Description, UpdateSourceTrigger=LostFocus}"/>
<ItemsControl ItemsSource="{Binding MyList}" Grid.IsSharedSizeScope="True" IsTabStop="False"
Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Col1"/>
<ColumnDefinition Width="5" SharedSizeGroup="Col2"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<GridSplitter Grid.Column="1" Grid.RowSpan="2147483647" Width="5" HorizontalAlignment="Left" VerticalContentAlignment="Stretch" IsTabStop="False"/>
<TextBlock Name="Type" Grid.Column="0" Grid.Row="0" Text="{Binding Type}"/>
<ComboBox Grid.Row="0" Grid.Column="2" ItemsSource="{Binding Source={StaticResource Types}}" SelectedItem="{Binding MyType}"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
ok, what if you will actually take out the itemscontrol and move it outside the grid and then put everything into dockpanel ?
<DockPanel Grid.IsSharedSizeScope="True">
<Grid DockPanel.Dock="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Col1"/>
<ColumnDefinition Width="5" SharedSizeGroup="Col2"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<GridSplitter Grid.Column="1" Grid.RowSpan="2" HorizontalAlignment="Left" Width="5" VerticalContentAlignment="Stretch" IsTabStop="False"/>
<TextBlock Name="ID" Grid.Row="0" Grid.Column="0" Text="ID"/>
<TextBox Name="TxtID" Grid.Row="0" Grid.Column="2" TabIndex="0" Text="ID"/>
<TextBlock Name="Description" Grid.Row="1" Grid.Column="0" Text="Description"/>
<TextBox x:Name="TxtDescription" Grid.Row="1" Grid.Column="2" Grid.RowSpan="1" Text="123"/>
</Grid>
<ItemsControl IsTabStop="False"
Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Col1"/>
<ColumnDefinition Width="5" SharedSizeGroup="Col2"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<GridSplitter Grid.Column="1" Grid.RowSpan="2147483647" Width="5" HorizontalAlignment="Left" VerticalContentAlignment="Stretch" IsTabStop="False"/>
<TextBlock Name="Type" Grid.Column="0" Grid.Row="0" Text="123"/>
<ComboBox Grid.Row="0" Grid.Column="2"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DockPanel>

WPF window throws TypeInitializationException at start up

I have an Excel AddIn with several ribbons, one ribbon is setting window.
When it is clicked, a custom window will show up.
However, there is nothing to show up when clicked.
I saw the following exceptions in log file. What causes this and how to fix it? thanks a lot
2012-04-09 09:59:50,161 [1] ERROR Helper [(null)] - Name:TypeInitializationException
Message:The type initializer for 'System.Windows.Window' threw an exception.
Target:Void .ctor()
Stack: at System.Windows.Window..ctor()
at MyShared.View.ConnectionSetup..ctor()
at MyAddIn.Connect.GetSettings()
at MyAddIn.Connect.BtnClick(IRibbonControl control)
Name:TypeInitializationException
Message:The type initializer for 'System.Windows.FrameworkElement' threw an exception.
Target:Void .cctor()
Stack: at System.Windows.Window..cctor()
Name:TypeInitializationException
Message:The type initializer for 'System.Windows.Documents.TextElement' threw an exception.
Target:Void .cctor()
Stack: at System.Windows.FrameworkElement..cctor()
Name:TypeInitializationException
Message:The type initializer for 'MS.Internal.FontCache.Util' threw an exception.
Target:Int32 get_Dpi()
Stack: at MS.Internal.FontCache.Util.get_Dpi()
at System.Windows.SystemFonts.ConvertFontHeight(Int32 height)
at System.Windows.Documents.TextElement..cctor()
Name:UriFormatException
Message:Invalid URI: The format of the URI could not be determined.
Target:Void CreateThis(System.String, Boolean, System.UriKind)
Stack: at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
at System.Uri..ctor(String uriString, UriKind uriKind)
at MS.Internal.FontCache.Util..cctor()
Edit, here is xaml code
<Window x:Class="MyShared.View.ConnectionSetup"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
xmlns:Converter="clr-namespace:MIMICShared.Converter"
WindowStartupLocation="CenterOwner"
Title="Settings" Width="446" Height="650" Closing="WindowClosing"
DataContextChanged="UserControlDataContextChanged" Foreground="Black" FontWeight="Normal" HorizontalAlignment="Stretch">
<Window.Resources>
<ResourceDictionary>
<Converter:Status2ImageConverter x:Key="string2Image"/>
</ResourceDictionary>
</Window.Resources>
<Grid FocusManager.FocusedElement="{Binding ElementName=uname}" >
<Grid.RowDefinitions>
<RowDefinition Height="0"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="0"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="0"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="5"></RowDefinition>
</Grid.RowDefinitions>
<GroupBox Grid.Row="1" Foreground="Blue">
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="5"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="2"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="2"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="2"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="2"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="2"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="2"></RowDefinition>
</Grid.RowDefinitions>
<Label Grid.Row="1" Grid.ColumnSpan="4" FontSize="14" HorizontalAlignment="Center" FontWeight="Bold" VerticalAlignment="Bottom">Historical</Label>
<GroupBox Grid.Column="1" Grid.Row="3" Header="Connections" Foreground="Blue" Grid.ColumnSpan="2">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" ></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<toolkit:DataGrid x:Name="connectionlist" Margin="5" Style="{DynamicResource DataGridStyleDefault}" Background="White"
AutoGenerateColumns="False" HeadersVisibility="All"
SelectionMode="Single"
GridLinesVisibility="None"
ItemsSource="{Binding SettingList}" CanUserAddRows="False" CanUserDeleteRows="True"
SelectionUnit="FullRow" SelectedItem="{Binding SelectedItem}"
SelectionChanged="ConnectionlistSelectionChanged"
VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
<!-- Column definition -->
<toolkit:DataGrid.Columns>
<!-- Pool -->
<!--<toolkit:DataGridCheckBoxColumn Header="Pool" Width="40" Binding="{Binding pool}" />-->
<!-- Connections info -->
<toolkit:DataGridTextColumn Header="Connection Info" Width="*" Binding="{Binding host}" />
<toolkit:DataGridTextColumn Header="Description" Width="*" Binding="{Binding desc}" >
<toolkit:DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextWrapping" Value="Wrap" />
</Style>
</toolkit:DataGridTextColumn.ElementStyle>
</toolkit:DataGridTextColumn>
</toolkit:DataGrid.Columns>
</toolkit:DataGrid>
<Grid Grid.Row="2" HorizontalAlignment="Center">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Margin="2" Padding="10 0" x:Name="AddButton" Click="AddClicked">Add</Button>
<Button Grid.Column="1" Margin="2" Padding="10 0" Command="{Binding Remove}">Remove</Button>
</Grid>
</Grid>
</Grid>
</GroupBox>
<GroupBox Header="Authentication" Grid.Row="5" Grid.Column="1" Foreground="Blue" Grid.ColumnSpan="2">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="2"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="2"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0">Username:</Label>
<TextBox x:Name="uname" Width="100" Grid.Row="0" Grid.Column="2" Text="{Binding UserName, UpdateSourceTrigger=PropertyChanged}" ></TextBox>
<Label Grid.Row="2" Grid.Column="0">Password:</Label>
<PasswordBox x:Name="pwd" ToolTip="Password" PasswordChar="*"
Width="100" Grid.Row="2" Grid.Column="2" KeyDown="OnKeyDownHandler"></PasswordBox>
<Label Grid.Row="2" Grid.Column="3">
<Hyperlink Click="HyperlinkClick">Forgot Password?</Hyperlink>
</Label>
<CheckBox Margin="10 0 0 0" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" x:Name="saveauth" IsChecked="{Binding SaveAuth}">Save username and password</CheckBox>
<Button Margin="10 0 0 0" Grid.Row="4" Grid.Column="3" Click="VerifyButtonClick">OK</Button>
</Grid>
</GroupBox>
<GroupBox Header="Status" Grid.Row="7" Grid.Column="1" Foreground="Blue" Grid.ColumnSpan="2">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="0"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="1" Grid.Column="0">Historical</Label>
<Image Grid.Row="1" Grid.Column="1"
Source="{Binding ElementName=historicalStatusLabel, Path=Content, Converter={StaticResource string2Image}}"
ToolTip="Connection Status"></Image>
<Label Name="historicalStatusLabel" Grid.Row="1" Grid.Column="2" Content="{Binding ConnectionStatus, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
</GroupBox>
</Grid>
</GroupBox>
<GroupBox Grid.Row="3" Grid.Column="1" Foreground="Blue">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="0"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="0"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="0"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="0"></RowDefinition>
</Grid.RowDefinitions>
<Label Grid.Row="1" FontSize="14" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Bottom">Real Time</Label>
<GroupBox Header="Authorization" Grid.Row="3" Grid.Column="1" Foreground="Blue">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="2"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="2"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="2"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0">Username:</Label>
<TextBox x:Name="RTDemail" Width="100" Grid.Row="0" Grid.Column="2" Text="{Binding RTDUserName, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<Label Grid.Row="2" Grid.Column="0">Password:</Label>
<PasswordBox x:Name="RTDpwd" ToolTip="Password" PasswordChar="*"
Width="100" Grid.Row="2" Grid.Column="2" KeyDown="OnKeyDownHandler"></PasswordBox>
<Label Grid.Row="2" Grid.Column="3">
<Hyperlink Click="HyperlinkClickRTD">Forgot Password?</Hyperlink>
</Label>
<CheckBox Margin="10 0 0 0" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" x:Name="RTDsaveauth" IsChecked="{Binding RTDSaveAuth}">Save username and password</CheckBox>
<Button Margin="10 0 0 0" Grid.Row="4" Grid.Column="3" Click="RTDVerifyButtonClick">OK</Button>
</Grid>
</GroupBox>
<GroupBox Grid.Row="5" Grid.ColumnSpan="3">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Grid.Column="1" Margin="0 0 0 0" Foreground="Blue">Server</Label>
<Label Name="RTDServerURLLabel" Grid.Column="2" Content="{Binding RTDServerURL}"></Label>
</Grid>
</GroupBox>
<GroupBox Header="Status" Grid.Row="7" Grid.Column="1" Foreground="Blue">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="2"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="2"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Grid.Row ="1" Grid.Column="0">Real Time</Label>
<Image Grid.Row ="1" Grid.Column="1"
Source="{Binding ElementName=RTDStatusLabel, Path=Content, Converter={StaticResource string2Image}}"
ToolTip="Connection Status" HorizontalAlignment="Left" Width="20"></Image>
<Label Name="RTDStatusLabel" Grid.Row="1" Grid.Column="2" Content="{Binding RTDConnectionStatus, UpdateSourceTrigger=PropertyChanged}"></Label>
</Grid>
</GroupBox>
</Grid>
</GroupBox>
<Grid Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="3" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="60"></ColumnDefinition>
<ColumnDefinition Width="20"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button x:Name="CloseButton" Click="CloseButtonClick" Grid.Column ="1" Margin="0,0,0,0">Close</Button>
</Grid>
</Grid>
When ribbon is clicked, the code is executed, ConnectionSetup is the xaml file above
private bool GetSettings()
{
var settingUI = new ConnectionSetup();
settingUI.DataContext = Settings;
CenterMainWindow(settingUI, wndHandle.Handle);
bool? result = settingUI.ShowDialog();
return result ?? false;
}
private void CenterMainWindow(System.Windows.Window window, IntPtr ownerHandle)
{
var helper = new System.Windows.Interop.WindowInteropHelper(window) {Owner = ownerHandle};
// Center window
// Note - Need to use HwndSource to get handle to WPF owned window,
// and the handle only exists when SourceInitialized has been
// raised
window.SourceInitialized += delegate
{
// Get WPF size and location for non-WPF owner window
var nonWPFOwnerLeft = XLApp.Left;
var nonWPFOwnerWidth = XLApp.Width;
var nonWPFOwnerTop = XLApp.Top;
var nonWPFOwnerHeight = XLApp.Height;
// Get transform matrix to transform non-WPF owner window
// size and location units into device-independent WPF
// size and location units
var source = System.Windows.Interop.HwndSource.FromHwnd(helper.Handle);
if (source != null && source.CompositionTarget != null)
{
var matrix = source.CompositionTarget.TransformFromDevice;
var ownerWPFSize =
matrix.Transform(new System.Windows.Point(nonWPFOwnerWidth,
nonWPFOwnerHeight));
var ownerWPFPosition =
matrix.Transform(new System.Windows.Point(nonWPFOwnerLeft,
nonWPFOwnerTop));
// Center WPF window
window.WindowStartupLocation = WindowStartupLocation.Manual;
window.Left = Math.Max(0,
ownerWPFPosition.X +
(ownerWPFSize.X - window.Width)/2);
window.Top = Math.Max(0,
ownerWPFPosition.Y +
(ownerWPFSize.Y - window.Height)/2);
}
};
}
Judging from the error stack you posted, it appears that your settings window probably has a link on it. Unfortunately, the link has an invalid url in it, which causes the creation of the window to fail.
EDIT:
Having looked more closely at your code and error messages, it's clear that I was looking in the wrong area. Some searching turned up a bug in wpf.
The short version: the windir environment variable may be set incorrectly on the target machine, which causes the font subsytem to break. The workaround is to fix the environment variable, either on the target machine's registry, or in the code by adding something like the following to the startup:
Environment.SetEnvironmentVariable("windir", Environment.GetEnvironmentVariable("SystemRoot"));
We had a couple of people experience this problem. The solution described here (caused by PATH being greater than 2048 characters) resolved the issue.
I am concerned that the statement suggested above may be problematic in code that is running with either too many or too few privileges. Certainly my app running with on my workstation with me as administrator has no issue with this call to
Environment.SetEnvironmentVariable("windir",Environment.GetEnvironmentVariable("SystemRoot"));
but I am not sure that SetEnvironmentVariable will actually work on a locked-down OS. Has anybody a) tried this for real and b) tried it in a low-privilege environment>
Also, I am seeing some oddity on app startup after I install an app with this call, and run it once. All other applications no longer can see windir,which is not expanding to C:\Windows. I am going to go through the whole install and run without it in place and see if either the TypeInvocation exception resurfaces or if not, whether the OS windir variable is left intact.
UPDATE:
I have confirmed that the bad behavior I noticed was due to not specifying the target of the SetEnvironment call. Using the following my problems seem to have been alleviated:
System.Environment.SetEnvironmentVariable("windir", System.Environment.GetEnvironmentVariable("SystemRoot"), EnvironmentVariableTarget.User);
When I did not set the target, very odd things happened to my Windows session requiring me to either logoff and login or even reboot. So unless this change is really to be at the machine or process level, do not assume happy results with the default

Categories

Resources