Merge Grid column Header in WPF - wpf

I have a wpf Grid, i want to merge the two Column Header.I have tried using the below code the problem here is the column spawn to 2 but in the cell is one that why the for each column grid line is not visible.Can any body answer the better solution to achieve this
<toolkit:DataGridTemplateColumn>
<toolkit:DataGridTemplateColumn.Header>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Grid.ColumnSpan="3" HorizontalAlignment="Center" Text="Missing from FISAP Summary Part II Section F"/>
<TextBlock Grid.Column="0" Grid.Row="1" Text="A" Width="60" HorizontalAlignment="Center"/>
<TextBlock Grid.Column="1" Grid.Row="1" Text="C" Width="60" HorizontalAlignment="Center"/>
<TextBlock Grid.Column="2" Grid.Row="1" Text="D" Width="60" HorizontalAlignment="Center"/>
</Grid>
</toolkit:DataGridTemplateColumn.Header>
<toolkit:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Path=A}" Width="60" HorizontalAlignment="Right" />
<TextBlock Grid.Column="1" Text="{Binding Path=C}" Width="60" HorizontalAlignment="Right" />
<TextBlock Grid.Column="2" Text="{Binding Path=D}" Width="60" HorizontalAlignment="Right" />
</Grid>
</DataTemplate>
</toolkit:DataGridTemplateColumn.CellTemplate>
</toolkit:DataGridTemplateColumn>

Related

WPF how to do this indentation?

I need to format a page as follows:
This code is obviously totally wrong but gives a general idea:
<Grid Grid.Row="10">
<Label Grid.Row="0" Grid.Column="0" Content="Label1"/>
<TextBox Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="3"/>
<Label Grid.Row="0" Grid.Column="4" Content="Label2"/>
<TextBox Grid.Row="0" Grid.Column="5">
<Label Grid.Row="0" Grid.Column="6" Content="Label3"/>
<TextBox Grid.Row="0" Grid.Column="7">
<Label Grid.Row="1" Grid.Column="0" Content="Label4"/>
<TextBox Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="3"/>
<Label Grid.Row="1" Grid.Column="0" Content="Label5"/>
<TextBox Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="3"/>
</Grid>
The most tricky part for me is how to make label2/textbox2 & label3/textbox3 indented to textbox5.
The size of the labels should be dynamic according to the text source so using Width percentages for them is less desirable.
Also, this is a section of a long page (hence the Row="10") so it is best if solution doesn't affect rest of page (I think it won't since it's in the Grid).
Because you have encased this inside of its own Grid, you can divide it into however many rows and columns you want. You've already started with the appropriate column count by putting gray dividers in your image! Just use that and count the number of rectangles you've drawn from left-to-right to define your columns.
<Grid Grid.Row="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="Label1"/>
<TextBox x:Name="Textbox1" Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="6" Text="text box 1"/>
<Label Grid.Row="0" Grid.Column="8" Content="Label2"/>
<TextBox x:Name="Textbox2" Grid.Row="0" Grid.Column="9" Grid.ColumnSpan="3" Text="text box 2"/>
<Label Grid.Row="0" Grid.Column="12" Content="Label3"/>
<TextBox x:Name="Textbox3" Grid.Row="0" Grid.Column="13" Grid.ColumnSpan="1" Text="text box 3"/>
<Label Grid.Row="1" Grid.Column="0" Content="Label4" />
<TextBox x:Name="Textbox4" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="6" Text="text box 4"/>
<Label Grid.Row="1" Grid.Column="8" Content="Label5"/>
<TextBox x:Name="Textbox5" Grid.Row="1" Grid.Column="9" Grid.ColumnSpan="6" Text="text box 5"/>
</Grid>
Here's the output:

XAML - 2 buttons in one grid column

I have to put 2 buttons in this same row and column on grid
i don't want use margin or vertical position, but i want to their positions is:
on upper than second.
how to simply do that?
i did this by:
<button1 verticalAligment="top" />
<button2 verticalAligment="bottom" />
but how to do that better way?
Try This.
<telerik:RadGridView Name="AppsGridView" AutoGenerateColumns="False" IsReadOnly="True">
<telerik:RadGridView.Columns>
<telerik:GridViewColumn>
<telerik:GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="10"/>
</Grid.ColumnDefinitions>
<telerik:RadButton Grid.Row="0" Grid.Column="1" Width="40" Cursor="Hand" Content="Edit" FontSize="14" Background="#FFF2F2F2" CornerRadius="3" verticalAligment="top"/>
<telerik:RadButton Grid.Row="0" Grid.Column="1" Width="50" Cursor="Hand" Content="Delete" FontSize="14" Background="#FFF2F2F2" CornerRadius="3" verticalAligment="bottom"/>
</Grid>
</DataTemplate>
</telerik:GridViewColumn.CellTemplate>
</telerik:GridViewColumn>
</telerik:RadGridView.Columns>
</telerik:RadGridView>
Use Wrappanel as follows
<Wrappanel Orientation="Horizontal"><button1 HorizontalAlignment="Left"/><button2 HorizontalAlignment="Right" /></Wrappanel>

WPF: Change HeaderedContentControl orientation

Is there a way to change the orientation of the Header and the Content in a HeaderedContentControl?.
This is what I need to do:
Can someone give me a hand??
You can create a UserControl with something like the following as it's content :
Inside a UC (or any Template or way you may desire to wrap it in) :
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid Background="Green">
<TextBlock Text="Some Title" Background="Transparent" RenderTransformOrigin="0.5,0.5" VerticalAlignment="Center">
<TextBlock.LayoutTransform>
<RotateTransform Angle="-90"/>
</TextBlock.LayoutTransform>
</TextBlock>
</Grid>
<ContentControl Grid.Column="1">
<ContentControl.Content>
<Rectangle Fill="Red" /> <!-- Or Any Content you may desire -->
</ContentControl.Content>
</ContentControl>
</Grid>
I just solved it by using a grid as everyone does!
I created a grid with several columns and and I took the first column for working as the header. As you can see I had to rotate the text inside using a RenderTransform, but i looks pretty fine for me.
I know this is not answering the real question with the HeaderedContentControl, but for my task was the solution.
Here is my XAML:
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.05*"/>
<ColumnDefinition Width="0.15*"/>
<ColumnDefinition Width="0.15*"/>
<ColumnDefinition Width="0.15*"/>
<ColumnDefinition Width="0.15*"/>
<ColumnDefinition Width="0.15*"/>
</Grid.ColumnDefinitions>
<Grid Column="0" Background="#009999">
<TextBlock Grid.Column="0" Text="{Binding Strand.Description,FallbackValue='Strand X'}" FontSize="18"
Foreground="White" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0 30">
<TextBlock.LayoutTransform>
<RotateTransform Angle="-90"/>
</TextBlock.LayoutTransform>
</TextBlock>
</Grid>
<Grid Column="1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.25*"/>
<RowDefinition Height="0.25*"/>
<RowDefinition Height="0.25*"/>
<RowDefinition Height="0.25*"/>
<RowDefinition Height="0.25*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.55*"/>
<ColumnDefinition Width="0.45*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.ColumnSpan="2" Style="{StaticResource SubHeader}" Text="{x:Static Localization:LanguageResource.Variables}" TextAlignment="Center"/>
<TextBlock Grid.Row="1" Style="{StaticResource GridKeyCell}" Text="{x:Static Localization:LanguageResource.OperationMode}"/>
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding FALTA,FallbackValue='---'}" MinWidth="50" TextAlignment="Center" Style="{StaticResource GridValueCellHighRelevance}"/>
<TextBlock Grid.Row="2" Style="{StaticResource GridKeyCell}" Text="{x:Static Localization:LanguageResource.Speed}"/>
<TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding FALTA,FallbackValue='---'}" MinWidth="50" TextAlignment="Center" Style="{StaticResource GridValueCell}"/>
<TextBlock Grid.Row="3" Style="{StaticResource GridKeyCell}" Text="{x:Static Localization:LanguageResource.Direction}"/>
<TextBlock Grid.Row="3" Grid.Column="1" Text="{Binding FALTA,FallbackValue='---'}" MinWidth="50" TextAlignment="Center" Style="{StaticResource GridValueCell}"/>
<TextBlock Grid.Row="4" Style="{StaticResource GridKeyCell}"/>
<TextBlock Grid.Row="4" Grid.Column="1" MinWidth="50" TextAlignment="Center" Style="{StaticResource GridValueCell}"/>
</Grid>
</Grid>
<Grid Column="2">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.25*"/>
<RowDefinition Height="0.25*"/>
<RowDefinition Height="0.25*"/>
<RowDefinition Height="0.25*"/>
<RowDefinition Height="0.25*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.55*"/>
<ColumnDefinition Width="0.15*"/>
<ColumnDefinition Width="0.30*"/>
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Grid.Row="1" Grid.RowSpan="4" Source="../Style/Images/Withdrawal - DummyBar.png" Stretch="Fill" Height="105" Width="45"></Image>
<TextBlock Grid.Row="0" Grid.ColumnSpan="3" Style="{StaticResource SubHeader}" Text="{x:Static Localization:LanguageResource.Machine4}" TextAlignment="Center"/>
<TextBlock Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" Style="{StaticResource GridKeyCell}" Text="{x:Static Localization:LanguageResource.PressurePSI}"/>
<TextBlock Grid.Row="2" Grid.Column="1" Style="{StaticResource GridValueCell}" Text="{Binding FALTA,FallbackValue='---'}"/>
<TextBlock Grid.Row="2" Grid.Column="2" Style="{StaticResource GridValueCell}" Text="{Binding FALTA,FallbackValue='---'}"/>
<TextBlock Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="2" Style="{StaticResource GridKeyCell}" Text="{x:Static Localization:LanguageResource.Current}"/>
<TextBlock Grid.Row="4" Grid.Column="1" Style="{StaticResource GridValueCell}" Text="{Binding FALTA,FallbackValue='---'}"/>
<TextBlock Grid.Row="4" Grid.Column="2" Style="{StaticResource GridValueCell}" Text="{Binding FALTA,FallbackValue='---'}"/>
</Grid>
</Grid>
</Grid>
I didn't bind the data... where says 'FALTA' you just read any property you want.
Thanks everyone for the answers!!

Arrow Key Navigation in a WPF Form is Odd

I have a WPF form with which I would like to have arrow-key navigation. The current system is off, even though I have successfully specified the tab navigation order. I have two rows of three radio buttons. When I am on the middle button, top row, I should be able to hit the left arrow and select the first button of the top row; instead it selects the first button, bottom row. How do I correct this?
Update:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20*"/>
<ColumnDefinition Width="80*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="15*"/>
<RowDefinition Height="15*"/>
<RowDefinition Height="15*"/>
<RowDefinition Height="30*"/>
<RowDefinition Height="25*"/>
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="0" Content="Who:" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,0,5,0" FontSize="12"/>
<Label Grid.Column="0" Grid.Row="1" Content="Caller:" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,0,5,0" />
<Label Grid.Column="0" Grid.Row="2" Content="Office:" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,0,5,0" />
<Label Grid.Column="0" Grid.Row="3" Content="Name:" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,0,5,0" />
<Button Grid.Column="0" Grid.Row="4" Content="Find" VerticalAlignment="Bottom" HorizontalAlignment="Left" Margin="0,0,0,0" Width="50" Command="{Binding Path=FindCommand}" IsDefault="True" Click="Button_Click" TabIndex="9" />
<Grid Grid.Column="1" Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<RadioButton Content="All People" Grid.Column="0" HorizontalAlignment="Left" Margin="0,5,0,0" VerticalAlignment="Top" IsChecked="{Binding Path=IsPersonAll}" TabIndex="3" />
<RadioButton Content="Users Only" Grid.Column="1" HorizontalAlignment="Left" Margin="0,5,0,0" VerticalAlignment="Top" IsChecked="{Binding Path=IsPersonUser}" TabIndex="4" />
<RadioButton Content="Admin Only" Grid.Column="2" HorizontalAlignment="Left" Margin="0,5,0,0" VerticalAlignment="Top" TabIndex="5" />
</Grid>
<Grid Grid.Column="1" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<RadioButton Content="Off Campus" Grid.Column="0" HorizontalAlignment="Left" Margin="0,5,0,0" VerticalAlignment="Top" IsChecked="{Binding Path=IsCallerOff}" TabIndex="6" />
<RadioButton Content="Admin" Grid.Column="1" HorizontalAlignment="Left" Margin="0,5,0,0" VerticalAlignment="Top" IsChecked="{Binding Path=IsCallerOnAd}" TabIndex="7" />
<RadioButton Content="User" Grid.Column="2" HorizontalAlignment="Left" Margin="0,5,0,0" VerticalAlignment="Top" TabIndex="8" />
</Grid>
<ComboBox
Grid.Column="1" Grid.Row="2"
Height="25" HorizontalAlignment="Stretch"
Margin="0,0,0,0" VerticalAlignment="Top"
ItemsSource="{Binding Path=Offices}"
SelectedItem="{Binding Path=Office}" TabIndex="2" />
<Grid Grid.Column="1" Grid.Row="3">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="55*"/>
<RowDefinition Height="45*"/>
<RowDefinition Height="50*"/>
</Grid.RowDefinitions>
<TextBox Name="lName" Grid.Column="0" Grid.Row="0" Margin="0,0,5,0" VerticalAlignment="Top" HorizontalAlignment="Stretch" Height="22" Text="{Binding Path=LastName, UpdateSourceTrigger=PropertyChanged}" TabIndex="0" />
<TextBox Grid.Column="1" Grid.Row="0" Margin="5,0,0,0" VerticalAlignment="Top" HorizontalAlignment="Stretch" Height="22" Text="{Binding Path=FirstName, UpdateSourceTrigger=PropertyChanged}" TabIndex="1" />
<Label Grid.Column="0" Grid.Row="1" Content="Last Name" HorizontalAlignment="Left" Margin="-5,-5,0,0" VerticalAlignment="Top" />
<Label Grid.Column="1" Grid.Row="1" Content="First Name" HorizontalAlignment="Left" Margin="0,-5,0,0" VerticalAlignment="Top" />
<Label Grid.Row="2" Grid.ColumnSpan="2" Content="Please supply search information in Department or Name." Foreground="Red" HorizontalAlignment="Stretch" Margin="-5,-5,0,0" VerticalAlignment="Top" Height="26" Visibility="{Binding Path=ErrVisibility}"/>
</Grid>
</Grid>
It's also for me kind of odd behaviour and after playing around with it i can tell you that it's caused by the HorizontalAlignment property, but i can't really explain why. Maybe someone else can shed some light on that.
Some more information:
In your case controls are focused alphabetically by the content property. You can easily test that by changing
<RadioButton Content="All People" Grid.Column="0" HorizontalAlignment="Left"
to
<RadioButton Content="Pall People" Grid.Column="0" HorizontalAlignment="Left"
After changing, controls are now focused correctly from right to left on first line because of alphabetical order: Users -> Pall People -> Off Campus.
As i was not aware of this behaviour i tried to reproduce by creating a new project with following XAML, but the behaviour of arrow keys was as expected, focusing the next neigbour in direction of the arrow key:
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<RadioButton Grid.Column="0" Content="All People"></RadioButton>
<RadioButton Grid.Column="1" Content="Users Only"></RadioButton>
<RadioButton Grid.Column="2" Content="Admin Only"></RadioButton>
</Grid>
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<RadioButton Grid.Column="0" Content="Off Campus"></RadioButton>
<RadioButton Grid.Column="1" Content="Admin"></RadioButton>
<RadioButton Grid.Column="2" Content="User"></RadioButton>
</Grid>
</Grid>
So i removed properties of the radio buttons of your xaml till i finally hit the point by removing HorizontalAlignment.
That XAML is now working as expected, at least for the radio buttons:
<Grid Grid.Column="1" Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<RadioButton Content="All People" Grid.Column="0" Margin="0,5,0,0" VerticalAlignment="Top" IsChecked="{Binding Path=IsPersonAll}" TabIndex="3" />
<RadioButton Content="Users Only" Grid.Column="1" Margin="0,5,0,0" VerticalAlignment="Top" IsChecked="{Binding Path=IsPersonUser}" TabIndex="4" />
<RadioButton Content="Admin Only" Grid.Column="2" Margin="0,5,0,0" VerticalAlignment="Top" TabIndex="5" />
</Grid>
<Grid Grid.Column="1" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<RadioButton Content="Off Campus" Grid.Column="0" Margin="0,5,0,0" VerticalAlignment="Top" IsChecked="{Binding Path=IsCallerOff}" TabIndex="6" />
<RadioButton Content="Admin" Grid.Column="1" Margin="0,5,0,0" VerticalAlignment="Top" IsChecked="{Binding Path=IsCallerOnAd}" TabIndex="7" />
<RadioButton Content="User" Grid.Column="2" Margin="0,5,0,0" VerticalAlignment="Top" TabIndex="8" />
</Grid>

Set two children elements with equal width, each with 50% in wpf

If I have two elements in a stackpanel:
<StackPanel Margin="2,2,2,2" Orientation="Horizontal">
<TextBlock Grid.Column="0" Text="{Binding Name}" />
<TextBox Grid.Column="1" Text="{Binding Age}"/>
</StackPanel>
How can I set them with the width equally 50%,50%?
To answer your comment
<Grid>
<!-- Define Columns -->
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Name}" />
<TextBox Grid.Column="1" Text="{Binding Age}"/>
</Grid>
use UnformGrid with columns 2 or Grid with 2 columns
<UniformGrid Columns="2">
<TextBox></TextBox>
<TextBox></TextBox>
</UniformGrid>
or
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
Text="{Binding Name}" />
<TextBox Grid.Column="1"
Text="{Binding Age}" />
</Grid>

Resources