How to Stop WPF TextBox from growing - c#

WPF TextBox Control grows when a long text has been typed in.
This question has been already raised in Stackoverflow
and I refereed few answers also, but still I didn't find the proper answer which is working.
Here same problem has been mentioned but no working solution for this kind of issue.
I have spend enough time to solve this, please help me to solve this issue.
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<DockPanel LastChildFill="False" Grid.IsSharedSizeScope="True">
<GroupBox DockPanel.Dock="Top" DataContext="{Binding Data, ValidatesOnNotifyDataErrors=False}">
<GroupBox.HeaderTemplate>
<DataTemplate>
<TextBlock shell:Localization.VbeiTag="vbeiGeneralInformation" Text="General Information"/>
</DataTemplate>
</GroupBox.HeaderTemplate>
<Grid DockPanel.Dock="Top" Margin="0,0,0,3">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="GroupA" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="Auto" SharedSizeGroup="GroupB" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<!--Code Prefix Label -->
<Label Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" Content="Code" ></Label>
<Label Grid.Row="0" Grid.Column="2" HorizontalAlignment="Left" Content="Name" ></Label>
<Label Grid.Row="1" Grid.Column="0" HorizontalAlignment="Left" Content="Title" ></Label>
<Label Grid.Row="2" Grid.Column="0" HorizontalAlignment="Left" Content="Long Reference" ></Label>
<Label Grid.Row="3" Grid.Column="0" HorizontalAlignment="Left" Content="Client Reference"></Label>
<Label Grid.Row="3" Grid.Column="2" HorizontalAlignment="Left" Content="Lead Sub-Business Unit" ></Label>
<!--Code Prefix Editor-->
<common:StudyCodeEditor Grid.Row="0" Grid.Column="1" DataContext="{Binding ValidatesOnNotifyDataErrors=False}"
SuggestedStudyCodePrefixRange="{ViewModelBinding SuggestedStudyCodePrefixRange}"/>
<!--Study Name-->
<TextBox Grid.Row="0" Grid.Column="3" Margin="3" VerticalAlignment="Top"
Text="{Binding StudyName, UpdateSourceTrigger=PropertyChanged}" />
<!--Study Title -->
<TextBox Grid.Row="1" Grid.Column="1" Margin="3" Grid.ColumnSpan="3" VerticalAlignment="Top"
Text="{Binding StudyOfficialTitle, UpdateSourceTrigger=PropertyChanged}" />
<!--Client Long Reference -->
<TextBox Grid.Row="2" Grid.Column="1" Margin="3" Grid.ColumnSpan="3" VerticalAlignment="Top"
Text="{Binding StudyClientLongReference, UpdateSourceTrigger=PropertyChanged}" />
<!--Client Study Reference-->
<TextBox Grid.Row="3" Grid.Column="1" Margin="3" VerticalAlignment="Top"
Text="{Binding ClientStudyReference, UpdateSourceTrigger=PropertyChanged}" />
<!-- Description -->
<TextBox Grid.Row="7" Grid.Column="1" Grid.ColumnSpan="3"
Text="{Binding Description}" AcceptsReturn="True" DockPanel.Dock="Top"
TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" Margin="3" Height="62"/>
</Grid>
</GroupBox>
</DockPanel>
</ScrollViewer>

MaxWidth should do the trick. If not, what went wrong in your case?

Related

C# WPF Scrollbar changes/moves content

I'm having a hard time making my WPF form scrollable. The form is quite long, and i want the user to be able to scroll further down. If i adjust the height of the form, as the scrollbar becomes active, it moves everything around, and makes my content smaller. How do i avoid this? I've added 2 Pictures (Before & After) and my XAML code.
<Window x:Class="RegisterForm.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1000" Width="700" MinHeight="1000" MinWidth="700">
<ScrollViewer
VerticalScrollBarVisibility="Visible"
CanContentScroll="True">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label
Content="Full Name"
FontSize="14"
FontWeight="Bold"
Grid.Row="1"
Grid.ColumnSpan="3"
Margin="40,0,40,0"
HorizontalContentAlignment="Left">
</Label>
<Label
Content="Gender"
FontSize="14"
FontWeight="Bold"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="40,0,40,0"
HorizontalContentAlignment="Left">
</Label>
<Label
Content="Date of Birth"
FontSize="14"
FontWeight="Bold"
Grid.Row="3"
Grid.ColumnSpan="3"
Margin="40,0,40,0"
HorizontalContentAlignment="Left">
</Label>
<Label
Content="Address"
FontSize="14"
FontWeight="Bold"
Grid.Row="4"
Grid.ColumnSpan="3"
Margin="40,0,40,0"
HorizontalContentAlignment="Left">
</Label>
<Label
Content="Phone Number"
FontSize="14"
FontWeight="Bold"
Grid.Row="7"
Grid.ColumnSpan="3"
Margin="40,0,40,0"
HorizontalContentAlignment="Left">
</Label>
<Label
Content="Phone Number Family"
FontSize="14"
FontWeight="Bold"
Grid.Row="8"
Grid.ColumnSpan="3"
Margin="40,0,0,0"
HorizontalContentAlignment="Left">
</Label>
<Label
Content="E-mail"
FontSize="14"
FontWeight="Bold"
Grid.Row="9"
Grid.ColumnSpan="3"
Margin="40,0,40,0"
HorizontalContentAlignment="Left">
</Label>
<Label
Content="Do you speak english?"
FontSize="14"
FontWeight="Bold"
Grid.Row="10"
Grid.ColumnSpan="3"
Margin="40,0,0,0"
HorizontalContentAlignment="Left">
</Label>
<Label
Content="Education"
FontSize="14"
FontWeight="Bold"
Grid.Row="11"
Grid.ColumnSpan="3"
Margin="40,0,40,0"
HorizontalContentAlignment="Left">
</Label>
<Label
Content="Job Type/Profession"
FontSize="14"
FontWeight="Bold"
Grid.Row="12"
Grid.ColumnSpan="3"
Margin="40,0,0,0"
HorizontalContentAlignment="Left">
</Label>
<Label
Content="Experience"
FontSize="14"
FontWeight="Bold"
Grid.Row="13"
Grid.ColumnSpan="3"
Margin="40,0,40,0"
HorizontalContentAlignment="Left">
</Label>
<Label
Content="Drivers License"
FontSize="14"
FontWeight="Bold"
Grid.Row="14"
Grid.ColumnSpan="3"
Margin="40,0,40,0"
HorizontalContentAlignment="Left">
</Label>
<TextBox
x:Name="txtFirstName"
FontSize="14"
Grid.Row="1"
Grid.Column="3"
Grid.ColumnSpan="2"
Margin="10,0,0,30"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
</TextBox>
<Label
Content="First Name"
FontSize="12"
Grid.Row="1"
Grid.Column="3"
Grid.ColumnSpan="2"
Margin="10,25,0,0"
FontStyle="Italic"
HorizontalContentAlignment="Left"
HorizontalAlignment="Left">
</Label>
<TextBox
x:Name="txtLastName"
FontSize="14"
Grid.Row="1"
Grid.Column="5"
Grid.ColumnSpan="3"
Margin="10,0,0,30"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
</TextBox>
<Label
Content="Last Name"
FontSize="12"
Grid.Row="1"
Grid.Column="5"
Grid.ColumnSpan="2"
Margin="10,25,0,0"
FontStyle="Italic"
HorizontalContentAlignment="Left"
HorizontalAlignment="Left">
</Label>
<ComboBox
x:Name="CBGender"
FontSize="12"
Grid.Row="2"
Grid.Column="3"
Grid.ColumnSpan="2"
Margin="10,0,0,30"
IsEditable="True"
IsReadOnly="True"
Text="Please Select"
FontStyle="Normal">
<ListBoxItem Content="Male" FontStyle="Normal"/>
<ListBoxItem Content="Female" FontStyle="Normal"/>
</ComboBox>
<ComboBox
x:Name="CBDOBDay"
FontSize="12"
Grid.Row="3"
Grid.Column="3"
Grid.ColumnSpan="1"
Margin="10,0,0,30"
IsEditable="True"
IsReadOnly="True"
Text="DD"
FontStyle="Normal">
</ComboBox>
<ComboBox
x:Name="CBDOBMonth"
FontSize="12"
Grid.Row="3"
Grid.Column="4"
Grid.ColumnSpan="1"
Margin="10,0,0,30"
IsEditable="True"
IsReadOnly="True"
Text="MM"
FontStyle="Normal">
</ComboBox>
<ComboBox
x:Name="CBDOBYear"
FontSize="12"
Grid.Row="3"
Grid.Column="5"
Grid.ColumnSpan="1"
Margin="10,0,0,30"
IsEditable="True"
IsReadOnly="True"
Text="YY"
FontStyle="Normal">
</ComboBox>
<TextBox
x:Name="txtStreetAddress"
FontSize="14"
Grid.Row="4"
Grid.Column="3"
Grid.ColumnSpan="3"
Margin="10,0,0,30"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
</TextBox>
<Label
Content="Street Address"
FontSize="12"
Grid.Row="4"
Grid.Column="3"
Grid.ColumnSpan="2"
Margin="10,25,0,0"
FontStyle="Italic"
HorizontalContentAlignment="Left"
HorizontalAlignment="Left">
</Label>
<TextBox
x:Name="txtCity"
FontSize="14"
Grid.Row="5"
Grid.Column="3"
Grid.ColumnSpan="2"
Margin="10,0,0,30"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
</TextBox>
<Label
Content="City"
FontSize="12"
Grid.Row="5"
Grid.Column="3"
Grid.ColumnSpan="2"
Margin="10,25,0,0"
FontStyle="Italic"
HorizontalContentAlignment="Left"
HorizontalAlignment="Left">
</Label>
<TextBox
x:Name="txtStateProvince"
FontSize="14"
Grid.Row="5"
Grid.Column="5"
Grid.ColumnSpan="3"
Margin="10,0,0,30"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
</TextBox>
<Label
Content="State / province"
FontSize="12"
Grid.Row="5"
Grid.Column="5"
Grid.ColumnSpan="2"
Margin="10,25,0,0"
FontStyle="Italic"
HorizontalContentAlignment="Left"
HorizontalAlignment="Left">
</Label>
<TextBox
x:Name="txtPostalZipCode"
FontSize="14"
Grid.Row="6"
Grid.Column="3"
Grid.ColumnSpan="2"
Margin="10,0,0,30"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
</TextBox>
<Label
Content="Postal / Zip code"
FontSize="12"
Grid.Row="6"
Grid.Column="3"
Grid.ColumnSpan="2"
Margin="10,25,0,0"
FontStyle="Italic"
HorizontalContentAlignment="Left"
HorizontalAlignment="Left">
</Label>
<ComboBox
x:Name="CBCountry"
FontSize="12"
Grid.Row="6"
Grid.Column="5"
Grid.ColumnSpan="3"
Margin="10,0,0,30"
IsEditable="True"
IsReadOnly="True"
Text="Please Select"
FontStyle="Normal" SelectionChanged="CBCountry_SelectionChanged">
</ComboBox>
<Label
Content="Country"
FontSize="12"
Grid.Row="6"
Grid.Column="5"
Grid.ColumnSpan="2"
Margin="10,25,0,0"
FontStyle="Italic"
HorizontalContentAlignment="Left"
HorizontalAlignment="Left">
</Label>
<TextBox
x:Name="txtPhone1AreaCode"
FontSize="14"
Grid.Row="7"
Grid.Column="3"
Grid.ColumnSpan="2"
Margin="10,0,0,30"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
</TextBox>
<TextBox
x:Name="txtPhone1"
FontSize="14"
Grid.Row="7"
Grid.Column="5"
Grid.ColumnSpan="3"
Margin="10,0,0,30"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
</TextBox>
<Label
Content="Area Code (ex. +45)"
FontSize="12"
Grid.Row="7"
Grid.Column="3"
Grid.ColumnSpan="2"
Margin="10,25,0,0"
FontStyle="Italic"
HorizontalContentAlignment="Left"
HorizontalAlignment="Left">
</Label>
<Label
Content="Phone Number"
FontSize="12"
Grid.Row="7"
Grid.Column="5"
Grid.ColumnSpan="2"
Margin="10,25,0,0"
FontStyle="Italic"
HorizontalContentAlignment="Left"
HorizontalAlignment="Left">
</Label>
<TextBox
x:Name="txtPhone2AreaCode"
FontSize="14"
Grid.Row="8"
Grid.Column="3"
Grid.ColumnSpan="2"
Margin="10,0,0,30"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
</TextBox>
<TextBox
x:Name="txtPhone2"
FontSize="14"
Grid.Row="8"
Grid.Column="5"
Grid.ColumnSpan="3"
Margin="10,0,0,30"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
</TextBox>
<Label
Content="Area Code (ex. +45)"
FontSize="12"
Grid.Row="8"
Grid.Column="3"
Grid.ColumnSpan="2"
Margin="10,25,0,0"
FontStyle="Italic"
HorizontalContentAlignment="Left"
HorizontalAlignment="Left">
</Label>
<Label
Content="Phone Number"
FontSize="12"
Grid.Row="8"
Grid.Column="5"
Grid.ColumnSpan="2"
Margin="10,25,0,0"
FontStyle="Italic"
HorizontalContentAlignment="Left"
HorizontalAlignment="Left">
</Label>
<TextBox
x:Name="txtEmail"
FontSize="14"
Grid.Row="9"
Grid.Column="3"
Grid.ColumnSpan="4"
Margin="10,0,0,30"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
</TextBox>
<Label
Content="Ex. myname#example.com"
FontSize="12"
Grid.Row="9"
Grid.Column="3"
Grid.ColumnSpan="3"
Margin="10,25,0,0"
FontStyle="Italic"
HorizontalContentAlignment="Left"
HorizontalAlignment="Left">
</Label>
</Grid>
</ScrollViewer>
//Thanks, Rasmus
The problem with ScrollViewer is that it is virtually infinite. So, the content inside it can't calculate relative sizes such as *. In your case, i think if you add a height to your grid, something like <Grid Height="1000"> it will work fine.
You have to specify the Heigh or MinHeigh of at least one part: The Editors (Labels / Textboxes / Comboboxes) or the GridRows or the whole Grid. Currrently the Grid is filled within the Window and the Columns also fill all together the whole Grid. Normally you would Set Grids' Rows Height to
<RowDefinition Height="auto" />
instead of
<RowDefinition Height="*" />
and give the editors some kind of MinHeight or Padding.
So there are many Solutions. It just depends on how you want to have the Labels/Editors Height. (Dynamic or fixed)
EDIT/ADD:
And just as a SideNote, if you define the same style foreach of those Row-/ and ColumnDefinitions, you maybe should assign a Style in the Grid for those.
It makes things easier to Change at once ;D
<Grid>
<Grid.Resources>
<Style TargetType="RowDefinition">
<Setter Property="Height" Value="auto" />
</Style>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<!-- ... -->
</Grid>

Divide first row in the grid to four equal parts WPF

I used to draw something like tabcontrol in WizardPage control of extended WPF toolkit.
Relevant xaml code:
<xctk:WizardPage x:Name="Page1" PageType="Blank" Width="540"
BorderBrush="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="200"/>
</Grid.RowDefinitions>
<DockPanel Grid.Row="0">
<TextBox Width="135" Text="Step 1" Background="#FF2BADDE" FontSize="16" TextAlignment="Center" />
<TextBox Width="135" Text="Step 2" Background="#FF777A7C" FontSize="16" TextAlignment="Center" />
<TextBox Width="135" Text="Step 3" Background="#FF777A7C" FontSize="16" TextAlignment="Center" />
<TextBox Width="135" Text="Step 4" Background="#FF777A7C" FontSize="16" TextAlignment="Center" />
</DockPanel>
<Grid Grid.Row="1">
<GroupBox Header="Group 1" FontSize="16" Height="80" Margin="0,0,0,90" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="270"/>
<ColumnDefinition Width="270"/>
</Grid.ColumnDefinitions>
<RadioButton x:Name="RadioButNew" Content="New" FontSize="13.333" BorderThickness="0,1,1,1" HorizontalAlignment="Left" Margin="30,30,0,0"/>
<RadioButton x:Name="RadioButUpdate" Content="Update" Grid.Column="1" FontSize="13.333" Focusable="False" HorizontalAlignment="Left" Margin="30,30,0,0"/>
</Grid>
</GroupBox>
</Grid>
But text boxes inside grid aren't at the same width though I set each of them to same width which is the total WizardPage width/4 (since I have four text boxes).
Any solution please?
Thanks!
To equally divide in 4 columns , you should use a grid, instead of a DockPanel. Although it is unnecessary to precide the Width. Just indicate that you have 4 columns :
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="200"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Text="Step 1" Background="#FF2BADDE" FontSize="16" TextAlignment="Center" />
<TextBox Grid.Column="1" Text="Step 2" Background="#FF777A7C" FontSize="16" TextAlignment="Center" />
<TextBox Grid.Column="2" Text="Step 3" Background="#FF777A7C" FontSize="16" TextAlignment="Center" />
<TextBox Grid.Column="3" Text="Step 4" Background="#FF777A7C" FontSize="16" TextAlignment="Center" />
</Grid>
<Grid Grid.Row="1">
<GroupBox Header="Group 1" FontSize="16" Height="80" Margin="0,0,0,90" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="270"/>
<ColumnDefinition Width="270"/>
</Grid.ColumnDefinitions>
<RadioButton x:Name="RadioButNew" Content="New" FontSize="13.333" BorderThickness="0,1,1,1" HorizontalAlignment="Left" Margin="30,30,0,0"/>
<RadioButton x:Name="RadioButUpdate" Content="Update" Grid.Column="1" FontSize="13.333" Focusable="False" HorizontalAlignment="Left" Margin="30,30,0,0"/>
</Grid>
</GroupBox>
</Grid>
</Grid>
you can also simplify you xaml by using only 1 grid. Grid.ColumnSpan is handy is such a case :
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="80"/>
<RowDefinition Height="200"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBox Grid.Row="0" Grid.Column="0" Text="Step 1" Background="#FF2BADDE" FontSize="16" TextAlignment="Center" />
<TextBox Grid.Row="0" Grid.Column="1" Text="Step 2" Background="#FF777A7C" FontSize="16" TextAlignment="Center" />
<TextBox Grid.Row="0" Grid.Column="2" Text="Step 3" Background="#FF777A7C" FontSize="16" TextAlignment="Center" />
<TextBox Grid.Row="0" Grid.Column="3" Text="Step 4" Background="#FF777A7C" FontSize="16" TextAlignment="Center" />
<GroupBox Header="Group 1" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="4" FontSize="16" Margin="0,0,0,90" />
<RadioButton Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" x:Name="RadioButNew" Content="New" FontSize="13.333" BorderThickness="0,1,1,1" HorizontalAlignment="Center" Margin="30,30,0,0"/>
<RadioButton Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="2" x:Name="RadioButUpdate" Content="Update" FontSize="13.333" Focusable="False" HorizontalAlignment="Center" Margin="30,30,0,0"/>
</Grid>

WPF fixed rows are resizing

I'm new in WPF and I try to creat specific UserControl to display data for a single product. I used Grid inside UserControl. So I create 5 columns and 3 rows. I want ot make 4 columns fixed (image, green-clored, blue-colored and column with controls) and last column (orange-colored) to fill all availabel space. Here my XAML and few screenshots:
<Grid Margin="0,0,0,5" Background="#FFDCD9D9" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80" />
<ColumnDefinition Width="70" />
<ColumnDefinition Width="70" />
<ColumnDefinition Width="70" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Image Grid.Column="0" Grid.Row="0" Grid.RowSpan="4" Source="{Binding ItemThumbnailUrl}" Stretch="None" HorizontalAlignment="Right" Margin="5,0" />
<StackPanel Grid.Column="1" Grid.Row="0" Grid.ColumnSpan="4" Background="#FFDA6F6F">
<Label BorderThickness="0" Content="dsgsdgsgsgsdgsdg sd " FontSize="13.333" FontWeight="Bold" HorizontalAlignment="Left" />
</StackPanel>
<StackPanel Grid.Column="1" Grid.Row="1" Orientation="Horizontal" Background="#FF517823" HorizontalAlignment="Left" Width="70">
<Label Content="{Binding ItemPrice}" HorizontalAlignment="Left" FontSize="9.333" Width="45" />
<Label Content="грн." HorizontalAlignment="Left" FontSize="9.333" Width="25"/>
</StackPanel>
<StackPanel Grid.Column="2" Grid.Row="1" Orientation="Horizontal" Background="#FF214299" HorizontalAlignment="Left" Width="70">
<Label Content="{Binding Quantity}" HorizontalAlignment="Left" FontSize="9.333" Width="45" />
<Label Content="шт." HorizontalAlignment="Left" FontSize="9.333" Width="25"/>
</StackPanel>
<StackPanel Grid.Column="1" Grid.Row="2" Orientation="Horizontal" Background="#FF88B91E" HorizontalAlignment="Left" Width="70">
<Label Content="1С" HorizontalAlignment="Right" FontSize="9.333" Foreground="#FF8B8888" Width="45"/>
<Label Content="грн." HorizontalAlignment="Right" FontSize="9.333" Foreground="#FF8B8888" Width="25"/>
</StackPanel>
<StackPanel Grid.Column="2" Grid.Row="2" Orientation="Horizontal" Background="#FF228CBD" HorizontalAlignment="Left" Width="70">
<Label Content="1С" HorizontalAlignment="Right" FontSize="9.333" Foreground="#FF8B8888" Width="45"/>
<Label Content="шт." HorizontalAlignment="Right" FontSize="9.333" Foreground="#FF8B8888" Width="25"/>
</StackPanel>
<CheckBox Grid.Column="3" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<Button Grid.Column="3" Grid.Row="2" Background="{x:Null}" Content="Редакт." Foreground="#FF444343" Width="50" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Label Grid.Column="4" Grid.RowSpan="2" Grid.Row="1" Background="#FFE08212" HorizontalContentAlignment="Stretch" />
</Grid>
If I have "Title" text (in red-colored cell) less than sum of 3 my fixed columns, everything is OK, but if a text larger I have problems with some paddings between columns (please see pictures)
So how can I resolve this problem?
I could reproduce your issue in a variety of cases when a Grid is used in a DataTemplate. I removed the StackPanel and used a TextBlock, then a a TextBlock hosted in a separate Grid, but all with the same result. I guess something is going wrong when WPF is determining the required size. I have often occurred this kind of strange behaviour in Grids (when part of an ItemTemplate). If you need a quick workaround then this should do the trick
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="80" MaxWidth="80" />
<ColumnDefinition MinWidth="70" MaxWidth="70" />
<ColumnDefinition MinWidth="70" MaxWidth="70" />
<ColumnDefinition MinWidth="70" MaxWidth="70" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

WPF is not setting the values to the EF object that is set as Datacontext

I have observation/issue/problem.
In my app I have loaded a list of EF objects (Let's call them "Car") that I am setting as the "DataContext" to an "ItemsControl". The ItemsControl has a bunch of controls that represent the Properties of the Object ("Car") like textboxes and similar....
As far as I understood it, If I assign a Property to a control it should be binded, If I change the value in the TextBox it should also be changed in the Object. But this is not happening in my app. Am I missing something?
As far as I know, for the other way around, the property's class needs to implement the Interface "INotifyPropertyChanged" when the change happens from Object->GUI, but nothing like this needs to be done when there is a change from GUI-Object.
Am I missing something, Am I missinformed, or just writing buggy code?
EDITED - 1st time - thnx for the suggestion
<ItemsControl Name="icRoles" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" VerticalAlignment="Stretch" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Name="grdRoles" Background="Gray" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<Grid.RowDefinitions>
<RowDefinition Height="80" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="40" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="1.4*"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<!--Subject-->
<Label Content="Uloga" Grid.ColumnSpan="2" Height="28" HorizontalAlignment="Center" Name="label8" VerticalAlignment="Center" />
<Label Content="ID:" Grid.Row="1" Height="28" Name="label1" VerticalAlignment="Top" />
<Label Content="Ime Uloge:" Grid.Row="2" Height="28" Name="label2" VerticalAlignment="Top" />
<Label Content="Prikazano ime:" Grid.Row="3" Height="28" Name="label3" VerticalAlignment="Top" />
<Label Content="Tip Uloge:" Grid.Row="4" Height="28" Name="label4" VerticalAlignment="Top" />
<Label Content="Datum Stvaranja:" Grid.Row="5" Height="28" Name="label5" VerticalAlignment="Top" />
<Label Content="Datum promjene:" Grid.Row="6" Height="28" Name="label6" VerticalAlignment="Top" />
<Label Content="Stanje:" Grid.Row="7" Height="28" Name="label7" VerticalAlignment="Top" />
<TextBox Grid.Column="1" Grid.Row="1" Height="23" Text="{Binding ID}" IsEnabled="False" />
<TextBox Grid.Column="1" Grid.Row="2" Height="23" Text="{Binding Name}" />
<TextBox Grid.Column="1" Grid.Row="3" Height="23" Text="{Binding Text}" />
<WrapPanel Grid.Column="1" Grid.Row="4" Height="25" />
<DatePicker Grid.Column="1" Grid.Row="5" Height="25" SelectedDate="{Binding DateCreated}" IsEnabled="False" />
<DatePicker Grid.Column="1" Grid.Row="6" Height="25" SelectedDate="{Binding DateModified}" IsEnabled="False" />
<CheckBox Grid.Column="1" Grid.Row="7" Height="16" IsChecked="{Binding active}" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
EDITED - 2nd Time
I have now set the Mode of the binding to the controls as they are supossed to be. But one strange thing is happening. I have set the Binding of two textboxes to
Text="{Binding Path=Name, Mode=TwoWay}"
Text="{Binding Path=Text, Mode=TwoWay}"
But only the first one is being set to the propery. The fist one stays null.+
OK. Solved it from suggestion from User that helped
I was missing the binding mode. (Text="{Binding Path=Name, Mode=TwoWay}")
the second issue is because the control did not save the data into the object because it still had focus. The saving was not commited into the object

Stay on the same textbox control inside a listbox whilst using keyboard navigation

I'm very new to WPF so go easy
I have a bound list box which is bounce to a datasource.
I have created a data template for the items in the list box and some of the columns of data need to be editable. to make them editable I have created a bound textbox in a data template in a resource file.
My question is. Once I am in the textbox and have edited the data. How do I then use the keyboard to arrow down to the next line and have it automatically give focus to the same textbox on the next line down.
I have been searching for 2 days now but no solution seems to fit my problem
Template in resource file;
<DataTemplate x:Key="myTemplate" x:Name="dTemplate">
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Path=b2b_StockCode}" Control.Template="{StaticResource lvTemplate}" Width="80"/>
<Label Content="{Binding Path=b2b_PartNo}" Control.Template="{StaticResource lvTemplate}" Width="70"/>
<Label Content="{Binding Path=b2b_desc}" Control.Template="{StaticResource lvTemplate}" Width="245"/>
<TextBox x:Name="liProfileStockQty" HorizontalContentAlignment="Right" Text="{Binding Path=b2b_ProfileStockQty}" Control.Template="{StaticResource lvTextBoxTemplate}" Width="50"/>
<Label Content="{Binding Path=b2b_StockQty}" Control.Template="{StaticResource lvLabelRightAlignTemplate}" Width="50"/>
<Label Content="{Binding Path=b2b_DBCost}" Control.Template="{StaticResource lvLabelRightAlignTemplate}" Width="50"/>
<Label Content="{Binding Path=b2b_InternetCost}" Control.Template="{StaticResource lvLabelRightAlignTemplate}" Width="50"/>
<Label Content="{Binding Path=b2b_PerCost}" Control.Template="{StaticResource lvLabelRightAlignTemplate}" Width="50"/>
<Label Content="{Binding Path=b2b_TotalCost}" Control.Template="{StaticResource lvLabelRightAlignTemplate}" Width="50"/>
<Separator Width="2"/>
<Label Content="{Binding Path=b2b_TradePrice}" Control.Template="{StaticResource lvLabelRightAlignTemplate}" Width="50"/>
<Label Content="{Binding Path=b2b_PerTrade}" Control.Template="{StaticResource lvLabelRightAlignTemplate}" Width="50"/>
<TextBox x:Name="liDiscountTrade" HorizontalContentAlignment="Right" Text="{Binding Path=b2b_DiscountTrade}" Control.Template="{StaticResource lvTextBoxTemplate}" Width="50"/>
</StackPanel>
</DataTemplate>
my Main Xaml looks like this...
<Window x:Class="B2BPricing.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:my="clr-namespace:B2BPricing"
Title="B2B Pricing" Height="768" Width="1024" WindowStartupLocation="CenterScreen"
WindowStyle="ThreeDBorderWindow" ResizeMode="CanMinimize" Icon="/B2BPricing;component/NewCEF.ico" >
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ListViewResources.xaml" />
</ResourceDictionary.MergedDictionaries>
<my:B2BPricingDataSet x:Key="b2bPricing" />
<CollectionViewSource x:Key="b2bPricingViewSource" Source="{Binding Path=B2bPricing, Source={StaticResource b2bPricing}}" />
<CollectionViewSource x:Key="b2bPricingsupplierViewSource" Source="{Binding Path=B2bPricing_supplier, Source={StaticResource b2bPricingViewSource}}" />
</ResourceDictionary>
</Window.Resources>
<Grid DataContext="{StaticResource b2bPricingViewSource}" Name="MainGrid" >
<Label Content="Label" Height="28" HorizontalAlignment="Left" Margin="836,12,0,0" Name="resultLabel" VerticalAlignment="Top" />
<Label Content="Stock Code" Height="28" HorizontalAlignment="Left" Margin="13,34,0,0" Name="lblStockCode" VerticalAlignment="Top" />
<Label Content="Part No." Height="28" HorizontalAlignment="Right" Margin="0,34,864,0" Name="lblPartNo" VerticalAlignment="Top" />
<Label Content="Description" Height="28" HorizontalAlignment="Right" Margin="0,34,776,0" Name="lblDescription" VerticalAlignment="Top" />
<Grid HorizontalAlignment="Left" Margin="51,609,0,0" Name="grid2" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Content="Stock Code" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
<TextBox Grid.Column="1" Grid.Row="0" Height="23" HorizontalAlignment="Left" Margin="3" Name="b2b_StockCodeTextBox" Text="{Binding Path=b2b_StockCode, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
</Grid>
<Grid HorizontalAlignment="Left" Margin="51,644,0,0" Name="grid1" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Content="Description" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
<TextBox Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="3" Name="b2b_descTextBox" Text="{Binding Path=b2b_desc, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="386" />
</Grid>
<Grid HorizontalAlignment="Left" Margin="297,609,0,0" Name="grdSupplier" VerticalAlignment="Top" Width="219">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Content="Supplier" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
<TextBox Grid.Column="1" Height="22" HorizontalAlignment="Left" Margin="3,4,0,6" Name="tbSupplier" VerticalAlignment="Center" Width="150" Text="159" />
</Grid>
<ListBox ItemsSource="{Binding}" ItemTemplate="{StaticResource myTemplate}" Height="542" HorizontalAlignment="Stretch"
Margin="0,60,0,0" Name="lbPricing" VerticalAlignment="Top" Width="983" IsManipulationEnabled="True"
SelectionMode="Extended" IsSynchronizedWithCurrentItem="True"
KeyboardNavigation.TabNavigation="Continue"
KeyboardNavigation.DirectionalNavigation="Once">
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Label Content="{Binding Path=Name}" Padding="4" Background="{StaticResource myGroupingBrush}" Foreground="White">
</Label>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListBox.GroupStyle>
</ListBox>
<Button Content="_Load" Height="23" HorizontalAlignment="Left" Margin="52,694,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<ProgressBar HorizontalAlignment="Stretch" Margin="230,311,246,385" Name="progressBar1" VerticalAlignment="Stretch" Width="526" Visibility="Visible" IsIndeterminate="False" />
<Grid DataContext="{StaticResource b2bPricingsupplierViewSource}" HorizontalAlignment="Left" Margin="12,-3,0,0" Name="grid4" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Content="Supplier: " Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" FlowDirection="LeftToRight" />
<Label Content="{Binding Path=sup_fullname}" Grid.Column="1" Grid.Row="0" Height="28" HorizontalAlignment="Left" Margin="3" Name="sup_fullnameLabel" VerticalAlignment="Center" />
</Grid>
<Button Content="_Show" Height="23" HorizontalAlignment="Left" Margin="156,694,0,0" Name="btnTextBoxContents" VerticalAlignment="Top" Width="75" Click="btnTextBoxContents_Click" />
</Grid>
and the c#:
private void btnTextBoxContents_Click(object sender, RoutedEventArgs e)
{
ListBoxItem item = (lbPricing.SelectedItem as ListBoxItem);
TextBox tb = (TextBox)lbPricing.ItemsPanel.FindName("liProfileStockQty", lbPricing);
//TextBox tb = dTemplate.FindName("liProfileStockQty") as TextBox;
resultLabel.Content = tb.Text;
}
This is not the only way I've tried. I also tried copying something like this.
foreach (LinePosition item in this.ListLinePositions.Items)
{
CheckBox cb = FindByName("checkedPosition",
(ListViewItem)this.ListLinePositions.ItemContainerGenerator.ContainerFromItem(item)) as CheckBox;
}
but replacing that examples checkbox with the textbox control i'm tring to find

Categories