I have a ContentControl inside a TabItem. The ContentControl displays a UserControl. My Problem is the Width of the ContentControl isn't set corectly, it is way to big. The Height is correctly displayd.
I want to SizeToContent because I have multiple TabItems with different sizes.
I have the following code:
MainWindow.xaml (Short Version)
<Window Height="350" Width="525" SizeToContent="WidthAndHeight">
<DockPanel>
<TabControl DockPanel.Dock="Left" TabStripPlacement="Left">
<TabItem Header="BDE Config" IsEnabled="{Binding LoggedIn}">
<Grid>
<ContentControl Content="{Binding CurrentViewModel}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</Grid>
</TabItem>
</TabControl>
</DockPanel>
The UserControl doesn't has Width/Height Properties. So why is it that the Height is correct but the Width way to big for my Screen.
XAML of the UserControl:
<UserControl x:Class="BDE_Config2015.Views.BDEConfig"
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:BDE_Config2015"
xmlns:h="clr-namespace:BDE_Config2015.Helpers"
mc:Ignorable="d"
Background="{StaticResource AppBg}" d:DesignHeight="655" d:DesignWidth="1180">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<!--1-->
<GroupBox Grid.Column="0" Grid.Row="0" Header="BDE-Benutzer">
<ComboBox Height="23" DisplayMemberPath="Benutzername" SelectedValuePath="Kbez" ItemsSource="{Binding BDEConfigModel.BDEBenutzer}" SelectedValue="{Binding CurrentUser}" />
</GroupBox>
<!--2-->
<TabControl Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" Background="Transparent" Margin="0,3,0,0">
<TabItem Header="BDE">
<Grid x:Name="gBDEKonfig">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition />
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="80"/>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition Width="70"/>
<ColumnDefinition Width="80"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<!--Row 0-->
<TextBlock Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0" Text="Buttons pro Reiter" VerticalAlignment="Center" Visibility="Collapsed" />
<TextBox Grid.Column="2" Grid.Row="0" Name="txtBDEReiter" Margin="0,0,0,5" Visibility="Collapsed" />
<TextBlock Grid.Column="3" Grid.Row="0" Text="Hintergrundfarbe" VerticalAlignment="Center" Margin="10,0,0,0" Visibility="Collapsed" />
<TextBox Grid.Column="4" Grid.Row="0" Margin="0,0,0,5" Visibility="Collapsed" />
<Button Grid.Column="5" Grid.Row="0" Margin="10,0,0,5" Visibility="Collapsed">...</Button>
<!--Row 1-->
<TextBlock Grid.Column="0" Grid.Row="1" Text="BDE-Titel" VerticalAlignment="Top"/>
<TextBox Grid.Column="1" Grid.ColumnSpan="3" Grid.Row="1" Margin="0,0,0,5" Text="{Binding BDETitel, UpdateSourceTrigger=LostFocus}" KeyDown="TextBox_KeyDown" />
<!--Row 2-->
<TextBlock Grid.Column="0" Grid.Row="2" Text="BDE-Belegung"/>
<!--Row 3-->
<DataGrid Grid.Column="0" Grid.ColumnSpan="8" Grid.Row="3" ItemsSource="{Binding BDEConfigModel.Clientoptionen}" AutoGenerateColumns="False" CanUserAddRows="False" CanUserReorderColumns="False"
SelectedValue="{Binding crBDEBelegung}" Name="dgvBDEBelegung" CellEditEnding="DGV_CellEditEnding" CurrentCellChanged="DGV_CurrentCellChanged"
h:DataGridBehavior.AutoScrollIntoView="True" SelectionMode="Single">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Nummer, UpdateSourceTrigger=LostFocus}" Header="Nr" />
<DataGridTextColumn Binding="{Binding OptionText, UpdateSourceTrigger=LostFocus}" Header="Arbeitsplatztitel" />
<DataGridTextColumn Binding="{Binding OptionInfo, UpdateSourceTrigger=LostFocus}" Header="Buttonbezeichnung"/>
<DataGridTemplateColumn Header="Anlage" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox DisplayMemberPath="Anlage" SelectedValuePath="FertigungsStättenID" SelectedValue="{Binding OptionZahl}" IsTextSearchEnabled="True"
ItemsSource="{Binding Path=DataContext.BDEConfigModel.Fertigungsstaette, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
SelectedItem="{Binding Path=DataContext.siFertigungsstaette, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Mode=OneWayToSource}">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Arbeitsplatz" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox DisplayMemberPath="Arbeitsplatzbez" SelectedValuePath="ArbeitsPlatzNummer" SelectedValue="{Binding OptionGanzzahl}" IsTextSearchEnabled="True"
ItemsSource="{Binding Path=DataContext.BDEConfigModel.Arbeitsplatz, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
SelectedItem="{Binding Path=DataContext.siArbeitsplatz, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Mode=OneWayToSource}">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<!--Row 4-->
<Button Grid.Column="0" Grid.Row="4" Margin="5" Command="{Binding NeuBDEBelegungCommand}">Neu</Button>
<Button Grid.Column="1" Grid.Row="4" Margin="5" Command="{Binding LoeschenBDEBelegungCommand}">Löschen</Button>
</Grid>
</TabItem>
<TabItem Header="Arbeitsgang/Arbeitsplan">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*"/>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="Arbeitsplan"/>
<DataGrid Grid.Row="1">
</DataGrid>
<CheckBox Grid.Row="2" HorizontalAlignment="Right" Content="Unabhängig vom Arbeitsplan" Margin="0,10,5,0" />
<TextBlock Grid.Row="3" Text="Arbeitsgang"/>
<DataGrid Grid.Row="4">
</DataGrid>
</Grid>
</TabItem>
<TabItem Header="ColumnsManagement">
<Grid Margin="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="Arbeitsplatz" Margin="5,0" />
<ComboBox Grid.Column="1" Margin="5,0,5,5" ItemsSource="{Binding BDEConfigModel.BDEArbeitsplatz}" DisplayMemberPath="OptionText" SelectedValuePath="PKID"
SelectedValue="{Binding SelectedArbeitsplatzCM}" />
<TextBlock Grid.Column="2" Text="DataSource" Margin="5,0" />
<ComboBox Grid.Column="3" Margin="5,0,5,5" SelectedValue="{Binding SelectedDataSource}">
<ComboBox.ItemsSource>
<x:Array Type="sys:String" xmlns:sys="clr-namespace:System;assembly=mscorlib">
<sys:String>proc_BDEoW_ChargeStartDetail</sys:String>
<sys:String>proc_BDEoW_ChargeProduktion</sys:String>
</x:Array>
</ComboBox.ItemsSource>
</ComboBox>
<Button Grid.Column="4" Content="Laden" Width="85" HorizontalAlignment="Right" Margin="5,0,5,5" Command="{Binding LadenCMCommand}"/>
</Grid>
<DataGrid Grid.Row="1" ItemsSource="{Binding BDEConfigModel.ArbeitsplatzSpaltenManagement}" AutoGenerateColumns="False" CanUserAddRows="False" CanUserReorderColumns="False"
IsEnabled="{Binding EnabledCM}" SelectedValue="{Binding crArbeitsplatzSM}" h:DataGridBehavior.AutoScrollIntoView="True">
<DataGrid.Columns>
<DataGridTextColumn Width="Auto" Header="Spalte" Binding="{Binding SQLVisibleColumns}" />
<DataGridTextColumn Width="*" Header="Titel" Binding="{Binding SQLVisibleColumnsName}" />
<DataGridTextColumn Width="*" Header="%Weite" Binding="{Binding SQLVisibleColumnsWidth}" />
<DataGridTextColumn Width="*" Header="ReadOnly" Binding="{Binding SQLVisibleColumnsAccess}" />
</DataGrid.Columns>
</DataGrid>
<TextBlock Grid.Row="2" Text="{Binding MessageCM}" HorizontalAlignment="Stretch" Margin="5,0,5,0" />
<Grid Grid.Row="3">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Content="Neu" Margin="5" Command="{Binding NeuCMCommand}" IsEnabled="{Binding EnabledCM}" />
<Button Grid.Column="1" Content="Löschen" Margin="5" Command="{Binding LoeschenCMCommand}" IsEnabled="{Binding EnabledCM}" />
<Button Grid.Column="2" Content="Con. Kopieren" Margin="5" Command="{Binding KopierenCMCommand}" IsEnabled="{Binding EnabledCM}" />
<TextBox Grid.Column="3" IsReadOnly="True" Margin="5" Text="{Binding SummeCM}"/>
<Button Grid.Column="4" Content="Speichern" Margin="5" Command="{Binding SpeichernCMCommand}" IsEnabled="{Binding EnabledCM}" />
</Grid>
</Grid>
</TabItem>
</TabControl>
<!--3-->
<GroupBox Grid.Column="2" Grid.Row="0" Grid.RowSpan="2" Header="Modul-Info" Name="gbModulInfo" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"/>
<ColumnDefinition />
<ColumnDefinition Width="80"/>
</Grid.ColumnDefinitions>
<DataGrid Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="0" ItemsSource="{Binding BDEConfigModel.BDEModul}" AutoGenerateColumns="False" CanUserAddRows="False" CanUserReorderColumns="False"
SelectedValue="{Binding crModulInfo}" h:DataGridBehavior.AutoScrollIntoView="True">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Typ}" Header="Typ" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Bezeichnung}" Header="Bezeichnung" Width="*" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
<Button Grid.Column="0" Grid.Row="1" Content="V Spez." Margin="5" Command="{Binding VSpezCommand}"/>
<Button Grid.Column="2" Grid.Row="1" Content="V Global" Margin="5" Command="{Binding VGlobalCommand}"/>
</Grid>
</GroupBox>
<!--4-->
<GroupBox Grid.Column="0" Grid.Row="2" Header="Register" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Grid Name="gRegister">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="80"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<DataGrid Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="0" ItemsSource="{Binding BDEConfigModel.Register}" AutoGenerateColumns="False" CanUserAddRows="False" CanUserReorderColumns="False"
SelectedValue="{Binding crRegister}" h:DataGridBehavior.AutoScrollIntoView="True" CellEditEnding="DGV_CellEditEnding" CurrentCellChanged="DGV_CurrentCellChanged">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Nummer, UpdateSourceTrigger=LostFocus}" Header="Nr" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding OptionText, UpdateSourceTrigger=LostFocus}" Header="Registername" Width="*"/>
</DataGrid.Columns>
</DataGrid>
<Button Grid.Column="0" Grid.Row="1" Content="Neu" Margin="5" Command="{Binding NeuRegisterCommand}" />
<Button Grid.Column="1" Grid.Row="1" Content="Löschen" Margin="5" Command="{Binding LoeschenRegisterCommand}" />
</Grid>
</GroupBox>
<!--5-->
<GroupBox Grid.Column="1" Grid.Row="3" Header="Spez. Modulzuordnung" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<DataGrid Name="dgvMZ" Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0" ItemsSource="{Binding BDEConfigModel.BDEMZ}" AutoGenerateColumns="False" CanUserAddRows="False"
CanUserReorderColumns="False" SelectedValue="{Binding crMZ}" h:DataGridBehavior.AutoScrollIntoView="True" CellEditEnding="DGV_CellEditEnding" CurrentCellChanged="DGV_CurrentCellChanged">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Modul}" Header="Typ" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Modullevel, UpdateSourceTrigger=LostFocus}" Header="Level" />
<DataGridTextColumn Binding="{Binding Parameter1, UpdateSourceTrigger=LostFocus}" Header="Parameter" Width="*" />
<DataGridTextColumn Binding="{Binding ModulSetup, UpdateSourceTrigger=LostFocus}" Header="Setup" Width="*"/>
</DataGrid.Columns>
</DataGrid>
<Button Grid.Column="0" Grid.Row="1" Content="Löschen" Margin="5" Command="{Binding LoeschenMZCommand}"/>
</Grid>
</GroupBox>
<!--6-->
<GroupBox Grid.Column="2" Grid.Row="3" Header="Global Modulzuordnung" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<DataGrid Name="dgvMZGlobal" Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0" ItemsSource="{Binding BDEConfigModel.BDEMZGlobal}" AutoGenerateColumns="False" CanUserAddRows="False"
CanUserReorderColumns="False" SelectedValue="{Binding crMZGlobal}" h:DataGridBehavior.AutoScrollIntoView="True" CellEditEnding="DGV_CellEditEnding"
CurrentCellChanged="DGV_CurrentCellChanged">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Modul}" Header="Typ" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding Modullevel, UpdateSourceTrigger=LostFocus}" Header="Level"/>
<DataGridTextColumn Binding="{Binding Parameter1, UpdateSourceTrigger=LostFocus}" Header="Parameter" Width="*"/>
<DataGridTextColumn Binding="{Binding ModulSetup, UpdateSourceTrigger=LostFocus}" Header="Setup" Width="*"/>
</DataGrid.Columns>
</DataGrid>
<Button Grid.Column="0" Grid.Row="1" Content="Löschen" Margin="5" Command="{Binding LoeschenMZGlobalCommand}"/>
</Grid>
</GroupBox>
</Grid>
It seems to be related to a strange interaction between the SizeToContent property on the window and the "star-sizing" on the data grid columns.
Either set the window's SizeToContent property to Height and explicitly set a width for the window, or limit the width of the star-sized columns by setting their MaxWidth properties to some suitable limit.
This thread on CodePlex describes the problem. Apparently things used to be worse in that an exception would be thrown! From a post at the very bottom of that thread:
The exception has been fixed but it's still not working properly. When placed in a user control with (sic) doesn't have a fixed width, setting "*" on column width will cause the column to be extremely wide (several thousand pixel), and this happens both at design time and run time.
As an aside, you could get rid of a lot of XAML markup by using StackPanel and DockPanel containers rather than using a Grid for everything.
After some more trying I have found an Answert that works for me. I have done the following:
MaxWidth="{x:Static SystemParameters.PrimaryScreenWidth}" WindowStartupLocation="CenterScreen" Left="{Binding winLeft, Mode=TwoWay}" Top="{Binding winTop, Mode=TwoWay}"
It sets the MaxWidth of the Window the the Screen Width and then whenever a User Control with Star-Sized Columns is loaded it also sets the Position to 0,0.
Related
I have a Grid in another Grid and add the second Grid into ScrollViewer but don't see scrollbar for it. The grid i add ScrollViewer has the name InfoGrid. I dont understand why ScrollViewer is not working. Is there any alternative way you can suggest me ? My xaml code for the all window :
<Window x:Class="ModelView.KillCardCheck"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="Auto" Height="Auto" SizeToContent="WidthAndHeight"
ResizeMode="NoResize" Icon="pack://application:,,,/Resources/image/ffdc_gui.png"
xmlns:lex="http://schemas.root-project.org/xaml/presentation" Closing="WindowClosing">
<Grid Style="{DynamicResource GridStyle}" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="12"/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="11"/>
<ColumnDefinition Width="109"/>
<ColumnDefinition Width="250"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<DataGrid Grid.Column="1" Grid.ColumnSpan="3" Grid.Row="2" Style="{DynamicResource {x:Type DataGrid}}"
AutoGenerateColumns="False" CanUserSortColumns="False" CanUserAddRows="False"
Margin="4,4,4,4" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" RowHeight="25" Name="dgKillCardChecks"
SelectionMode="Single" BorderThickness="1,1,1,0">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Number}"
Width="30" IsReadOnly="True">
</DataGridTextColumn>
<DataGridTemplateColumn Header="{lex:LocText Key=check_list, Dict=language, Assembly=FfdcLocalization}" Width="*" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="chkCheckList" Content="{Binding CheckItem}"
Click="CheckListCheckBoxOnClick"
Tag="{Binding Index}"
IsChecked="{Binding Path=CheckStatus, UpdateSourceTrigger=PropertyChanged}"
IsHitTestVisible="{Binding Enabled}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<ScrollViewer VerticalScrollBarVisibility="Visible">
<Grid Style="{DynamicResource GridStyle}" Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="3" Margin="4,4,4,4" Name="InfoGrid" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
</Grid>
</ScrollViewer>
<TextBlock Name="descriptionBlock" TextWrapping="Wrap" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="3" Margin="4,4,4,4"></TextBlock>
<Button Style="{DynamicResource {x:Type Button}}" Grid.Row="4" Grid.Column="3" Grid.ColumnSpan="1"
x:Name="btnQuit" MaxHeight="30" Margin="2,4,0,6" MaxWidth="120" MinWidth="80"
Content="{lex:LocText Key=close, Dict=language, Assembly=FfdcLocalization}"
VerticalContentAlignment="Center" MinHeight="30" VerticalAlignment="Center" HorizontalAlignment="Left" Click="CloseClick"/>
<Button Style="{DynamicResource {x:Type Button}}" Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2"
x:Name="btnClear" MaxHeight="30" Margin="2,4,0,6" MaxWidth="200" MinWidth="80"
Content="{lex:LocText Key=clear_killcard, Dict=language, Assembly=FfdcLocalization}"
VerticalContentAlignment="Center" MinHeight="30" VerticalAlignment="Center" HorizontalAlignment="Right" Click="ClearKillCardClick"/>
</Grid>
</Window>
The ScrollViewer that you made has no grid.Row set, instead you put it on the grid inside it. That does not make much sense since the grid ("Info Grid") is inside the ScrollViewer so it's parent is the ScrollViewer. You might want to try setting the ScrollViewer row and column.
I need to glue content to window sides, would also like to add a gridsplitter to resize internal control separation, and the status bar creates whitespace.
The Grid won't resize vertically with the bottom of the window for instance.
Currently, I am yet unaware of the cause or resolution, starting to go blind on the code. I could use a second pair of eyes.
Change Heights into MinHeights, use Grid containers, etc.
Pretty much anything I could find and think of, including staring down the code...
<Window x:Class="Teachers_Lounge.MainWindow"
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"
xmlns:local="clr-namespace:Teachers_Lounge"
mc:Ignorable="d"
Title="Teachers Lounge"
Height="625"
Width="1525"
MinHeight="625"
MinWidth="1525"
KeyDown="Window_KeyDown"
Closing="Window_Closing" Loaded="Window_Loaded">
<Window.Resources>
<DataTemplate x:Key="CalendarTemplate">
<Grid Margin="1">
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition Height="20" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="265" />
<ColumnDefinition Width="50" />
</Grid.ColumnDefinitions>
<DockPanel Grid.Row="0" Grid.Column="0">
<TextBlock FontWeight="Bold" Text="Name:" DockPanel.Dock="Left" Margin="5,0,10,0" />
<TextBlock Text="" />
<TextBlock Text="{Binding EventName}" Foreground="Green" FontWeight="Bold" />
</DockPanel>
<DockPanel Grid.Row="1" Grid.Column="0">
<TextBlock FontWeight="Bold" Text="Date:" Foreground="DarkGray" DockPanel.Dock="Left" Margin="5,0,5,0" />
<TextBlock Text="{Binding EventDateTime}" />
</DockPanel>
<Grid Grid.Column="1" Grid.RowSpan="2" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="-5,0,5,0">
<Button x:Name="EditEvent_Button" Width="40" Height="15" Margin="0,5,0,0" VerticalAlignment="Top" BorderThickness="0" Background="LightGray" CommandParameter="{Binding}" Command="{Binding ElementName=Window, Path=OnClickEditEvent}" Click="EditEvent_Button_Click">
<TextBlock Margin="0, -2">Edit</TextBlock>
</Button>
<Button x:Name="DeleteEvent_Button" Width="40" Height="15" Margin="0,20,0,5" VerticalAlignment="Bottom" BorderThickness="0" Background="LightGray" CommandParameter="{Binding}" Command="{Binding ElementName=Window, Path=OnClickDeleteEvent}" Click="DeleteEvent_Button_Click">
<TextBlock Margin="0, -2">Delete</TextBlock>
</Button>
</Grid>
</Grid>
</DataTemplate>
</Window.Resources>
<Grid MinHeight="586" VerticalAlignment="Top">
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="*" />
<RowDefinition Height="20" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="360" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBox x:Name="DateAndTime" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" Height="30" Margin="10,10,0,0" TextWrapping="Wrap" Text="dddd dd mmmm HHh" VerticalAlignment="Top" Width="350" FontSize="20" IsEnabled="False"/>
<TextBox x:Name="Topic" Grid.Row="0" Grid.Column="1" Margin="10,10,77,10" TextWrapping="Wrap" Text="Topic" FontSize="20" FontWeight="Bold" KeyDown="Topic_KeyDown" LostFocus="Topic_LostFocus" />
<Button x:Name="Settings_Button" Grid.Column="1" Margin="0,10,10,10" FontWeight="Bold" Click="Settings_Button_Click" HorizontalAlignment="Right" Width="62">Settings</Button>
<Grid Grid.Row="1" Grid.Column="0"
Margin="10,0,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition Height="489*" />
<RowDefinition Height="7*"/>
</Grid.RowDefinitions>
<Menu BorderThickness="1 1 1 0" BorderBrush="Black">
<Menu.ItemsPanel>
<ItemsPanelTemplate>
<DockPanel HorizontalAlignment="Stretch"/>
</ItemsPanelTemplate>
</Menu.ItemsPanel>
<MenuItem Header="New Event" Click="NewReplacement_Click" />
</Menu>
<ListBox x:Name="CalendarList" Grid.Row="1"
BorderThickness="1 0 1 1" BorderBrush="Black"
HorizontalAlignment="Left" MinWidth="350" MinHeight="485" VerticalAlignment="Top"
ItemsSource="{Binding Tables[0]}" ItemTemplate="{StaticResource CalendarTemplate}" KeyDown="CalendarList_KeyDown"/>
</Grid>
<Grid Grid.Row="1" Grid.Column="1"
HorizontalAlignment="Left" MinHeight="485" MinWidth="1072" VerticalAlignment="Top" Margin="10,0,10,0">
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Menu BorderThickness="1 1 1 0" BorderBrush="Black">
<Menu.ItemsPanel>
<ItemsPanelTemplate>
<DockPanel HorizontalAlignment="Stretch"/>
</ItemsPanelTemplate>
</Menu.ItemsPanel>
<MenuItem Header="🠜" Click="ExcelPreviousDate_Click" />
<MenuItem Header="🡄" Click="ExcelPreviousWeek_Click" />
<MenuItem x:Name="ExcelActiveTimewindow" Header="Week XX, Date dd-MM-yy" IsEnabled="False" />
<MenuItem Header="🡆" Click="ExcelNextWeek_Click" />
<MenuItem Header="🠞" Click="ExcelNextDate_Click" />
<Separator />
<MenuItem Header="New Replacement" Click="NewReplacement_Click" />
<MenuItem Header="Delete Replacements" HorizontalAlignment="Right" Click="DeleteReplacements_Click" />
</Menu>
<DataGrid Name="ExcelDataView" Grid.Row="1"
BorderThickness="1 0 1 1" BorderBrush="Black"
AutoGenerateColumns="False"
ColumnWidth="*"
RowBackground="WhiteSmoke"
Background="LightBlue"
CanUserAddRows="False"
BeginningEdit="ExcelDataView_BeginningEdit"
CellEditEnding="ExcelDataView_CellEditEnding"
RowEditEnding="ExcelDataView_RowEditEnding"
SelectionChanged="ExcelDataView_SelectionChanged"
MinWidth="1077" MinHeight="485" HorizontalAlignment="Left" VerticalAlignment="Top">
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader">
<EventSetter Event="Click" Handler="OnExcelHeaderClicked" />
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="Time" Binding="{Binding TimeText, Mode=TwoWay}" Width="70" CanUserSort="False" />
<DataGridTextColumn Header="Replaced User" Binding="{Binding ReplacedUser, Mode=TwoWay}" Width="140" CanUserSort="False" />
<DataGridTextColumn Header="Replacement User" Binding="{Binding ReplacementUser, Mode=TwoWay}" Width="140" CanUserSort="False" />
<DataGridTextColumn Header="Class" Binding="{Binding Class, Mode=TwoWay}" Width="170" CanUserSort="False" />
<DataGridTextColumn Header="Location" Binding="{Binding Location, Mode=TwoWay}" Width="170" CanUserSort="False" />
<DataGridTextColumn Header="Info" Binding="{Binding AdditionalInfo, Mode=TwoWay}" CanUserSort="False" />
<DataGridCheckBoxColumn Header="Mark" Binding="{Binding Marked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" CanUserSort="False" Width="50">
<DataGridCheckBoxColumn.CellStyle>
<Style>
<EventSetter Event="CheckBox.Checked" Handler="OnExcelDeleteColumnChecked"/>
<EventSetter Event="CheckBox.Unchecked" Handler="OnExcelDeleteColumnUnchecked"/>
</Style>
</DataGridCheckBoxColumn.CellStyle>
</DataGridCheckBoxColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
<Menu Grid.Row="3" Grid.ColumnSpan="2"
BorderThickness="0 1 0 0" BorderBrush="Black" IsEnabled="False"
VerticalAlignment="Bottom" Height="20">
<Menu.ItemsPanel>
<ItemsPanelTemplate>
<DockPanel HorizontalAlignment="Stretch"/>
</ItemsPanelTemplate>
</Menu.ItemsPanel>
<MenuItem MinWidth="1111" BorderThickness="0">
<MenuItem.Header>
<TextBlock x:Name="StatusbarMessage"
HorizontalAlignment="Left" VerticalAlignment="Center"
TextAlignment="Left" />
</MenuItem.Header>
</MenuItem>
<MenuItem BorderThickness="0"
HorizontalAlignment="Right" HorizontalContentAlignment="Right" Width="350" >
<MenuItem.Header>
<TextBlock x:Name="StatusbarInfo"
VerticalAlignment="Center" Width="335"
TextAlignment="Right" />
</MenuItem.Header>
</MenuItem>
</Menu>
</Grid>
</Window>
A proper layout that resizes as it should is what I'd expect.
Some weird behavior and glue-ing are what is happening though.
I am working on some view in wpf mvvm. I don't know how to fix columns and rows to show correctly my datagrid. I want to have one textbox under name, one textbox under value
Here is a image and xaml code, can anybody help me?
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DataGrid ItemsSource="{Binding EnviromentVariables}" IsReadOnly="False" Margin="10" AutoGenerateColumns="False" Grid.ColumnSpan="4">
<DataGrid.Columns>
<DataGridTextColumn x:Name="name" Width="*" Header="Name" Binding="{Binding Name}" />
<DataGridTextColumn x:Name="value" Width="*" Header="Value" Binding="{Binding Value}" />
</DataGrid.Columns>
</DataGrid>
<StackPanel Orientation="Horizontal" Grid.Row="1">
<TextBox Grid.Row="1" Grid.Column="1" Width="{Binding ElementName =name}" />
<TextBox Grid.Row="1" Grid.Column="3" Width="{Binding ElementName= value}"/>
<Button Content="+" Grid.Column="2" Grid.Row="2" />
<Button Content="-" Grid.Column="3" Grid.Row="2" />
</StackPanel>
</Grid>
Try first of all removing the StackPanel, because, even if you use Grid.Row and Grid.Column properties nothing will effect:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DataGrid ItemsSource="{Binding EnviromentVariables}" IsReadOnly="False" Margin="10" AutoGenerateColumns="False" Grid.ColumnSpan="4">
<DataGrid.Columns>
<DataGridTextColumn x:Name="name" Width="*" Header="Name" Binding="{Binding Name}" />
<DataGridTextColumn x:Name="value" Width="*" Header="Value" Binding="{Binding Value}" />
</DataGrid.Columns>
</DataGrid>
<TextBox Grid.Row="1" Grid.Column="0" Width="{Binding ElementName =name}" Grid.ColumnSpan="2" Margin="10,0,0,0"/>
<TextBox Grid.Row="1" Grid.Column="2" Width="{Binding ElementName= value}" Grid.ColumnSpan="2" Margin="0,0,10,0"/>
<Button Content="+" Grid.Column="2" Grid.Row="2" />
<Button Content="-" Grid.Column="3" Grid.Row="2" />
</Grid>
Remember to adjust margin of Textboxes.
I am trying to collapse expander when when it collapse the other grid columns doesnot take up space.
I tried this in expander_collapsed, the grids are taking the whole space but then I am not able access expander only to make it expand.
private void Expander_Expanded(object sender, RoutedEventArgs e)
{
groupBoxMain.Visibility = Visibility.Visible;
}
private void Expander_Collapsed(object sender, RoutedEventArgs e)
{
MainGrid.ColumnDefinitions[0].Width = new GridLength(1, GridUnitType.Star);
groupBoxMain.Visibility = Visibility.Hidden;
}
xaml code:
<Grid x:Name="MainGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="Auto" Width="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="90*"/>
<ColumnDefinition Width="177*"/>
<ColumnDefinition Width="133*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="Auto" Width="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="8*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="680*"/>
<RowDefinition Height="31*"/>
</Grid.RowDefinitions>
<GroupBox Name="groupBoxMain" BorderBrush="Black" Grid.ColumnSpan="3" Grid.Column="1" Grid.Row="0" Margin="0,0,0,6">
<DataGrid Background="DarkGray" CanUserResizeColumns="True" CanUserSortColumns="True">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding BackupVersionNumber}" Header="File Name" MinWidth="100" FontFamily="Default Font" FontSize="16"/>
<DataGridTextColumn Binding="{Binding BackupDateTime}" Header="Total Time" MinWidth="100" FontFamily="Default Font" FontSize="16"/>
<DataGridTextColumn Binding="{Binding SavedBy}" Header="Total Packet" MinWidth="100" FontFamily="Default Font" FontSize="16"/>
</DataGrid.Columns>
</DataGrid>
</GroupBox>
<Expander ExpandDirection="Right" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Column="0" IsExpanded="True" Width="35" Grid.Row="0" Expanded="Expander_Expanded" Collapsed="Expander_Collapsed">
<Expander.Header>
<TextBlock Text="Select Manifest File" Width="Auto"
FontSize="11">
<TextBlock.LayoutTransform>
<TransformGroup>
<RotateTransform Angle="90"/>
</TransformGroup>
</TextBlock.LayoutTransform>
</TextBlock>
</Expander.Header>
<Grid Background="#FFE5E5E5" Width="0"/>
</Expander>
</Grid>
<Grid HorizontalAlignment="Stretch" Grid.Column="1" Grid.ColumnSpan="2" VerticalAlignment="Stretch" Height="Auto" Width="Auto" Margin="2,0,0,0.2">
<Grid Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="6" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,10.4,0.267,0.2" Background="{DynamicResource {x:Static SystemColors.AppWorkspaceBrushKey}}" />
</Grid>
</Grid>
I have 3 columns in my main grid. One is expander and other 2 are grid.
I want the other two columns to take up the space when expander is collapsed.
First, I think your GroupBox should be in your Expander. You need to fix your grid rows, because those are broken--there is no Grid.Row="3". You need to change the Width to Auto on your Grid in Column 0 in your MainGrid. There are probably other things that need attention, but this should help you get a start on figuring out what you need to do:
<Grid x:Name="MainGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="177*" />
<ColumnDefinition Width="133*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="680*" />
<RowDefinition Height="31*" />
</Grid.RowDefinitions>
<Grid Grid.Column="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="8*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Expander ExpandDirection="Right"
Grid.Column="0"
IsExpanded="True">
<Expander.Header>
<TextBlock Text="Header">
<TextBlock.LayoutTransform>
<RotateTransform Angle="90" />
</TextBlock.LayoutTransform>
</TextBlock>
</Expander.Header>
<GroupBox Name="groupBoxMain"
BorderBrush="Black"
Grid.Row="0"
Margin="0,0,0,6">
<DataGrid Background="DarkGray"
CanUserResizeColumns="True"
CanUserSortColumns="True">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding BackupVersionNumber}"
Header="File Name"
MinWidth="100"
FontFamily="Default Font"
FontSize="16" />
<DataGridTextColumn Binding="{Binding BackupDateTime}"
Header="Total Time"
MinWidth="100"
FontFamily="Default Font"
FontSize="16" />
<DataGridTextColumn Binding="{Binding SavedBy}"
Header="Total Packet"
MinWidth="100"
FontFamily="Default Font"
FontSize="16" />
</DataGrid.Columns>
</DataGrid>
</GroupBox>
</Expander>
</Grid>
<Grid Grid.Column="1"
Grid.ColumnSpan="2"
Height="Auto"
Width="Auto"
Margin="2,0,0,0.2">
<Grid Grid.Row="3"
Grid.Column="0"
Grid.ColumnSpan="6"
Margin="0,10.4,0.267,0.2"
Background="{DynamicResource {x:Static SystemColors.AppWorkspaceBrushKey}}" />
</Grid>
</Grid>
I have a datagrid that is populated by a SQL server. I am trying to select a row from the list of entries and enter them into textboxes outside the grid. I can't seem to find a way to convert the query result into manageable data, other than to populate the grid.
C#
MoviesEntities dataEntities = new MoviesEntities();
ObjectQuery<MovieTable> _movies = dataEntities.MovieTables;
var query =
from MovieTable in _movies
where MovieTable.Title == MMEM.MovieTitle
select MovieTable.id;
string result = Convert.ToString(query); //or something??
XAML:
<Window x:Class="MovieApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Height="650"
Width="525"
ResizeMode="NoResize"
WindowStartupLocation="CenterScreen"
xmlns:vm="clr-namespace:MovieApp">
<Window.Resources>
<vm:MovieAppViewModel x:Key="MovieAppViewModel"/>
</Window.Resources>
<StackPanel x:Name="RootStackPanel" DataContext="{StaticResource MovieAppViewModel}">
<Grid HorizontalAlignment="Center" Height="99" Width="440">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Text="Movie Title" Margin="30 0 30 0" HorizontalAlignment="Center" FontWeight="Heavy"/>
<TextBlock Grid.Column="1" Text="Rating" Margin="30 0 30 0" HorizontalAlignment="Center" FontWeight="Heavy"/>
<TextBlock Grid.Column="2" Text="Stars" Margin="30 0 30 0" HorizontalAlignment="Center" FontWeight="Heavy"/>
<TextBlock Grid.Column="3" Text="Release Year" Margin="30 0 30 0" HorizontalAlignment="Center" FontWeight="Heavy"/>
<TextBox Grid.Column="0" TextAlignment="Center" Text="{Binding MMEM.MovieTitle}" IsEnabled="{Binding MMEM.IsEditable}" Grid.Row="1" MaxLength="50" Width="100"/>
<TextBox Grid.Column="1" TextAlignment="Center" Text="{Binding MMEM.Rating}" IsEnabled="{Binding MMEM.IsEditable}" Grid.Row="1" MaxLength="5" Width="55"/>
<TextBox Grid.Column="2" TextAlignment="Center" Text="{Binding MMEM.Stars}" IsEnabled="{Binding MMEM.IsEditable}" Grid.Row="1" MaxLength="5" Width="20"/>
<TextBox Grid.Column="3" TextAlignment="Center" Text="{Binding MMEM.ReleaseYear}" IsEnabled="{Binding MMEM.IsEditable}" Grid.Row="1" MaxLength="20" Width="100"/>
</Grid>
<Button Content="Edit Mode" Command="{Binding ToggleCommand}"/>
<DataGrid
Width="Auto"
SelectionMode="Extended"
IsReadOnly="True"
AutoGenerateColumns="False"
ItemsSource="{Binding MMLM.MoviesList}"
DataContext="{StaticResource MovieAppViewModel}">
<DataGrid.Columns>
<DataGridTextColumn Width="200" Header="Title" Binding="{Binding Title}"/>
<DataGridTextColumn Width="100" Header="Rating" Binding="{Binding Rating}"/>
<DataGridTextColumn Width="100" Header="Stars" Binding="{Binding Stars}"/>
<DataGridTextColumn Width="93" Header="Release Year" Binding="{Binding ReleaseYear}"/>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
I am under the assumption that there is a more efficient way than parsing the row into strings and populating the textboxes, but at this point I'll take what I can get!
Thanks for any input!!!!
Just name thedatagrid eg masterDG
Then in the detail
Binding ElementName=masterDG, path=SelecedItem.Title