if you are having any control example stacklayout or list,
<StackLayout Padding="0,0,0,15" x:Name="GoalsStack" BindableLayout.ItemTemplateSelector="{StaticResource Goaltemplateselector}" BindableLayout.ItemsSource="{Binding FinalGoalList}">
</StackLayout>
Now you want to hide show this stacklayout based on the itemsource count without using any convertor.
you can use the following code easily to perform any action using triggers
<StackLayout Padding="0,0,0,15" x:Name="GoalsStack" BindableLayout.ItemTemplateSelector="{StaticResource Goaltemplateselector}" BindableLayout.ItemsSource="{Binding FinalGoalList}">
<StackLayout.Triggers>
<DataTrigger TargetType="StackLayout" Binding="{Binding Path=FinalGoalList.Count}" Value="0">
<Setter Property="HeightRequest" Value="20"/>
</DataTrigger>
</StackLayout.Triggers>
</StackLayout>
Related
I have a Frame with a StackLayout inside of it:
<Frame CornerRadius="1" HasShadow="false" Margin="10"
BackgroundColor="White" BorderColor="Silver" Padding="0" >
<StackLayout Orientation="Vertical" Spacing="0" Padding="0" >
<xaml:PtiXaml />
<template:LineTemplate />
<xaml:AtiXaml />
<template:LineTemplate />
<xaml:StiXaml />
</StackLayout>
</Frame>
Can I create a new object called NewFrame that is the same as the Frame with the StackLayout inside?
<template:NewFrame>
<xaml:PtiXaml />
<template:LineTemplate />
<xaml:AtiXaml />
<template:LineTemplate />
<xaml:StiXaml />
</template:NewFrame>
or
<template:NewFrame>
<xaml:ABCXaml />
</template:NewFrame>
or
<template:NewFrame>
<Label Text="X" />
</template:NewFrame>
It was suggested I use a Custom View but I have looked and can not find an example of this where it contains other elements inside.
Right-Click at the desired position in your Shared Project (or PCL) in your Solution Explorer (I would recommend adding a folder named "Views" or "CustomViews" and creating the item inside that folder), select "Add new item" and choose "Content View" (without (C#) behind it. The filename should be something like "View1.xaml", you can change that due to your liking, however the important thing is that the xaml extension is there.
This will create a new ContentView with a xaml and xaml.cs file.
Inside the xaml file you can declare your xaml code posted above and write any code necessary into the xaml.cs file.
Now you can add a namespace declaration to the page you want to put your view into:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
...
xmlns:customs="clr-namespace:YourNamespace.Views;assembly=YourNamespace"
and declare the element in that Page's or any layout's content:
<customs:CustomViewName ... />
If you want to be able to control the element's behaviour you can add BindableProperties in the codebehind.
For more in-depth information on that, you might want to take a look into this article: https://visualstudiomagazine.com/articles/2017/10/01/add-custom-controls.aspx
Use a ContentView along with a ControlTemplate to create a Custom Control. This way you can create a new control called NewFrame, write the XAML for your control and then use the <ContentPresenter> tag inside your <ControlTemplate> to assign where you'd like your content to be.
Like so:
.
└── NewFrame
├── NewFrame.cs
└── NewFrame.xaml -> Is a ResourceDictionary
NewFrame.cs:
namespace TestApp.Controls
{
public partial class NewFrame : ContentView
{
}
}
NewFrame.xaml:
<ResourceDictionary
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:newFrame="clr-namespace:TestApp.Controls"
x:Class="Namespace.For.A.ResourceDictionary">
<Style TargetType="newFrame:NewFrame">
<Setter Property="ControlTemplate">
<Setter.Value>
<ControlTemplate>
<ContentView BackgroundColor="Transparent">
<Frame CornerRadius="1" HasShadow="false" Margin="10" BackgroundColor="White" BorderColor="Silver" Padding="0" >
<StackLayout Orientation="Vertical" Spacing="0" Padding="0">
<ContentPresenter/>
</StackLayout>
</Frame>
</ContentView>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
ConsumingYourControl.xaml:
<template:NewFrame>
<template:NewFrame.Content>
<xaml:PtiXaml />
<template:LineTemplate />
<xaml:AtiXaml />
<template:LineTemplate />
<xaml:StiXaml />
</template:NewFrame.Content>
</template:NewFrame>
<template:NewFrame>
<template:NewFrame.Content>
<xaml:ABCXaml />
</template:NewFrame.Content>
</template:NewFrame>
<template:NewFrame>
<template:NewFrame.Content>
<Label Text=""/>
</template:NewFrame.Content>
</template:NewFrame>
My application has objects that are surrounded by this:
<StackLayout HeightRequest="49" BackgroundColor="Red" Orientation="Vertical" Margin="0" >
<Grid VerticalOptions="CenterAndExpand">
<!-- XAML here -->
</Grid>
</StackLayout>
I would like to make it so that I don't need to enter the XAML on the 1st, 2nd, last and next to last lines each time.
Can I use something like a control template to avoid doing this and if so how would I use it?
You will need to use a combination of ContentPresenter and ControlTemplate (as mentioned in the note on this page)
On a ContentPage (or ContentView), the Content property can be assigned and the ControlTemplate property can also be set. When this occurs, if the ControlTemplate contains a ContentPresenter instance, the content assigned to the Content property will be presented by the ContentPresenter within the ControlTemplate.
<ControlTemplate x:Key="DefaultTemplate">
<StackLayout HeightRequest="49" BackgroundColor="Red"
Orientation="Vertical" Margin="0">
<Grid VerticalOptions="CenterAndExpand">
<ContentPresenter /> <!-- This line is replaced by actual content -->
</Grid>
</StackLayout>
</ControlTemplate>
Usage:
<ContentView ControlTemplate="{StaticResource DefaultTemplate}">
<!-- XAML here (basically the view that is to be surrounded by above layout) -->
<Label TextColor="Yellow" Text="I represent the content" />
</ContentView>
Yes, Xamarin also provide the Control Template functionalities in XAML.
Write a below code in App.xaml
<Application.Resources>
<ResourceDictionary>
<ControlTemplate x:Key="TealTemplate">
<Grid>
<Label Text="Control Template Demo App"
TextColor="White"
VerticalOptions="Center" />
</ControlTemplate>
</ResourceDictionary>
</Application.Resources>
Call the template in side ContentPage
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="SimpleTheme.HomePage">
<ContentView x:Name="contentView" Padding="0,20,0,0"
ControlTemplate="{StaticResource TealTemplate}">
<StackLayout VerticalOptions="CenterAndExpand">
<Label Text="Welcome to the app!" HorizontalOptions="Center" />
<Button Text="Change Theme" Clicked="OnButtonClicked" />
</StackLayout>
</ContentView>
</ContentPage>
My ListViews all add a huge amount of padding before and after their children.
For example I have the following ListView in my master
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewModel="clr-namespace:ViewModel;assembly=App"
x:Class="Pages.Master"
Title="Menu">
<ContentPage.BindingContext>
<viewModel:MasterViewModel />
</ContentPage.BindingContext>
<ListView ItemsSource="{Binding Pages}"
ItemSelected="ListView_OnItemSelected"
BackgroundColor="Red">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="S" BackgroundColor="White" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
And it renders the following:
And as soon as you scroll just a little it'll reveal the first element (i.e. the top red space is equal to the height)
Similarly you can keep scrolling until the last item completely disappears (i.e. the bottom red space is equal to the height)
I don't change the properties/styling of ANY of the views in code - it's all done in the XAML, so why is there such a huge amount of space before and after?
It appears to be a problem with the rendering of ListView.Header and ListView.Footer
I added the following to my list view XAML and it removed the giant padding:
<ListView.Header>
<StackLayout HeightRequest="0" />
</ListView.Header>
<!-- ItemTemplate -->
<ListView.Footer>
<StackLayout HeightRequest="0" />
</ListView.Footer>
As this problem was affecting all of my list views, I chose to instead add it as a globally applied style in my App.xaml:
<Style TargetType="ListView">
<Setter Property="Header">
<Setter.Value>
<StackLayout HeightRequest="0" />
</Setter.Value>
</Setter>
<Setter Property="Footer">
<Setter.Value>
<StackLayout HeightRequest="0" />
</Setter.Value>
</Setter>
</Style>
I want to place my Flyoutat the Bottom of my Page.
So i created it like this.
<Grid x:Name="ExampleRoot">
<Grid HorizontalAlignment="Center"
VerticalAlignment="Top">
<Image Source="{Binding Path=CurrentPage}"
Stretch="Uniform" ContinuumNavigationTransitionInfo.IsEntranceElement="True"
/>
</Grid>
<FlyoutBase.AttachedFlyout>
<PickerFlyout Placement="Bottom" > <!-- Here I set my Placement --->
<StackPanel>
<TextBlock Style="{ThemeResource TitleTextBlockStyle}" Text="{Binding Path=DocumentPageCounterDescription, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Style="{ThemeResource TitleTextBlockStyle}" Text="{Binding Path=SelectedAnnotation.Description, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" TextWrapping="Wrap" />
</StackPanel>
</PickerFlyout>
</FlyoutBase.AttachedFlyout>
</Grid>
and open it like this in the code behind
private void SomePropertyChanged(DependencyPropertyChangedEventArgs e)
{
ViewModel selected = e.NewValue as ViewModel;
FlyoutBase fly = FlyoutBase.GetAttachedFlyout(this.LayoutRoot);
fly.ShowAt(this.LayoutRoot);
}
But it opens every time at the top of my page. I can set it to FlyoutPlacementMode.Full for example and it is working the expected way.
I attached it at the complete page and tried it, but the result is the same. So how do I get it to open at the bottom of my Page?
//// Edit
I found the following answer, but this is not working for me! I dont want to open it from a button!
It is working with a MenuFlyout the way I want to, but i want to have a PickerFlyout.
There is descripted that other placements not working on Flyouts. Any Ideas ?
Unfortunately, Windows Phone RT apps only support the flyout in Full or Top placement which you've already established. It is possible to do what you're asking however.
This article might be of use to you: Flyout on Windows Phone 8.1 Runtime
All right I solved it!
I want to show Text / information in it so a MenuFlyout was no option for me. The PickerFlyout isn't modifiable enought so i took the common one.
As I mentioned there is no working placement with a Flyout at the Bottom except with a MenuFlyout.
So here is what I did.
<FlyoutBase.AttachedFlyout>
<Flyout Placement="Full">
<StackPanel Background="{ThemeResource FlyoutBackgroundThemeBrush}" >
<StackPanel Margin="5,0">
<TextBlock Style="{ThemeResource TitleTextBlockStyle}" Text="Some Title Example" />
<TextBlock Style="{ThemeResource TitleTextBlockStyle}" Text="Some Information Example" TextWrapping="Wrap" />
</StackPanel>
</StackPanel>
<Flyout.FlyoutPresenterStyle>
<Style TargetType="FlyoutPresenter">
<Setter Property="Background" Value="Transparent" />
<Setter Property="VerticalContentAlignment" Value="Bottom" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</Flyout.FlyoutPresenterStyle>
</Flyout>
</FlyoutBase.AttachedFlyout>
</Grid>
I set the PlacementMode to Full and applied an invisible Background. Only the the outer Stackpanel is now showing a background. This way it feels like the real PlacementMode.Bottom.
What is the easiest way to set the font size of the panorama item header once so it can be used for all item headers in my app?
There isn't a way to automatically do it for all headers in your app yet. You'll need to set the style for each one.
Implicit styling is coming in the Mango update and that should allow this to be done then.
Update
Here's what you can do now.
Create a global template style for the FontSzie you want. Something like:
<Application.Resources>
<DataTemplate x:Key="MyItemHeaderTemplate">
<Grid>
<ContentPresenter>
<TextBlock Text="{Binding}" FontSize="20" />
</ContentPresenter>
</Grid>
</DataTemplate>
</Application.Resources>
Then in every PanoramaItem that I wish to have styled this way I set the HeaderTemplate:
<controls:PanoramaItem Header="first" HeaderTemplate="{StaticResource MyItemHeaderTemplate}">
// ...
</controls:PanoramaItem>
This had been a difficult issue for me as well. However I have found a pretty simple solution to take care of this for each head item you wantto resize/fontweight/font...so-on. I have inserted a snippet from a current project I have been working on. Take notice to the xaml portion for controls:PanoramaItem.HeaderTemplate. This is where the templete is modified for the header item. Good Luck!
<!--Panorama item one-->
<controls:PanoramaItem Header="Locations">
<Grid>
<ListBox Height="498" HorizontalAlignment="Left" Margin="2,0,0,0" Name="listBox1" VerticalAlignment="Top" Width="424" />
</Grid>
<controls:PanoramaItem.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}" FontSize="55" FontFamily="Segoe WP Bold" Foreground="Black" TextAlignment="Left" FontWeight="Normal" FontStyle="Italic" />
</DataTemplate>
</controls:PanoramaItem.HeaderTemplate>
</controls:PanoramaItem>
Maybe you could try putting this in under the <controls:Panorama> :
<controls:Panorama.TitleTemplate>
<DataTemplate>
<TextBlock Text="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}" FontSize="150" Margin="0,20,0,0" FontWeight="Bold" />
</DataTemplate>
</controls:Panorama.TitleTemplate>
Found here: http://www.jstawski.com/archive/2010/10/25/change-windows-phone-7-panoramarsquos-control-title.aspx
You can create your own PanoramaItem Control and use generic.xaml to apply your custom PanoramaItem style.
public class MyPanoramaItem : Microsoft.Phone.Controls.PanoramaItem
{
public MyPanoramaItem()
{
DefaultStyleKey = typeof(MyPanoramaItem);
}
}
Then you create Themes\Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:YourProjectNamespace">
<Style TargetType="local:MyPanoramaItem">
<!—your custom PanoramaItem style-->
</Style>
</ResourceDictionary>
And then use your custom Panorama like this:
xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
xmlns:local="clr-namespace:YourProjectNamespace"
<Grid x:Name="LayoutRoot" Background="Transparent">
<!--Panorama control-->
<controls:Panorama Title="my application">
<controls:Panorama.Background>
<ImageBrush ImageSource="PanoramaBackground.png"/>
</controls:Panorama.Background>
<!--Panorama item one-->
<local:MyPanoramaItem Header="first item">
</ local:MyPanoramaItem >
</controls:Panorama>
More about generic.xaml and its usage you can find here.