I'm looking for something like a dynamic margin or flexible space in XAML but couldn't find something.
This XAML:
<HubSection VerticalContentAlignment="Stretch">
<DataTemplate>
<Grid>
<TextBlock Text="Products>" Foreground="#FF464646" FontSize="36" Margin="0,-50,0,0"></TextBlock>
<StackPanel VerticalAlignment="Center">
<Button Background="#FF00AEFF" Width="260" Height="60" Content="Button1"></Button>
<Button Background="#FFFF8000" Width="260" Height="60" Content="Button2"></Button>
<Button Background="#FFDE0101" Width="260" Height="60" Content="Button3"></Button>
<Button Background="#FF6300DA" Width="260" Height="60" Content="Button4"></Button>
<Button Background="#FF973E00" Width="260" Height="60" Content="Button5"></Button>
<Button Background="#FF00AA1F" Width="260" Height="60" Content="Button6"></Button>
</StackPanel>
</Grid>
</DataTemplate>
</HubSection>
Gives me:
But the buttons should have margins between them to fill the space according to the available screen height.
Something like this:
Is there something like a dynamic margin height?
Make it grid and it will work like a charm.
<HubSection VerticalContentAlignment="Stretch">
<DataTemplate>
<Grid VerticalAlignment="Stretch" Background="Yellow">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Text="Products>" Foreground="#FF464646" FontSize="36" Grid.Row="0"></TextBlock>
<Grid VerticalAlignment="Stretch" Background="Pink" Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Button Background="#FF00AA1F" Width="260" Height="60" Content="Button6" Grid.Row="6"></Button>
</Grid>
<Grid Grid.Row="1">
<Button Background="#FF00AEFF" Width="260" Height="60" Content="Button1" ></Button>
</Grid>
<Grid Grid.Row="2">
<Button Background="#FFFF8000" Width="260" Height="60" Content="Button2" Grid.Row="2"></Button>
</Grid>
<Grid Grid.Row="3">
<Button Background="#FFDE0101" Width="260" Height="60" Content="Button3" Grid.Row="3"></Button>
</Grid>
<Grid Grid.Row="4">
<Button Background="#FF6300DA" Width="260" Height="60" Content="Button4" Grid.Row="4"></Button>
</Grid>
<Grid Grid.Row="5">
<Button Background="#FF973E00" Width="260" Height="60" Content="Button5" Grid.Row="5"></Button>
</Grid>
</Grid>
</Grid>
</DataTemplate>
</HubSection>
This will solve your problem. I have added colors just that you know which grid is using which space.
If you do not want to hardcode the XAML and keep the XAML clean, be able to add items, I have created the SpaceChildrenBehavior
<Grid Background="AliceBlue" >
<i:Interaction.Behaviors>
<local:SpaceChildrenBehavior/>
</i:Interaction.Behaviors>
<Button Margin="0,10,0,0" Background="#FF00AEFF" Width="260" Height="60" Content="Button1"></Button>
<Button Background="#FFFF8000" Width="260" Height="60" Content="Button2"></Button>
<Button Background="#FFDE0101" Width="260" Height="60" Content="Button3"></Button>
<Button Background="#FF6300DA" Width="260" Height="60" Content="Button4"></Button>
<Button Background="#FF973E00" Width="260" Height="60" Content="Button5"></Button>
<Button Background="#FF00AA1F" Width="260" Height="60" Content="Button6"></Button>
</Grid>
This is the XAML (really clean)
and the behaviour
public class SpaceChildrenBehavior : DependencyObject, IBehavior
{
public DependencyObject AssociatedObject { get; private set; }
public Grid AssociatedGrid;
EventHandler<object> layoutupdated= null;
public void Attach(DependencyObject associatedObject)
{
AssociatedObject = associatedObject;
AssociatedGrid = associatedObject as Grid;
layoutupdated = async (s, e) =>
{
if (AssociatedGrid.ActualHeight > 0)
{
AssociatedGrid.LayoutUpdated -= layoutupdated;
await Task.Delay(100);
AssociatedGrid.RowDefinitions.Clear();
int i = -1;
foreach (var child in AssociatedGrid.Children)
{
AssociatedGrid.RowDefinitions.Add(new RowDefinition()
{ Height = new GridLength(1, GridUnitType.Star) });
Grid.SetRow(child as FrameworkElement, ++i);
}
AssociatedGrid.LayoutUpdated += layoutupdated;
}
};
AssociatedGrid.LayoutUpdated += layoutupdated;
}
public void Detach()
{
AssociatedGrid.LayoutUpdated -= layoutupdated;
}
}
Now is dynamic, you can add more items and it will adapt the grid rows
Related
I'm trying to add button to this datatemplate but I cannot access the ICommand EditTripClick Command from the ViewModel.
So I am wondering is it possible to access it although I am in a datatemplate?
Example code of what I want to do
<CommandBar OverflowButtonVisibility="Auto">
<AppBarButton Label="Add trip" Icon="Add" Command="{x:Bind ViewModel.NewTripClickCommand}"/>
<AppBarButton Label="Refresh" Icon="Refresh" Command="{x:Bind ViewModel.RefreshClickCommand}"/>
</CommandBar>
<controls:Expander
Header="Current Trips"
Background="White"
IsExpanded="{x:Bind ViewModel.HasCurrentTrips, Mode=OneWay }"
IsEnabled="{x:Bind ViewModel.HasCurrentTrips, Mode=OneWay}">
<controls:AdaptiveGridView
Padding="{StaticResource MediumLeftRightMargin}"
animations:Connected.ListItemElementName="itemThumbnail"
animations:Connected.ListItemKey="animationKeyTripsView"
DesiredWidth="180"
ItemHeight="160"
IsItemClickEnabled="True"
ItemClickCommand="{x:Bind ViewModel.ItemClickCommand}"
ItemsSource="{x:Bind ViewModel.CurrentTrips, Mode=OneWay}"
SelectionMode="None"
StretchContentForSingleRow="False">
<controls:AdaptiveGridView.ItemTemplate>
<DataTemplate x:DataType="models:Trip">
<Grid
x:Name="a"
Padding="{StaticResource MediumBottomMargin}"
Background="{ThemeResource SystemControlPageBackgroundChromeLowBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<Button Name="TEST1" Height="30" Width="40"
HorizontalAlignment="Right" VerticalAlignment="Top" Grid.Row="0">
<SymbolIcon Symbol="More"/>
</Button>
<!--<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">-->
<TextBlock
Grid.Row="1"
Margin="{StaticResource XXSmallTopMargin}"
HorizontalAlignment="Center"
Style="{ThemeResource BodyTextStyle}"
Text="{x:Bind Title}" />
<!--</StackPanel>-->
</Grid>
</DataTemplate>
</controls:AdaptiveGridView.ItemTemplate>
</controls:AdaptiveGridView>
</controls:Expander>
First, you could set name of the control which DataContext is your viewmodel, then bind the control to change your button's DataContext. For example, I add a StackPanel and set its name to MyRoot, then reference the its DataContext.
.xaml:
<StackPanel x:Name="MyRoot">
<CommandBar OverflowButtonVisibility="Auto">
<AppBarButton Label="Add trip" Icon="Add" Command="{x:Bind ViewModel.NewTripClickCommand}"/>
<AppBarButton Label="Refresh" Icon="Refresh" Command="{x:Bind ViewModel.RefreshClickCommand}"/>
</CommandBar>
<controls:Expander Header="Current Trips"
Background="White"
IsExpanded="{x:Bind ViewModel.HasCurrentTrips, Mode=OneWay }"
IsEnabled="{x:Bind ViewModel.HasCurrentTrips, Mode=OneWay}">
<controls:AdaptiveGridView DesiredWidth="180"
ItemHeight="160"
IsItemClickEnabled="True"
ItemClickCommand="{x:Bind ViewModel.ItemClickCommand}"
ItemsSource="{x:Bind ViewModel.CurrentTrips, Mode=OneWay}"
SelectionMode="None"
StretchContentForSingleRow="False">
<controls:AdaptiveGridView.ItemTemplate>
<DataTemplate x:DataType="local:Trip">
<Grid x:Name="a">
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<Button Name="TEST1" Height="30" Width="40"
Command="{Binding ElementName=MyRoot,Path=DataContext.EditTripClick}"
HorizontalAlignment="Right"
VerticalAlignment="Top" Grid.Row="0">
<SymbolIcon Symbol="More"/>
</Button>
<TextBlock
Grid.Row="1"
HorizontalAlignment="Center"
Text="{Binding Title}" />
</Grid>
</DataTemplate>
</controls:AdaptiveGridView.ItemTemplate>
</controls:AdaptiveGridView>
</controls:Expander>
</StackPanel>
In addition, you need to set the DataContext in code-behind.
.cs:
this.DataContext = ViewModel;
I am having trouble with height problems in Xamarin PCL with Listview. I want to create a comments section in a scrollable page. But i am struggling with the height of the comments. It just wont expand.
Row.Definition = Auto for the Grid:
<StackLayout Grid.Row="4" Padding="0,20,0,0">
<ListView ItemsSource="{Binding CommentsMVVM}" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<local:CommentsRow />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
In the CommentsRow template:
<Grid RowSpacing="0" ColumnSpacing="14" Padding="20" VerticalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Image Grid.Column="0" Grid.RowSpan="3" Source="{ Binding Image }" VerticalOptions="Start" />
<Label Grid.Column="1" Grid.Row="0" Text="{ Binding UserName }" VerticalOptions="EndAndExpand" TextColor="Blue" />
<Label Grid.Column="1" Grid.Row="1" Margin="0,4,0,0" Text="{ Binding CreatedAt }" TextColor="Grey" FontSize="13" />
<StackLayout Grid.Column="1" Grid.Row="2" Spacing="30" x:Name="StackLayoutMap" VerticalOptions="FillAndExpand" />
</Grid>
And at the C#:
public CommentsRow()
{
InitializeComponent();
var html_label = new HtmlFormattedLabel()
{
TextColor = Color.Black,
FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
};
html_label.SetBinding(HtmlFormattedLabel.TextProperty, "Memo");
this.StackLayoutMap.Children.Add(html_label);
}
But the StackLayout comments text just wont expand automatically. What am i doing wrong?
I have a button with a flyout and for some reason I can't remove the border, white, around the black grid. Any suggestions?
Picture of output
Xaml Implementation
<Button Foreground="Transparent" HorizontalAlignment="Right" Width="30" Height="30" Margin="0,0,15,5">
<Button.Background>
<ImageBrush ImageSource="ms-appx:///Assets/ButtonImage.png" />
</Button.Background>
<Button.Flyout>
<Flyout Placement="Top" >
<Grid Width="300" Height="auto" Margin="0,0,0,0" Background="Black" BorderThickness="3" BorderBrush="blue" >
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Height="50" Grid.Row="0" Background="Black" BorderBrush="Black">
<TextBlock x:Name="SSMenuAppVersionText" Text="123" FontSize="15" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
<Grid x:Name="AppSuggestionGrid" Grid.Row="1" Background="Black" BorderBrush="Black">
<Button x:Name="AppSuggestionButton" Click="FeedBackButtonClicked" Background="Transparent" Height="50" HorizontalAlignment="Stretch">
<TextBlock x:Name="SSMenuAppSuggesstionText" Text="App Suggestions" Foreground="#007AFF" FontSize="14" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Button>
</Grid>
<Grid Grid.Row="2" BorderBrush="Black" Background="Black">
<Button x:Name="ReferButton" Click="ReferButtonClicked" Background="Black" Height="50" HorizontalAlignment="Stretch">
<TextBlock x:Name="SSMenuReferText" Text="Refer " Foreground="#007AFF" FontSize="14" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Button>
</Grid>
<Grid Grid.Row="3" BorderBrush="Black" Background="Black">
<Button x:Name="VisitButton" Click="VisitButtonClicked" Background="Black" Height="50" HorizontalAlignment="Stretch">
<TextBlock x:Name="SSMenuVisitText" Text="Visit " Foreground="#007AFF" FontSize="14" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Button>
</Grid>
</Grid>
</Flyout>
</Button.Flyout>
</Button>
You have options. If we go look at the guts of the Flyout Style Template we notice some set theme resources for Padding and Border which you can use to either override the properties, or just create your own Style template for Flyout and make them whatever you like.
So for example if you went and tossed something like this into your resource dictionary, you should override the ThemeResource for the app.
<Thickness x:Key="FlyoutContentThemePadding">0,0,0,0</Thickness>
<Thickness x:Key="FlyoutBorderThemeThickness">0</Thickness>
Hope this helps, cheers!
I´m trying to build a UWP app and I have the follow components:
MainPage:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Name="TituloStackPanel" Orientation="Horizontal">
<Button Name="HamburguerButton" Content="" FontFamily="Segoe MDL2 Assets" Height="50" Width="50" Click="HamburguerButton_Click"/>
<TextBlock Name="Titulo" Text="Estrutura de Dados" VerticalAlignment="Center" Margin="10"/>
</StackPanel>
<SplitView Name="PrincipalSplitView" DisplayMode="CompactOverlay" IsPaneOpen="False"
CompactPaneLength="50" OpenPaneLength="200" Grid.Row="1">
<SplitView.Pane>
<StackPanel>
<StackPanel Orientation="Horizontal">
<Button Name="ListasButton" Content="L" Width="50" Height="50" Click="ListasButton_Click"/>
<TextBlock Text="Listas" VerticalAlignment="Center" Margin="10"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Button Name="PilhasButton" Content="P" Width="50" Height="50"/>
<TextBlock Text="Pilhas" VerticalAlignment="Center" Margin="10"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Button Name="FilasButton" Content="F" Width="50" Height="50"/>
<TextBlock Text="Filas" VerticalAlignment="Center" Margin="10"/>
</StackPanel>
</StackPanel>
</SplitView.Pane>
<SplitView.Content>
<TextBlock Text="SplitView Content" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</SplitView.Content>
</SplitView>
</Grid>
The next Page will be loaded inside the Content of the SplitView.
<Page>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="200"/>
</Grid.ColumnDefinitions>
<ScrollViewer Background="Cyan" Grid.Column="1" VerticalScrollBarVisibility="Auto">
<RelativePanel Padding="20" MaxHeight="700" VerticalAlignment="Top">
<A lot of Components: TextBlocks, TextBoxes, RadioButtons, ... />
</RelativePanel>
</ScrollViewer>
</Grid>
</Page>
The ScrollViewer should work as a Toolbar on the right side of the window. But when I resize the window, the vertical scroll not works.
Full screen:
Resized:
The problem is because your SplitView is in the Row with the <RowDefinition Height="Auto"/>. This makes the SplitView higher than the Screen and does not scroll as a result.
Use <RowDefinition Height="*"/> for this row to make it as high as the screen is.
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<SplitView Grid.Row="1">
....
</SpitView>
I have a TabControl which has four labels. Its xaml is:
![enter image description here][1]<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TabControl Height="282" HorizontalAlignment="Left" Name="tabControl1"
VerticalAlignment="Top" Width="503">
<TabItem Header="tabItem1" Name="tabItem1">
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="50"/>
<RowDefinition Height="50"/>
<RowDefinition Height="101*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="367"/>
<ColumnDefinition Width="125*" />
</Grid.ColumnDefinitions>
<Label Content="Library:" Height="28" HorizontalAlignment="Left" Margin="309,15,0,0" Name="label1" VerticalAlignment="Top" Width="49" />
<Label Content="Museum:" Height="28" HorizontalAlignment="Left" Margin="303,13,0,0" Name="label2" VerticalAlignment="Top" Width="58" Grid.Row="1" />
<Label Content="Cinema:" Height="28" HorizontalAlignment="Left" Margin="309,11,0,0" Name="label3" VerticalAlignment="Top" Width="50" Grid.Row="2" />
<Label Content="Embankment:" Height="28" HorizontalAlignment="Left" Margin="281,30,0,0" Name="label4" VerticalAlignment="Top" Width="81" Grid.Row="3" />
</Grid>
</TabItem>
</TabControl>
</Grid>
When a program is run then some letters, signs are disappeared and I cannot understand the reason why. For example, in this screen shot it can be seen that colon of word “Cinema” is not drawn.
I made a grid, however, it does not help. What should I do to make my program work? It happens very often. I want all my labels to show correctly.
It can be seen that a colon of a word “Cinema” is not drawn.
try removing the "Width" of the labels and allow the content to size the control
<Grid>
<TabControl Height="282" HorizontalAlignment="Left" Name="tabControl1"
VerticalAlignment="Top" Width="503">
<TabItem Header="tabItem1" Name="tabItem1">
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="50"/>
<RowDefinition Height="50"/>
<RowDefinition Height="101*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="367"/>
<ColumnDefinition Width="125*" />
</Grid.ColumnDefinitions>
<Label Content="Library:" Height="28" HorizontalAlignment="Left" Margin="309,15,0,0" Name="label1" VerticalAlignment="Top" />
<Label Content="Museum:" Height="28" HorizontalAlignment="Left" Margin="303,13,0,0" Name="label2" VerticalAlignment="Top" Grid.Row="1" />
<Label Content="Cinema:" Height="28" HorizontalAlignment="Left" Margin="309,11,0,0" Name="label3" VerticalAlignment="Top" Grid.Row="2" />
<Label Content="Embankment:" Height="28" HorizontalAlignment="Left" Margin="281,30,0,0" Name="label4" VerticalAlignment="Top" Grid.Row="3" />
</Grid>
</TabItem>
</TabControl>
</Grid>