Pass a SQL query result into multiple textboxes - c#

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

Related

Grid in Grid not showing Scrollbar in C# WPF

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.

C# - Listview Fullrowselect on UWP

I want do some things with my listview on my UWP project:
When a item of the listview is selected I want the entire row selected, also the subitems (cells) on the row should be not selectable.
I was trying with Fullrowselect but seem is not longer availiable in uwp.
my current xaml is:
<ListView x:Name="ListView1" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,164,0,0" BorderBrush="Black" BorderThickness="1" Background="WhiteSmoke" Visibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible" >
<ListView.ItemTemplate>
<DataTemplate>
<Grid Padding="0" Margin="0" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="130" />
<ColumnDefinition Width="300" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<TextBox x:Name="TextBox_Item" Grid.Column="0" Text="{Binding Path=Item}" TextWrapping="Wrap" BorderBrush="Black" BorderThickness="1.5" IsReadOnly="True" TextAlignment="Left"/>
<TextBox x:Name="TextBox_Name" Grid.Column="1" Text="{Binding Path=Name}" TextWrapping="Wrap" BorderThickness="1.5" BorderBrush="Black" IsReadOnly="True" TextAlignment="Left"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Any help is appreciated.
There's a property called "IsHitTestVisible, try changing it to false on your textboxes.
<ListView x:Name="ListView1" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,164,0,0" BorderBrush="Black" BorderThickness="1" Background="WhiteSmoke" Visibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible" >
<ListView.ItemTemplate>
<DataTemplate>
<Grid Padding="0" Margin="0" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="130" />
<ColumnDefinition Width="300" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<TextBox x:Name="TextBox_Item" Grid.Column="0" Text="{Binding Path=Item}" TextWrapping="Wrap" BorderBrush="Black" BorderThickness="1.5" IsReadOnly="True" TextAlignment="Left" IsHitTestVisible="False"/>
<TextBox x:Name="TextBox_Name" Grid.Column="1" Text="{Binding Path=Name}" TextWrapping="Wrap" BorderThickness="1.5" BorderBrush="Black" IsReadOnly="True" TextAlignment="Left" IsHitTestVisible="False"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

ContentControl inside TabItem doesn't resize proberly

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.

How to display textbox data in wpf datagrid using MVVM model?

Hi everyone my question is that i am working on wpf c# application which is based on MVVM pattern.The problem where i stuck is that i want my textboxes data to display in my datagrid by clicking a button but i am unable to did that and finally asking for some good suggestions and solutions.
My code in XAML:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label
Grid.Row="0"
Grid.Column="0"
VerticalAlignment="Center"
Content="VLAN NAME"
Foreground="Black"
Opacity="0.8"
/>
<TextBox
Grid.Row="0"
Grid.Column="1"
Margin="0,5,0,0"
Height="25"
Foreground="Black"
Opacity="0.8"
Width="Auto"
Text="{Binding VlanName, UpdateSourceTrigger=PropertyChanged}"
/>
<Label
Grid.Row="1"
Grid.Column="0"
VerticalAlignment="Center"
Content="VLAN ID"
Foreground="Black"
Opacity="0.8"
/>
<TextBox
Grid.Row="1"
Grid.Column="1"
Margin="0,5,0,0"
Height="25"
Foreground="Black"
Opacity="0.8"
Width="70"
HorizontalAlignment="Left"
Text="{Binding VlanID, UpdateSourceTrigger=PropertyChanged}"
/>
<Label
Grid.Row="2"
Grid.Column="0"
VerticalAlignment="Center"
Content="IP FOR VLAN"
Foreground="Black"
Opacity="0.8"
/>
<Grid
Grid.Column="1"
Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="40"/>
</Grid.ColumnDefinitions>
<TextBox
Grid.Row="2"
Grid.Column="0"
Margin="5"
VerticalAlignment="Center"
MaxLength="3"
Width="30"
Foreground="Black"
Opacity="0.8"
Text="{Binding VlanIP1, UpdateSourceTrigger=PropertyChanged}"
/>
<TextBox
Grid.Row="2"
Grid.Column="1"
Margin="5"
VerticalAlignment="Center"
MaxLength="3"
Width="30"
Foreground="Black"
Opacity="0.8"
Text="{Binding VlanIP2, UpdateSourceTrigger=PropertyChanged}"
/>
<TextBox
Grid.Row="2"
Grid.Column="2"
Margin="5"
VerticalAlignment="Center"
MaxLength="3"
Width="30"
Foreground="Black"
Opacity="0.8"
Text="{Binding VlanIP3, UpdateSourceTrigger=PropertyChanged}"
/>
<TextBox
Grid.Row="2"
Grid.Column="3"
Margin="5"
VerticalAlignment="Center"
MaxLength="3"
Width="30"
Foreground="Black"
Opacity="0.8"
Text="{Binding VlanIP4, UpdateSourceTrigger=PropertyChanged}"
/>
</Grid>
<Label
Grid.Row="3"
Grid.Column="0"
VerticalAlignment="Center"
Content="VLAN PORT"
Foreground="Black"
Opacity="0.8"
/>
<ComboBox
Grid.Row="3"
Grid.Column="1"
ItemsSource="{Binding AvailableVlanPorts}"
SelectedItem="{Binding SelectedVlanPort}"
>
</ComboBox>
<Button Grid.Column="3"
Grid.Row="1"
Content="Add VLAN"
Margin="10,5,0,0"
Style="{StaticResource AppButtons}"
Command="{Binding AddVlan}"
Click="Add_Vlan"
/>
<Button Grid.Column="3"
Grid.Row="2"
Content="Remove VLAN"
Margin="10,5,0,0"
Style="{StaticResource AppButtons}"
Width="100"
Command="{Binding RemoveVlan}"
/>
<DataGrid Grid.Row="4"
Grid.ColumnSpan="3"
Height="200"
Margin="10,10,0,0"
Name="dg"
ItemsSource="{Binding vlan}"
AutoGenerateColumns="False"
>
<DataGrid.Columns>
<DataGridTextColumn Header="S.No" Binding="{Binding Path=S_No}"/>
<DataGridTextColumn Header="VLAN Name" Binding="{Binding Path=vname}" />
<DataGridTextColumn Header="VLAN ID" Binding="{Binding Path=vid}" />
<DataGridTextColumn Header=" IP" Width="100"/>
<DataGridTextColumn Header="VLAN Ports" Width="100"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
C# code is:
public string VlanName
{
get
{
return this.ConfigurationLibrary.ConfigLibraryVlanName;
}
set
{
if (String.Equals(this.ConfigurationLibrary.ConfigLibraryVlanName, value))
{
return;
}
this.ConfigurationLibrary.ConfigLibraryVlanName = value;
this.OnPropertyChanged("VlanName");
}
}
public string VlanID
{
get
{
return this.ConfigurationLibrary.ConfigLibraryVlanName;
}
set
{
if (String.Equals(this.ConfigurationLibrary.ConfigLibraryVlanName, value))
{
return;
}
this.ConfigurationLibrary.ConfigLibraryVlanName = value;
this.OnPropertyChanged("VlanID");
}
}
public ICommand AddVlan
{
get
{
if (_addVlan == null)
_addVlan = new RelayCommand(() => this.AddVlans());
return _addVlan;
}
}
void AddVlans()
{
Console.Write("Add vlan");
var serial = new VLANSPropertyClass();
serial.S_No = vlan.Count + 1;
serial.vname = VlanName;
serial.vid = VlanID;
vlan.Add(serial);
}
Firstly i am displaying only two columns for testing.
Any help would be greatly appreciable.
In order to do what you want, the DataGrid should be bound to the collection (e.g. ObservableCollection) where you store all the items, when you click the "Add" button, you call Entities.Add(NewEntity);, where Entities is the collection and NewEntity is a property of the view model. You bind the text boxes to the properties of NewEntity. i've made a small gist here to demonstrate.

Xaml : Nested properties are not supported: DataGridTemplateColumn.CellTemplate.

I am Silverlight5 beginner and i have an error which is as below (which are for same line which i pointed in xaml code , where as i have properly referenced assembmy xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"):
Error Nested properties are not supported: DataGridTemplateColumn.CellTemplate.
Error The attachable property 'CellTemplate' was not found in type 'DataGridTemplateColumn'.
Error Unexpected PROPERTYELEMENT in parse rule PropertyElement ::= . PROPERTYELEMENT Content? ENDTAG..
And cod to do so is as below:
<data:DataGrid Grid.Row="1" x:Name="gridVersions" ItemsSource="{Binding ProgramVersion }" IsReadOnly="True" AutoGenerateColumns="False">
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="Version" Binding="{Binding Version}" Width="2*"></data:DataGridTextColumn>
<data:DataGridTextColumn Header="Live" Binding="{Binding Live}" Width="2*"></data:DataGridTextColumn>
<data:DataGridTextColumn Header="Date modif." Binding="{Binding ModifDate}" Width="3*"></data:DataGridTextColumn>
<data:DataGridTextColumn Header="..." Binding="{Binding Path=More}" Width="*"></data:DataGridTextColumn>
**<data:DataGridTemplateColumn.CellTemplate>** //Error prone line
<DataTemplate>
<Button>More Info
<ToolTipService.ToolTip>
<Border Background="White">
<Grid Width="Auto">
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="Name" />
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Name}"/>
<TextBlock Grid.Row="1" Grid.Column="0" Text="Author" />
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Author}"/>
<TextBlock Grid.Row="2" Grid.Column="0" Text="Date of Creation" />
<TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding DateCreation}"/>
<TextBlock Grid.Row="3" Grid.Column="0" Text="Company" />
<TextBlock Grid.Row="3" Grid.Column="1" Text="{Binding Company}"/>
<TextBlock Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="1" Text="Description"/>
<TextBox Grid.Row="5" Grid.Column="0" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch" Grid.ColumnSpan="2" Text="{Binding Description}"/>
<TextBlock Grid.Row="6" Grid.Column="0" Text="DocUrl" />
<TextBlock Grid.Row="6" Grid.Column="1" Text="{Binding DocUrl}"/>
<TextBlock Grid.Row="7" Grid.Column="0" Text="ProgramId" />
<TextBlock Grid.Row="7" Grid.Column="1" Text="{Binding ProgramId}"/>
</Grid>
</Border>
</ToolTipService.ToolTip>
</Button>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGrid.Columns>
</data:DataGrid>
</Grid>
You need to define an actual DataGridTemplateColumn element first:
<data:DataGrid.Columns>
<!-- Columns ... -->
<data:DataGridTemplateColumn>
<data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
</data:DataGrid.Columns>
Note that in XAML syntax, when you have an element and sub-element like this:
<a>
<a.b>
content
</a.b>
</a>
Then this means to set the property b of a to "content". If the elements are of this form:
<a>
<b.c>
</b.c>
</a>
That can be legal also, but it means to set the attached dependency property c of the class b on the object a. That's why you're seeing the error message:
The attachable property 'CellTemplate' was not found in type 'DataGridTemplateColumn'
The XAML parser thinks you're trying to attach a property DataGridTemplateColumn.CellTemplate ("b.c") to the element DataGrid.Columns ("a").

Categories

Resources