Adding elements dynamically in Grid Columns - xaml

Can i add elements dynamically into Grid columns? I have this
<phone:LongListSelector>
inside a grid with 2 columns.
I want add each object of my list into a StackPanel but adding dinamicaly in the correct column. First element in column 1,
second element in column 2, third element in column 1, fourth element in column 2...
I can do this writing code in .cs file but i will need to create each element programmatically, add each properties and finally add
the element in correct column. Can i do this with xaml?
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<phone:LongListSelector x:Name="MyLongListSelector">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel>
My content here
</StackPanel>
<DataTemplate>
(...)

I suggest using an ItemsControl with Data Templates.
ItemsControl Class

Related

UWP XAML: How to get an auto-sized Grid with equal column widths

My goal is to have a Grid panel that has two columns of controls (each column will contain a vertical StackPanel), where both columns are the same width but auto-sized based on the controls they contain. So both column widths will be equal to the widest control in either column.
I tried doing something like this:
<Grid HorizontalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Content="This is a wide button" HorizontalAlignment="Center" />
<Button Grid.Column="1" Content="Button" HorizontalAlignment="Center" />
</Grid>
The problem here is that the column widths aren't equal. The effect is the same as if I would have specified Width="Auto" for the column definitions. Using a * width only makes the columns equal width if the grid has a HorizontalAlignment of Stretch instead of Center. (Except then the columns aren't auto-sized to the content anymore.)
Am I missing something? Is there a way to get equal-sized column widths based on the content in the grid cells? In a UWP app (so things like UniformGrid aren't available)?
Thats because you have set HorizontalAlignment to "Center" , Grid wont occupy complete page just occupies center part(its horizontal space depends on sum of child elements, in this case it wont add up to fill up whole page.
Just change the HorizontalAlignment = "Stretch" so that whole space available and individual grid components occupy half space.
<Grid HorizontalAlignment="Stretch" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Grid.Column="0"
Content="This is a wide button"
HorizontalAlignment="Center" />
<Button Grid.Column="1"
Content="Button"
HorizontalAlignment="Center"/>
</Grid>
Image:
The Windows Community Toolkit has a Uniform Grid control you can use if you add in the NuGet package.
Telerik RadDataGrid control will help you with your scenario
Add\install Telerik.UI.Xaml.Controls.Grid from Nuget inorder to use RadDataGrid in your UWP App

How to use 2 view boxes to create custom control to draw lines?

I know that We can use a view box to simulate a line in xaml
<BoxView
VerticalOptions="Fill"
HorizontalOptions="Center"
WidthRequest="1"
Color="Black"/>
That would create a vertical line, however I want to create something like:
I wonder if a grid would be enough to create something like that
How to use a viewbox to draw the vertical line from bottom to top until middle, and then use other view box to middle to right
I was thinking on using a stacklayout instead of a grid and then use
StackOrientation.Vertical and LayoutOptions.Center but I donĀ“t know exactly how to proceed.
What would be the best or easiest way to do it?
I have created something similar that you are looking for:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border Grid.ColumnSpan="2" Grid.RowSpan="2" BorderBrush="Black" BorderThickness="4">
</Border>
<Border Grid.Column="2" Grid.Row="2" BorderBrush="Black" BorderThickness="4">
</Border>
</Grid>
PS: Just make sure that the controls inside the first border do not cross the first column of the second row inside the grid as the second border is overlapping the first one.

Width of different elements in Xaml

I have a grid with two columns with width 3* and 1*. Inside the first column i have a stack panel with "width="Auto"" and with different grids inside it with vertical orientation. The grids have two columns with 15* and 1*.
So my question is how to set the first column of the first grid to adjust to the size of the screen and the second column to be always static lets say 50p? i've trayed to set auto to the first column and its elements but it didn't work.
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinitions Width="*"/>
<ColumnDefinitions Width="50"/>
</Grid.ColumnDefinitions>
<StackPanel>
<Grid>
</Grid>
<Grid>
</Grid>
<Grid>
</Grid>
<Grid>
</Grid>
</StackPanel>
</Grid>

Want to merge data from two different Grids

I am working on a silverlight application and am creating girds dynamically with each having one row and two columns. In the first grid I am populating column with some fields(text box/ combobox/ datepicker etc) and leaving column 2 blank. In the second grid I am populating the second column and leaving the first blank and so on alternately. Now I want the data from the second column in the second grid in the first grid. (In a way I want to merge two grids). Is there any way to do that?
It goes something like this:
<Grid>
<views:DynamicFieldsView IsTabStop="True" Grid.Column="0" DataContext="{Binding Path=FieldProperties}" />
</Grid>
And the dynamicFieldsView is like this:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="{Binding GridColumnNumber}" width = 140 ....>
<Controls:FieldItemControl FieldType="{Binding Type}" Margin="10,0,0,0" Grid.Column="{Binding FieldGridColumnNumber}">
<Controls:FieldItemControl.DataTemplates>
<Controls:TemplateSelectorDataTemplate FieldType="TextBox">
<TextBox Text="{Binding UserText}" width = 200 ....>
</Controls:TemplateSelectorDataTemplate>
<Controls:TemplateSelectorDataTemplate FieldType="Button">
<Button x:Name="SearchButton" Margin="10 10 10 0" .....>
</Controls:TemplateSelectorDataTemplate>
<Controls:TemplateSelectorDataTemplate FieldType="DropDownList">
<DynamicControls:AutoCompleteComboBox VerticalAlignment="Top" Width =200 ....>
</Controls:TemplateSelectorDataTemplate>
....
....
....
</Controls:FieldItemControl.DataTemplates>
</Controls:FieldItemControl>
</Grid>
You should clone the control from the second grid into the first and make a binding so both display same data.

Vertically aligning Labels and TextBlocks at Top in XAML

How can I vertically align a Label and TextBlock at Top so that their first lines of text line up?
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<Label Grid.Column="0" VerticalAlignment="Top">Some Label:</Label>
<TextBlock Grid.Column="0" VerticalAlignment="Top">Some text<TextBlock>
</Grid>
The above code gives me this:
Vertically misaligned Label and TextBlock text http://img156.imageshack.us/img156/4940/labeltextblock.png
The extra space around the label comes from the Padding property. To remove the space, you can explicitly set the Padding property to "0" directly on the Label, or, of course, set it via a Style.
<TextBlock>
<InlineUIContainer BaselineAlignment="Top"><Label Content="Label"/></InlineUIContainer>
<InlineUIContainer BaselineAlignment="Top"><TextBlock>TextBlock Content</TextBlock> </InlineUIContainer>
</TextBlock>
HTH.
Here is a workaround: Align bottoms of text in controls.
I posted a connection: https://connect.microsoft.com/WPF/feedback/ViewFeedback.aspx?FeedbackID=523432, please vote.