am developing an app that takes an input from user with error labels for input validation
the error label visibility default value is false.
am having more than a 10 entry and their error label.
and after the app takes input and validates it with error labels it hides everything from the middle of the screen to the very end of the layout.
this my xaml code
<ContentPage.Content>
<ScrollView>
<StackLayout
Margin="20">
<!--Unit Type Picker-->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label
Grid.Column="0"
Text="{xct:Translate UnitType}"
Style="{StaticResource PickerLabelStyle}"/>
<Picker
Grid.Column="1"
ItemsSource="{Binding UnitTypeList,Mode=OneWay}"
SelectedIndex="{Binding UnitTypeIndex,Mode=OneWayToSource}"
Style="{StaticResource PickerStyle}"/>
</Grid>
<Label
Text="{Binding UnitTypeError,Mode=OneWay}"
Style="{StaticResource ErrorLabel}"
IsVisible="{Binding UnitTypeErrorVis,Mode=OneWay}"
/>
<!--Unit IsFurnished Picker-->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label
Grid.Column="0"
Text="{xct:Translate Furnished}"
Style="{StaticResource PickerLabelStyle}"/>
<Picker
Grid.Column="1"
ItemsSource="{Binding IsFurnishedList,Mode=OneWay}"
SelectedIndex="{Binding FurnishedIndex,Mode=OneWayToSource}"
Style="{StaticResource PickerStyle}"/>
</Grid>
<Label
Text="{Binding FurnishedError,Mode=OneWay}"
Style="{StaticResource ErrorLabel}"
IsVisible="{Binding FurnishedErrorVis,Mode=OneWay}"
/>
<!--Unit National ID-->
<Entry
Placeholder="{xct:Translate UnitNationalID}"
Text="{Binding UnitNationalID}"
Style="{StaticResource EntryStyle}"
Keyboard="Numeric"/>
<Label
Text="{Binding UnitNationalIDError,Mode=OneWay}"
Style="{StaticResource ErrorLabel}"
IsVisible="{Binding UnitNationalIDErrorVis,Mode=OneWay}"
/>
<!--Floor Num-->
<Entry
Placeholder="{xct:Translate FloorNum}"
Text="{Binding FloorNum,Mode=OneWayToSource}"
Style="{StaticResource EntryStyle}"
Keyboard="Numeric"/>
<Label
Text="{Binding FloorNumError,Mode=OneWay}"
Style="{StaticResource ErrorLabel}"
IsVisible="{Binding FloorNumErrorVis,Mode=OneWay}"
/>
<!--Unit Space-->
<Entry
Placeholder="{xct:Translate UnitSpace}"
Text="{Binding UnitSpace,Mode=OneWayToSource}"
Style="{StaticResource EntryStyle}"
Keyboard="Numeric"/>
<Label
Text="{Binding UnitSpaceError,Mode=OneWay}"
Style="{StaticResource ErrorLabel}"
IsVisible="{Binding UnitSpaceErrorVis,Mode=OneWay}"
/>
<!--Living Room Space-->
<Entry
Placeholder="{Binding LivingRoomSpacePH,Mode=OneWay}"
Text="{Binding LivingRoomSpace,Mode=OneWayToSource}"
Style="{StaticResource EntryStyle}"
Keyboard="Numeric"/>
<!--Rooms Num-->
<Entry
Placeholder="{Binding RoomsNumPH,Mode=OneWay}"
Text="{Binding RoomsNum,Mode=OneWayToSource}"
Style="{StaticResource EntryStyle}"
Keyboard="Numeric"/>
<!--Lights Num-->
<Entry
Placeholder="{Binding LightsNumPH,Mode=OneWay}"
Text="{Binding LightsNum,Mode=OneWayToSource}"
Style="{StaticResource EntryStyle}"
Keyboard="Numeric"/>
<!--Baths Num-->
<Entry
Placeholder="{xct:Translate BathsNum}"
Text="{Binding BathsNum,Mode=OneWayToSource}"
Style="{StaticResource EntryStyle}"
Keyboard="Numeric"/>
<Label
Text="{Binding BathsNumError,Mode=OneWay}"
Style="{StaticResource ErrorLabel}"
IsVisible="{Binding BathsNumErrorVis,Mode=OneWay}"
/>
<!--Maid Rooms Num-->
<Entry
Placeholder="{xct:Translate MaidRoomsNum}"
Text="{Binding MaidRoomsNum,Mode=OneWayToSource}"
Style="{StaticResource EntryStyle}"
Keyboard="Numeric"/>
<Label
Text="{Binding MaidRoomsNumError,Mode=OneWay}"
Style="{StaticResource ErrorLabel}"
IsVisible="{Binding MaidRoomsNumErrorVis,Mode=OneWay}"
/>
<!--Air Conditioner Type Picker-->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label
Grid.Column="0"
Text="{xct:Translate AirCondType}"
Style="{StaticResource PickerLabelStyle}"/>
<Picker
Grid.Column="1"
ItemsSource="{Binding AirCondTypeList,Mode=OneWay}"
SelectedIndex="{Binding AirCondTypeIndex,Mode=OneWayToSource}"
Style="{StaticResource PickerStyle}"/>
</Grid>
<Label
Text="{Binding AirCondTypeError,Mode=OneWay}"
Style="{StaticResource ErrorLabel}"
IsVisible="{Binding AirCondTypeErrorVis,Mode=OneWay}"
/>
<!--Air Cond Power-->
<Entry
Placeholder="{xct:Translate AirCondPower}"
Text="{Binding AirCondPower,Mode=OneWayToSource}"
Style="{StaticResource EntryStyle}"
Keyboard="Numeric"/>
<Label
Text="{Binding AirCondPowerError,Mode=OneWay}"
Style="{StaticResource ErrorLabel}"
IsVisible="{Binding AirCondPowerErrorVis,Mode=OneWay}"
/>
<!--Rent-->
<Entry
Placeholder="{xct:Translate Rent}"
Text="{Binding Rent,Mode=OneWayToSource}"
Style="{StaticResource EntryStyle}"
Keyboard="Numeric"/>
<Label
Text="{Binding RentError,Mode=OneWay}"
Style="{StaticResource ErrorLabel}"
IsVisible="{Binding RentErrorVis,Mode=OneWay}"
/>
<!--Discount-->
<Entry
Placeholder="{Binding DiscountPH,Mode=OneWay}"
Text="{Binding Discount,Mode=OneWayToSource}"
Style="{StaticResource EntryStyle}"
Keyboard="Numeric"/>
<!--Unit Availability Picker-->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label
Grid.Column="0"
Text="{xct:Translate UnitAvailability}"
Style="{StaticResource PickerLabelStyle}"/>
<Picker
Grid.Column="1"
ItemsSource="{Binding UnitAvailabilityList,Mode=OneWay}"
SelectedIndex="{Binding UnitAvailabilityIndex,Mode=OneWayToSource}"
Style="{StaticResource PickerStyle}"/>
</Grid>
<Label
Text="{Binding UnitAvailabilityError,Mode=OneWay}"
Style="{StaticResource ErrorLabel}"
IsVisible="{Binding UnitAvailabilityErrorVis,Mode=OneWay}"
/>
<!--Electricity Meter-->
<Entry
Placeholder="{xct:Translate ElectricityMeterInfo}"
Text="{Binding ElecMeter}"
Style="{StaticResource EntryStyle}"
Keyboard="Numeric"/>
<Label
Text="{Binding ElecMeterError,Mode=OneWay}"
Style="{StaticResource ErrorLabel}"
IsVisible="{Binding ElecMeterErrorVis,Mode=OneWay}"
/>
<!--Water Meter-->
<Entry
Placeholder="{xct:Translate WaterMeterInfo}"
Text="{Binding WaterMeter}"
Style="{StaticResource EntryStyle}"
Keyboard="Numeric"/>
<Label
Text="{Binding WaterMeterError,Mode=OneWay}"
Style="{StaticResource ErrorLabel}"
IsVisible="{Binding WaterMeterErrorVis,Mode=OneWay}"
/>
<!--Submit Button-->
<Button
Text="{xct:Translate Submit}"
Command="{Binding SubmitCommand}"
Style="{StaticResource ButtonStyle}"/>
</StackLayout>
</ScrollView>
</ContentPage.Content>
and my static resourse for styling each view
<!--Entry Style-->
<Style x:Key="EntryStyle" TargetType="Entry">
<Setter Property="VerticalOptions" Value="CenterAndExpand"/>
<Setter Property="HorizontalOptions" Value="FillAndExpand"/>
<Setter Property="PlaceholderColor" Value="Black"/>
<Setter Property="HorizontalTextAlignment" Value="Start"/>
</Style>
<!--Button Style-->
<Style x:Key="ButtonStyle" TargetType="Button">
<Setter Property="VerticalOptions" Value="CenterAndExpand"/>
<Setter Property="HorizontalOptions" Value="CenterAndExpand"/>
<Setter Property="Margin" Value="20"/>
<Setter Property="CornerRadius" Value="30"/>
<Setter Property="BackgroundColor" Value="Black"/>
<Setter Property="TextColor" Value="White"/>
<Setter Property="WidthRequest" Value="140"/>
</Style>
<!--Error Label-->
<Style x:Key="ErrorLabel" TargetType="Label">
<Setter Property="TextColor" Value="Red"/>
</Style>
<!--Picker Style-->
<Style x:Key="PickerStyle" TargetType="Picker">
<Setter Property="WidthRequest" Value="300"/>
<Setter Property="VerticalOptions" Value="CenterAndExpand"/>
<Setter Property="HorizontalOptions" Value="CenterAndExpand"/>
</Style>
<!--Picker Label Style-->
<Style x:Key="PickerLabelStyle" TargetType="Label">
<Setter Property="VerticalOptions" Value="CenterAndExpand"/>
<Setter Property="HorizontalOptions" Value="CenterAndExpand"/>
<Setter Property="TextColor" Value="Black"/>
<Setter Property="FontSize" Value="Medium"/>
</Style>
Note: the screen shot taken from android device xiaomi redmi note 7
It might be this open bug.
A work-around from this comment:
Replace this:
<ScrollView>
...
</ScrollView>
With this:
<Grid>
<ScrollView>
...
</ScrollView>
<Grid>
Related
I am trying to show/hide a label using Trigger on RadioButton check/uncheck but I am getting the error. I can also do it in c# code by inotifypropertychanged but i want to use Triggers and this is my first time. I am not able to understand this error.
<StackLayout Orientation="Vertical" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" Padding="10">
<Frame HasShadow="False" Margin="10,20" CornerRadius="5" BorderColor="{StaticResource BorderColor}" BackgroundColor="#F7F7F7">
<Grid HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<RadioButton Content="English" x:Name="rdbEnglish" HorizontalOptions="FillAndExpand" BackgroundColor="Transparent"
CornerRadius="10" BorderWidth="2" Margin="2"
Grid.Column="0" Grid.Row="0" IsChecked="{Binding IsCheckedEnglish,Mode=TwoWay}" >
</RadioButton>
<RadioButton Content="hindi" x:Name="rdbHindi" HorizontalOptions="FillAndExpand"
BackgroundColor="Transparent"
CornerRadius="10" BorderWidth="2" IsChecked="{Binding IsCheckedHindi,Mode=TwoWay}"
Margin="2" Grid.Column="1" Grid.Row="0" />
</Grid>
</Frame>
<StackLayout Orientation="Vertical" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" Padding="10">
<Label x:Name="lblrdbEnglish" Text="Note : Menu iteam changes will be updated next time"
Margin="2,-10,2,5" TextColor="Red" IsVisible="false" FontAttributes="Italic" >
<Label.Triggers>
<DataTrigger TargetType="RadioButton"
Binding="{Binding Source={x:Reference rdbEnglish},Path=IsChecked}" Value="True" >
<Setter Property="IsVisible" Value="True" />
</DataTrigger>
</Label.Triggers>
</Label>
</StackLayout>
</StackLayout>
Error
System.InvalidOperationException: 'bindable not an instance of AssociatedType'
The TargetType should be of type Label and not RadioButton.
From triggers docs:
TargetType - the control type that the trigger applies to.
...
<Label.Triggers>
<DataTrigger TargetType="Label"
Binding="{Binding Source={x:Reference rdbEnglish},
Path=IsChecked}" Value="True">
<Setter Property="IsVisible" Value="True" />
</DataTrigger>
</Label.Triggers>
I have different Buttons that I want to put inside the <Frame> control, all this with the aim of avoiding the overflow of the image within the <ListView>, but when putting the HorizontalOptions and VerticalOptions property in FillAndExpand is not filling the Frame, I attached an image of the problem
How can I make the <Button> inside the <Frame> fill the space? I have tried to give the Columns and Rows the Auto property and I have not succeeded.
How can I keep the size of the button (Height and Width) but without losing the frame?
How can I fill and expand the button inside a Frame?
Any help for me?
VIEW.XAML:
<ListView
ItemsSource="{Binding ListaInventario}"
SelectionMode="None"
VerticalOptions="FillAndExpand"
IsRefreshing="{Binding IsRefreshing, Mode=TwoWay}"
HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout
Margin="0,4,0,0"
Orientation="Vertical">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label
Text="{Binding Id}"
FontSize="Small"
HorizontalOptions="Center"
FontAttributes="Bold"
HeightRequest="39"
VerticalTextAlignment="Start"
TextColor="{StaticResource das.color.texto}"
Grid.Column="0"
Grid.Row="0">
</Label>
<Label
Text="{Binding NombreComercial}"
HorizontalOptions="Start"
FontSize="Small"
MaxLines="3"
HeightRequest="39"
TextColor="{StaticResource das.color.texto}"
VerticalOptions="Start"
Grid.Column="1"
Grid.Row="0">
</Label>
<Frame
HorizontalOptions="End"
BackgroundColor="Transparent"
Grid.Column="2"
Grid.Row="0">
<Button
ImageSource="ic_pdf"
HeightRequest="35"
WidthRequest="50"
BackgroundColor="{StaticResource das.color.estado_primary}"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Command="{Binding HDSSisquimCommand}"/>
</Frame>
</Grid>
<!---->
<!--CANTIDAD-->
<StackLayout
Orientation="Horizontal"
HorizontalOptions="FillAndExpand"
Margin="0,1,0,2">
<StackLayout
Orientation="Horizontal"
HorizontalOptions="Start"
Margin="0,0,3,0">
<Label
HorizontalOptions="Start"
HorizontalTextAlignment="Start"
VerticalOptions="End"
VerticalTextAlignment="Center"
Text="Cantidad: "
FontAttributes="Bold"
FontSize="Small"
HeightRequest="35"
IsEnabled="{Binding EnabledEstimadoEntry}"></Label>
<Entry
HorizontalOptions="FillAndExpand"
Placeholder="Cantidad"
Keyboard="Numeric"
HorizontalTextAlignment="Center"
MaxLength="4"
FontSize="Small"
HeightRequest="35"
Text="{Binding CantidadDecimal}"
IsEnabled="{Binding EnabledContenedorEntry}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStatesEntry">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="Transparent" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Focused">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="LightGray" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Entry>
</StackLayout>
<!--UNIDAD-->
<StackLayout
Orientation="Horizontal"
HorizontalOptions="FillAndExpand">
<Label
HorizontalOptions="Start"
HorizontalTextAlignment="Start"
VerticalOptions="End"
VerticalTextAlignment="Center"
Text="Unidad: "
FontAttributes="Bold"
FontSize="Small"
HeightRequest="35"
IsEnabled="{Binding EnabledEstimadoEntry}"></Label>
<Picker
iOSSpecific:Picker.UpdateMode = "WhenFinished"
ItemsSource="{Binding ListaUnidadesMedida}"
SelectedItem="{Binding SelectedUnidadMedida}"
ItemDisplayBinding="{Binding NombreCorto}"
Title="Unidad"
FontSize="Small"
HeightRequest="35"
HorizontalOptions="FillAndExpand"
IsEnabled="{Binding EnabledContenedor}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStatesPicker">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="Transparent" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Focused">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="LightGray" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Picker>
</StackLayout>
</StackLayout>
<!-- TWO BUTTONS -->
<StackLayout
Orientation="Horizontal"
HorizontalOptions="End"
Margin="0,1,0,2">
<Button
Margin="0,0,2,0"
ImageSource="ic_actualizar"
BackgroundColor="{Binding ColorBotonActualizar}"
HorizontalOptions="End"
VerticalOptions="Center"
HeightRequest="35"
WidthRequest="50"
Command="{Binding ActualizarSQCommand}"/>
<Button
ImageSource="ic_minus_sisquim"
BackgroundColor="{StaticResource das.color.estado_danger}"
HorizontalOptions="End"
VerticalOptions="Center"
HeightRequest="35"
WidthRequest="50"
Command="{Binding DesasociarSQCommand}"/>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I only needed to set Padding="0" in the frame.
HorizontalOptions and VerticalOptions where not necessary to set as #Jessie Zhang -MSFT suggested.
<Frame Padding="0">
<Button />
</Frame>
You can try to added a property Padding="0" to Frame and remove the following properties of Button.
HeightRequest="35"
WidthRequest="50"
So the code is like this:
<Frame
HorizontalOptions="End"
BackgroundColor="Green"
Grid.Column="2"
Padding="0"
Grid.Row="0">
<Button
Text="test"
BackgroundColor="Accent"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
/>
</Frame>
I am building a Windows 8.1 Universal app. On the orientation change for the phone, the outer "Grid" element doesn't change in width when the phone is switched to Landscape mode (and vice versa if the app is originally opened in landscape - doesn't change width when orientation=portrait). The Width is bound to the PivotItem Width and the PivotItem fills the entire screen on orientation change but the Grid does not.
<Page x:Name="page"
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:m="using:App1.Models"
xmlns:vm="using:App1.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
mc:Ignorable="d">
<Page.BottomAppBar>
<CommandBar Foreground="White" Background="{StaticResource CustomResource}">
<AppBarButton x:Name="BackButton" Label="Back" HorizontalAlignment="Center"
Click="BackButton_Click" Icon="Back" />
<AppBarButton x:Name="ForwardButton" Label="Forward" HorizontalAlignment="Center"
Click="ForwardButton_Click" Icon="Forward" />
<AppBarButton x:Name="HomeButton" Label="Home" HorizontalAlignment="Center"
Click="HomeButton_Click" Icon="Home" />
<AppBarButton x:Name="SettingsButton" Label="Settings" HorizontalAlignment="Center"
Click="SettingsButton_Click" Icon="Setting" />
</CommandBar>
</Page.BottomAppBar>
<Page.Resources>
<DataTemplate x:Key="myHeader">
<TextBlock Text="{Binding}" FontFamily="CustomFont" />
</DataTemplate>
<Style x:Key="PivotStyle" TargetType="Pivot">
<Setter Property="HeaderTemplate" Value="{StaticResource myHeader}"/>
</Style>
<!--<dict:NumberConverter x:Key="myNumberConverter" />-->
</Page.Resources>
<d:Page.DataContext>
<vm:DesignTimeDataSample />
</d:Page.DataContext>
<Grid>
<Pivot x:Name="MyPivot"
Style="{StaticResource PivotStyle}"
Background="{StaticResource CustomItem}"
SelectionChanged="MyPivot_SelectionChanged"
Foreground="Purple"
Grid.Row="1">
<PivotItem x:Name="Feed"
Header="news feed"
FontFamily="CustomFont">
<ScrollViewer Name="ScrollViewer">
<StackPanel x:Name="stackPanel">
<ListView ItemsSource="{Binding TileStories}" x:Name="cont" Margin="0,10,0,10" Background="{StaticResource CustomItem}" BorderBrush="{StaticResource CustomItem}" Foreground="{StaticResource CustomItem}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="MinHeight" Value="0" />
<Setter Property="Margin" Value="0,10,0,10" />
<Setter Property="Foreground" Value="{StaticResource CustomItem}" />
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Border CornerRadius="20" BorderThickness="0" Background="White" HorizontalAlignment="Stretch">
<StackPanel Height="160" Orientation="Horizontal">
<Grid HorizontalAlignment="Stretch" Width="{Binding ActualWidth, ElementName=Feed, Mode=OneWay}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Image Source="{Binding ImagePath}" Margin="20"/>
</Grid>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="2*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0" Margin="0,0,0.333,53" Grid.RowSpan="2" HorizontalAlignment="Stretch">
<TextBlock VerticalAlignment="Center"
LineStackingStrategy="BlockLineHeight"
LineHeight="20" MaxHeight="80"
TextWrapping="WrapWholeWords"
Text="{Binding Headline}" FontSize="18"
FontFamily="CustomItem" />
</Grid>
<Grid Grid.Row="1" Margin="0,0.333,0.333,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<TextBlock VerticalAlignment="Center"
HorizontalAlignment="Left"
Margin="5" Text="{Binding Date}"
Foreground="#FFBDBDBD"
FontFamily="CustomItem" />
</Grid>
<Grid Grid.Column="1">
<AppBarButton
Style="{StaticResource CustomItem}"
VerticalAlignment="Center"
Margin="0,0,0,2"
HorizontalAlignment="Right"
Icon="Read"
CommandParameter="{Binding Url}"
Click="StoryButton_Click" FontFamily="Global User Interface"/>
</Grid>
</Grid>
</Grid>
</Grid>
</StackPanel>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Foreground="White" Content="Send Notification" Grid.Row="0" HorizontalAlignment="Stretch" Name="ToastButton" Click="ToastButton_Click" Margin="0,0,0,25.333"/>
</StackPanel>
</ScrollViewer>
</PivotItem>
<PivotItem x:Name="News" Header="news">
<WebView x:Name="MyWebView" NavigationCompleted="MyWebView_NavigationCompleted"/>
</PivotItem>
</Pivot>
</Grid>
</Page>
I develop an Universal app using MVVM-Light, and I need to manage comments on a page.
On this page, I show a List of comments, and I want to show a MenuFlyout that allows user to edit or delete its comments:
By following this link Using a Behavior to open Attached Flyouts in Windows 8.1 Store apps, I managed to show the MenuFlyout when an Item of the ListView is tapped.
=> But I would like to display the MenuFlyout only if the user is the author of the selected comment...
Is there a way to do this?
Here is my XAML:
<ListView x:Name="myComments"
ItemsSource="{Binding Comments}"
IsItemClickEnabled="True"
SelectionMode="Single"
SelectedItem="{Binding SelectedComment}"
ContinuumNavigationTransitionInfo.ExitElementContainer="True">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,19,12"
HorizontalAlignment="Stretch">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- 1. Author -->
<TextBlock Grid.Column="0"
Text="{Binding name}"
HorizontalAlignment="Stretch" VerticalAlignment="Center"
TextAlignment="Left"
Margin="0"
Foreground="{StaticResource ThemeBrush}"
Style="{StaticResource ListViewItemSubheaderTextBlockStyle}" />
<!-- 2. Date -->
<TextBlock Grid.Column="1"
Text="{Binding date, Converter={StaticResource DateToStringConverter}}"
HorizontalAlignment="Stretch" VerticalAlignment="Center"
TextAlignment="Right"
Margin="0"
Foreground="{StaticResource ThemeBrush}"
Style="{StaticResource ListViewItemSubheaderTextBlockStyle}" />
</Grid>
<!-- 3. Content -->
<TextBlock Text="{Binding content}"
TextAlignment="Left"
TextWrapping="Wrap"
Margin="0"
Foreground="Black"
FontSize="20"
Style="{StaticResource GroupHeaderTextBlockStyle}" />
<!-- MenuFlyout - with Commands -->
<FlyoutBase.AttachedFlyout>
<MenuFlyout>
<MenuFlyout.MenuFlyoutPresenterStyle>
<Style TargetType="MenuFlyoutPresenter">
<Setter Property="Background" Value="{StaticResource ThemeBrush}"/>
</Style>
</MenuFlyout.MenuFlyoutPresenterStyle>
<MenuFlyoutItem Text="Edit"
Command="Binding ElementName=MyPage, Path=DataContext.EditCommentCommand}"/>
<MenuFlyoutItem Text="Delete"
Command="{Binding ElementName=MyPage, Path=DataContext.DeleteCommentCommand}"/>
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
<!-- Behavior -->
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Tapped">
<local:OpenFlyoutAction />
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
And here is the code-cehind:
public class OpenFlyoutAction : DependencyObject, IAction
{
public object Execute(object sender, object parameter)
{
FlyoutBase.ShowAttachedFlyout((FrameworkElement)sender);
return null;
}
}
=> Is it possible to show the FlyoutBase directly in the ViewModel instead of the code-behind?
I've first created another topic because I encountered a problem on MenuFLyoutItem and Command: XAML - MenuFlyoutItem attached to a ListView doesn't work in WP8.1
Tell me if I have to continue on this another topic and delete this one.
In complement to the answer at Depechie on the DataTemplateSelector, here is what I've done:
I create a DataTemplateSelector class called "CommentsTemplateSelector":
public class CommentsTemplateSelector : DataTemplateSelector
{
public DataTemplate UserComment { get; set; }
public DataTemplate NoUserComment { get; set; }
protected override DataTemplate SelectTemplateCore(object item)
{
Commentaire comment = (Commentaire)item;
if (comment.isAuthor)
return UserComment;
else
return NoUserComment;
}
}
I declare this DataTemplateSelector and its associated templates in the page's resources:
<Page.Resources>
<!-- TemplateSelectors -->
<ts:CommentsTemplateSelector x:Key="CommentsTemplateSelector" />
<!-- DataTemplates -->
<!-- UserComment : with MenuFlyout -->
<DataTemplate x:Key="UserComment">
<Border Tapped="Border_Tapped">
<StackPanel Margin="0,0,19,12"
HorizontalAlignment="Stretch">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- 1. Author -->
<TextBlock Grid.Column="0"
Text="{Binding name}"
... />
<!-- 2. Date -->
<TextBlock Grid.Column="1"
Text="{Binding date, Converter={StaticResource DateToStringConverter}}"
... />
</Grid>
...
</StackPanel>
<!-- MenuFlyout -->
<FlyoutBase.AttachedFlyout>
<MenuFlyout>
<MenuFlyout.MenuFlyoutPresenterStyle>
<Style TargetType="MenuFlyoutPresenter">
<Setter Property="Background" Value="{StaticResource ThemeBrush}"/>
</Style>
</MenuFlyout.MenuFlyoutPresenterStyle>
<MenuFlyoutItem Text="Edit"
Command="{Binding ElementName=CommentsPage, Path=DataContext.EditCommentCommand}" />
</MenuFlyoutItem>
<MenuFlyoutItem Text="Delete"
Command="{Binding ElementName=CommentsPage, Path=DataContext.DeleteCommentCommand}" />
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Tapped">
<local:OpenFlyoutAction />
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
</Border>
</DataTemplate>
<!-- NoUserComment : without MenuFlyout -->
<DataTemplate x:Key="NoUserComment">
<Border Tapped="Border_Tapped" Background="Red">
<StackPanel Margin="0,0,19,12"
HorizontalAlignment="Stretch">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- 1. Author -->
<TextBlock Grid.Column="0"
Text="{Binding name}"
... />
<!-- 2. Date -->
<TextBlock Grid.Column="1"
Text="{Binding date, Converter={StaticResource DateToStringConverter}}"
... />
</Grid>
...
</StackPanel>
</Border>
</DataTemplate>
...
<Page.Resources>
Finally, I affected the ItemTemplateSelector of my ListView to the DataTemplateSelector:
<ListView x:Name="myCommentaires"
ItemsSource="{Binding Commentaires}"
IsItemClickEnabled="True"
ItemTemplateSelector="{StaticResource CommentsTemplateSelector}"
SelectionMode="Single"
SelectedItem="{Binding SelectedCommentaire, Mode=TwoWay}"
ContinuumNavigationTransitionInfo.ExitElementContainer="True">
When I launch the app, I go into the class "CommentsTemplateSelector" and it works fine, but the ListView only shows the model name for each items:
So I think there is a Binding problem in the DataTemplates but what is it?
Finally it worked after monvig the templates from the page's resources into ListView.ItemTemplateSelector:
<ListView.ItemTemplateSelector>
<ts:CommentsTemplateSelector>
<!-- UserComment : with MenuFlyout -->
<ts:CommentsTemplateSelector.UserComment>
<DataTemplate>
<Border Tapped="Border_Tapped">
<StackPanel Margin="0,0,19,12"
HorizontalAlignment="Stretch">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- 1. Author -->
<TextBlock Grid.Column="0"
Text="{Binding name}"
... />
<!-- 2. Date -->
<TextBlock Grid.Column="1"
Text="{Binding date, Converter={StaticResource DateToStringConverter}}"
... />
</Grid>
...
</StackPanel>
<!-- MenuFlyout -->
<FlyoutBase.AttachedFlyout>
<MenuFlyout>
<MenuFlyout.MenuFlyoutPresenterStyle>
<Style TargetType="MenuFlyoutPresenter">
<Setter Property="Background" Value="{StaticResource ThemeBrush}"/>
</Style>
</MenuFlyout.MenuFlyoutPresenterStyle>
<MenuFlyoutItem Text="Edit"
Command="{Binding ElementName=CommentsPage, Path=DataContext.EditCommentCommand}" />
</MenuFlyoutItem>
<MenuFlyoutItem Text="Delete"
Command="{Binding ElementName=CommentsPage, Path=DataContext.DeleteCommentCommand}" />
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Tapped">
<local:OpenFlyoutAction />
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
</Border>
</DataTemplate>
</ts:CommentsTemplateSelector.UserComment>
<!-- NoUserComment : without MenuFlyout -->
<ts:CommentsTemplateSelector.NoUserComment>
<DataTemplate>
<Border Background="Red">
<StackPanel Margin="0,0,19,12"
HorizontalAlignment="Stretch">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- 1. Author -->
<TextBlock Grid.Column="0"
Text="{Binding name}"
... />
<!-- 2. Date -->
<TextBlock Grid.Column="1"
Text="{Binding date, Converter={StaticResource DateToStringConverter}}"
... />
</Grid>
...
</StackPanel>
</Border>
</DataTemplate>
</ts:CommentsTemplateSelector.UserComment>
</ts:CommentsTemplateSelector>
<ListView.ItemTemplateSelector>
I don't know why it hasn't worked into the page's resources as I found some samples like this...
I am working on rendering a table with selectable rows. Ultimately the data coming in will be data-bound from a database but right now I'm just trying to get a row to show. Here is what I have:
<Grid Background="WhiteSmoke">
<StackPanel>
<Grid Width="900">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="200" />
<ColumnDefinition Width="200" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.Resources>
<Style TargetType="Border">
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="BorderThickness" Value="2" />
<Setter Property="Background" Value="White" />
<Setter Property="Padding" Value="5" />
</Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Black" />
</Style>
</Grid.Resources>
<Border Grid.Column="0" Grid.Row="1">
<TextBlock Text="Status" />
</Border>
<Border Grid.Column="1" Grid.Row="1">
<TextBlock Text="Work Package" />
</Border>
<Border Grid.Column="2" Grid.Row="1">
<TextBlock Text="Description" />
</Border>
<Border Grid.Column="3" Grid.Row="1">
<TextBlock Text="Foreman" />
</Border>
<Border Grid.Column="4" Grid.Row="1">
<TextBlock Text="Field Issue" />
</Border>
<Border Grid.Column="5" Grid.Row="1">
<TextBlock Text="Start Date" />
</Border>
<Border Grid.Column="6" Grid.Row="1">
<TextBlock Text="Finish Date" />
</Border>
</Grid>
<ListBox >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Width="900">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="200" />
<ColumnDefinition Width="200" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.Resources>
<Style TargetType="Border">
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="BorderThickness" Value="2" />
<Setter Property="Background" Value="White" />
<Setter Property="Padding" Value="5" />
</Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Black" />
</Style>
</Grid.Resources>
<Border Grid.Column="0" Grid.Row="1">
<TextBlock Text="1" />
</Border>
<Border Grid.Column="1" Grid.Row="1">
<TextBlock Text="2" />
</Border>
<Border Grid.Column="2" Grid.Row="1">
<TextBlock Text="3" />
</Border>
<Border Grid.Column="3" Grid.Row="1">
<TextBlock Text="4" />
</Border>
<Border Grid.Column="4" Grid.Row="1">
<TextBlock Text="5" />
</Border>
<Border Grid.Column="5" Grid.Row="1">
<TextBlock Text="6" />
</Border>
<Border Grid.Column="6" Grid.Row="1">
<TextBlock Text="7" />
</Border>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>
The header's are being rendered but the 1st row which should just have numerical values is not showing:
What am I doing wrong? Why isn't the row of data showing?
As I stated in my comment, you have not added any items to the listbox so the dataTemplate will not show.
For testing you can add items after the ItemTemplate:
</ListBox.ItemTemplate>
<ListBoxItem>Item 1</ListBoxItem>
<ListBoxItem>Item 2</ListBoxItem>
</ListBox>