Hold event on ListViewItem Windows Phone 8.1 - c#

I've got an issue trying catching events on item from a ListView.
WPF Sample
<ListView x:Name="itemsListView" IsHoldingEnabled="True"
ItemContainerStyle="{StaticResource SimpleListItem}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="ItemSp" Holding="ItemSp_Holding">
<FlyoutBase.AttachedFlyout>
<MenuFlyout>
<MenuFlyoutItem Text="Modifier" />
<MenuFlyoutItem Text="Supprimer" />
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
<TextBlock Text="{Binding Title}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Code Behind Sample
private void ItemSp_Holding(object sender, HoldingRoutedEventArgs e)
{
Debug.WriteLine("holded");
}
Style Sample
<Style x:Key="SimpleListItem" TargetType="ListViewItem">
<Setter Property="BorderThickness" Value="0 0 0 2"></Setter>
<Setter Property="BorderBrush" Value="{StaticResource FlashGreen}"></Setter>
<Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
<Setter Property="Background" Value="{StaticResource SmoothDark}"></Setter>
<Setter Property="FontSize" Value="24"></Setter>
</Style>
Events aren't fired on Item (Tap or Hold, same problem).
Outside of these ListView events are working.
Am I doing something wrong?
EDIT:
Full WPF UC
<UserControl
x:Class="MuchroomPhone.ShopItemsUC"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MuchroomPhone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="9*"></RowDefinition>
<RowDefinition Height="1*"></RowDefinition>
</Grid.RowDefinitions>
<Grid x:Name="ItemsGrid">
<ListView x:Name="itemsListView" IsHoldingEnabled="True" ItemContainerStyle="{StaticResource SimpleListItem}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="ItemSp" Holding="ItemSp_Holding">
<FlyoutBase.AttachedFlyout>
<MenuFlyout>
<MenuFlyoutItem Text="Modifier" />
<MenuFlyoutItem Text="Supprimer" />
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
<TextBlock Text="{Binding Title}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
<Grid x:Name="NoItemsGrid">
<TextBlock Style="{StaticResource EmptyElemTextBox}">Aucun item</TextBlock>
</Grid>
<Grid x:Name="AddItemModal" Background="#AA000000" Canvas.ZIndex="1" Visibility="Collapsed">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Rectangle Grid.Row="1" Fill="White"></Rectangle>
<TextBox x:Name="NewItemTb" VerticalAlignment="Center" Grid.Row="1" PlaceholderText="Nom de l'item" ></TextBox>
<StackPanel Grid.Row="2" Orientation="Horizontal">
<Button x:Name="CancelAddItemBtn" Click="CancelAddItemBtn_Click">Annuler</Button>
<Button x:Name="AddItemBtn" Click="AddItemBtn_Click">Ajouter</Button>
</StackPanel>
</Grid>
<Rectangle Grid.Row="2" Fill="Black"></Rectangle>
<Button Grid.Row="2" Click="addItem_Click">New Item</Button>
</Grid>
</UserControl>

working on nearly the same subject just now.. templated listviewitem inside templated usercontrol with handling listview events outside of usercontrol
code below will give me item_holding event outside usercontrol
inside loaded event
lv = Listview
//setting event handler to items in the view
private void ConnectListView( ListView lv , SelectionChangedEventHandler SelectionChanged, HoldingEventHandler Item_holding)
{
lv.SelectionChanged += SelectionChanged;
for (int i = 0; i < lv.Items.Count; i++)
{
ListViewItem item = (ListViewItem)lv.ContainerFromIndex(i);
if (item != null)item.Holding += Item_holding;
}
}
// for items outside view in Listview ContainerContentChanging event
private void LbContacts_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
{
args.ItemContainer.Holding -= lbContactsItem_Holding;
args.ItemContainer.Holding += lbContactsItem_Holding;
}
still playing around and testing, but it seems that it is working.
don't like to do this in ContainerContentChanging event because it is calling too frequently, but for now don't find another point.

Related

Override a specific command button background-color in MVVM using a Shared Resource XAML

How can I change the back-ground color of a specific button without making a new set of styles in the SharedResources.xaml?
Button Picture
In this picture below, it shows three buttons using the same styles from the SharedResource.xaml. My goal is to make only the second button change it's color from "WhiteSmoke" to "Purple/Color of my choice".
Button Picture
MainWindowViewModel.cs
These buttons are styled in the SharedResources.xaml.
protected override void CreateCommands()
{
this.Commands.Add(new CommandViewModel("First Button", new DelegateCommand(p => this.BtnOne())));
this.Commands.Add(new CommandViewModel("Second Button", new DelegateCommand(p => this.BtnTwo())));
this.Commands.Add(new CommandViewModel("Third Button", new DelegateCommand(p => this.BtnThree())));
}
SharedResources.saml
The Background color is "WhiteSmoke". This is the color that I want to override just for that Second Button (or any specific button) without the need of creating another set of styles in the SharedResources.xaml.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:System">
<!-- Navigation Style for Buttons -->
<DataTemplate x:Key="CommandsTemplate">
<ItemsControl ItemsSource="{Binding}" HorizontalAlignment="Center">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Command="{Binding Path=Command}" Content="{Binding Path=DisplayName}" Width="180" Height="40" BorderThickness="1" FontSize="20" FontStyle="Oblique" Background="WhiteSmoke" Margin="8,8,0,0">
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
MainWindow.xaml
The buttons are bound in the "HeaderedContentControl's" "Content" and the Styles of the buttons are bounded in the "ContentTemplate".
<Window x:Class="System.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:System"
mc:Ignorable="d" Title="{Binding Path=DisplayName}" Height="550" Width="1080">
<!-- Connect this xaml to SharedResources -->
<Window.Resources>
<ResourceDictionary Source="SharedResources.xaml" />
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="4" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Border Height="60">
<HeaderedContentControl Content="{Binding Path=Commands}" ContentTemplate="{StaticResource CommandsTemplate}" />
</Border>
<Border Grid.Row="2">
<HeaderedContentControl Content="{Binding Path=ViewModels}" ContentTemplate="{StaticResource WorkspacesTemplate}" />
</Border>
</Grid>
Just use the BasedOn attribute to inherit either an exist resource style or the default one:
<Window.Resources>
<!-- Default button style is blue text on a white background -->
<Style TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="Blue" />
<Setter Property="Background" Value="White" />
</Style>
</Window.Resources>
<!-- Button with blue text on a yellow background -->
<Button Content="Hello World" HorizontalAlignment="Left" VerticalAlignment="Top">
<Button.Style>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="Yellow" />
</Style>
</Button.Style>
</Button>

Stretching a user control in a grid view

I have a usercontrol in a GridView in a uwp app.I want the usercontrol to be stretched in the Gridview's ItemsPanel. I tried to set the HorizontolAlignment of the usercontrol to Stretchbut no success.Someone knows the solution?
here's the xaml of the usercontrol
<UserControl
x:Class="LessonsBuild.Controls.TextItemControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:LessonsBuild.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="614"
HorizontalAlignment="Stretch"
Margin="5"
Height="Auto">
<Grid Width="{Binding RelativeSource={RelativeSource Mode=TemplatedParent },Path=Width}"
Height="Auto" >
<Rectangle x:Name="DropShadow"
Margin="5,5,0,0"
Fill="{StaticResource DropShadowBrush }"/>
<Grid x:Name="Root"
Width ="{Binding RelativeSource={RelativeSource Mode=TemplatedParent },Path=Width}"
Height="Auto"
Margin="0,0,3,3"
Background="White" >
<Grid.RowDefinitions >
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<!--#region Header-->
<Grid Width="{Binding RelativeSource={RelativeSource Mode=TemplatedParent },Path=Width}"
HorizontalAlignment="Stretch" >
<Grid.ColumnDefinitions >
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock x:Name="txtTitel"
Text="{x:Bind CurrentModel.Title}"
FontSize="30"
VerticalAlignment="Top" />
<StackPanel x:Name="controls"
Grid.Column="1"
Orientation="Horizontal" >
<AppBarButton x:Name="btnBold"
Icon="Bold"
Click="btnBold_Click" />
<AppBarButton x:Name="btnItalic"
Icon="Italic"
Click="btnItalic_Click" />
<AppBarButton x:Name="btnUnderline"
Icon="Underline"
Click="btnUnderline_Click" />
<AppBarButton x:Name="btnBullet"
Icon="Bullets"
Click="btnBullet_Click" />
</StackPanel>
<AppBarButton x:Name="btnDelete"
Grid.Column="2"
HorizontalAlignment="Right"
Icon="Delete"
Click="btnDelete_Click"
/>
</Grid>
<!--#endregion-->
<!--#region Content-->
<RichEditBox x:Name="txtBox"
Grid.Row="1" BorderThickness="0,1"
TextChanged="txtBox_TextChanged"
>
</RichEditBox>
<!--#endregion-->
</Grid>
</Grid>
Heres the GridViews xaml
<GridView Grid.Row="1"
Margin="30"
ItemsSource="{Binding TextControlList}" SelectionMode="None"
HorizontalContentAlignment="Stretch" />
To adjust it, modify the item of HorizontalContentAlignment as Stretch, within the 'ItemContainerStyle' (NOT within the gridview itself).
In list/gridview control, you can modify the way of item placement via this ItemContainerStyle.
<GridView x:Name="yourGridView" .... >
<GridView.ItemContainerStyle>
<Style TargetType="GridViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</GridView.ItemContainerStyle>
</GridView>
Try to use a ListView instead of a GridView

Inheritance of text boxes with the same event

I need to create 10 or more text boxes that have the same event and logic,
instead of copy and paste it there is option to create it once and than inherit from it?
For example name1 will inherit from name
<TextBox x:Name="Name" AcceptsReturn="True" AllowDrop="True"
PreviewDragEnter="DropText_PreviewDragEnter"
PreviewDrop="DropText_PreviewDrop"
SelectionChanged="listbox_SelectionChanged"
HorizontalAlignment="Left" Height="25" TextWrapping="Wrap"
VerticalAlignment="Top" Width="150" Grid.Column="4" Margin="0,50,0,0"
Grid.Row="2" />
<TextBox x:Name="name1" AcceptsReturn="True" AllowDrop="True"
PreviewDragEnter="DropText_PreviewDragEnter"
PreviewDrop="DropText_PreviewDrop"
SelectionChanged="listbox_SelectionChanged"
HorizontalAlignment="Left" Height="25" TextWrapping="Wrap"
VerticalAlignment="Top" Width="150" Grid.Column="4" Margin="0,50,0,0"
Grid.Row="2" />
you can create a control template of your textbox as a resource. Mark the resource as x:Shared=False
<Grid.Resources>
<ControlTemplate TargetType="TextBox" x:Key="TxtBoxTemplate" x:Shared="False">
<Grid.Resources>
use this template on other instances of textbox.
<TextBox x:Name="name1" Grid.Row="0" Template="{StaticResource TxtBoxTemplate}"/>
<TextBox x:Name="name2" Grid.Row="1" Template="{StaticResource TxtBoxTemplate}"/>
<TextBox x:Name="name3" Grid.Row="2" Template="{StaticResource TxtBoxTemplate}"/>
Complete code as follows.
<Grid>
<Grid.Resources>
<ControlTemplate TargetType="TextBox" x:Key="TxtBoxTemplate" x:Shared="False">
<TextBox x:Name="Name"
AcceptsReturn="True"
AllowDrop="True"
PreviewDragEnter="DropText_PreviewDragEnter"
PreviewDrop="DropText_PreviewDrop"
SelectionChanged="listbox_SelectionChanged"
HorizontalAlignment="Left" Height="25" TextWrapping="Wrap"
VerticalAlignment="Top" Width="150" Grid.Column="4" Margin="0,50,0,0" Grid.Row="2"
TextChanged="Name_OnTextChanged"
/>
</ControlTemplate>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBox x:Name="name1" Grid.Row="0" Template="{StaticResource TxtBoxTemplate}"/>
<TextBox x:Name="name2" Grid.Row="1" Template="{StaticResource TxtBoxTemplate}"/>
<TextBox x:Name="name3" Grid.Row="2" Template="{StaticResource TxtBoxTemplate}"/>
</Grid>
As mentioned by HighCore in comment, ItemsControl might be of your help here.
Create an ItemsControl and set ItemTemplate to have TextBox inside it. (Create ObjectDataProvider to return number of textBoxes you need)
<Grid>
<Grid.Resources>
<ObjectDataProvider x:Key="EnumerableRange"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:linq="clr-namespace:System.Linq;assembly=System.Core"
ObjectType="{x:Type linq:Enumerable}" MethodName="Range">
<ObjectDataProvider.MethodParameters>
<sys:Int32>1</sys:Int32>
<sys:Int32>15</sys:Int32>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Grid.Resources>
<ItemsControl ItemsSource="{Binding Source={StaticResource EnumerableRange}}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox x:Name="Name"
AcceptsReturn="True"
AllowDrop="True"
PreviewDragEnter="DropText_PreviewDragEnter"
PreviewDrop="DropText_PreviewDrop"
SelectionChanged="listbox_SelectionChanged"
HorizontalAlignment="Left" Height="25"
TextWrapping="Wrap"
VerticalAlignment="Top" Width="150" Grid.Column="4"
Margin="0,50,0,0" Grid.Row="2"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
UPDATE
You can use Style then to declare common properties over there and events and simply have multiple instances of TextBox referring to that style.
<Grid>
<Grid.Resources>
<Style TargetType="TextBox">
<Setter Property="AcceptsReturn" Value="True"/>
<Setter Property="AllowDrop" Value="True"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="Height" Value="25"/>
<Setter Property="Width" Value="150"/>
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="Margin" Value="0,50,0,0"/>
<EventSetter Event="PreviewDragEnter"
Handler="DropText_PreviewDragEnter"/>
<EventSetter Event="PreviewDrop"
Handler="DropText_PreviewDrop"/>
<EventSetter Event="SelectionChanged"
Handler="listbox_SelectionChanged"/>
</Style>
</Grid.Resources>
<TextBox x:Name="Name"/>
<TextBox x:Name="Name1"/>
......
<TextBox x:Name="Name15"/>
</Grid>
Note i haven't set x:Key on Style so it will get applied by default to all TextBoxes in your Grid. If you don't want that set x:Key and use that for all TextBoxes.
# first method : create a class with name customTextbox.cs and write down properties and events for textbox
public class customTextbox:TextBox
{
public customTextbox():base()
{
this.AcceptsReturn = true;
this.AllowDrop = true;
this.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
//set your remaining propreties
this.PreviewDragEnter +=customTextbox_PreviewDragEnter;
this.PreviewDragLeave +=customTextbox_PreviewDragLeave;
this.SelectionChanged += customTextbox_SelectionChanged;
}
void customTextbox_SelectionChanged(object sender, System.Windows.RoutedEventArgs e)
{
//code here
}
private void customTextbox_PreviewDragLeave(object sender, System.Windows.DragEventArgs e)
{
//code here
}
private void customTextbox_PreviewDragEnter(object sender, System.Windows.DragEventArgs e)
{
//code here
}
}
and in xaml
<Window x:Class="WpfApplication3.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"
xmlns:hp="clr-namespace:WpfApplication3">
<Grid Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<hp:customTextbox Height="100" Width="100" Grid.Row="0"></hp:customTextbox>
<hp:customTextbox Grid.Row="1" ></hp:customTextbox>
<hp:customTextbox Grid.Row="2" ></hp:customTextbox>
</Grid>
#second method:create a usecontrol like this(http://prntscr.com/2mren4) and replace usercontrol with textbox like below
<TextBox x:Class="WpfApplication3.CustomTB"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<!--set all properties and event you required inside textbox-->
and in .cs file use textbox instead of usercontrol like below
public partial class CustomTB : TextBox
{
public CustomTB()
{
InitializeComponent();
}
}
and in another window add
<Window x:Class="WpfApplication3.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"
xmlns:hp="clr-namespace:WpfApplication3">
<Grid Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<hp:CustomTB Grid.Row="0"/>
</Grid>

Binding when navigating with caliburn AppBarButton not working

I have this strange problem with AppBarButton. I have binded the button to a method in my view model like this:
XAML for MainView
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True" BackgroundColor="{StaticResource HsAppBarColor}" Opacity="1">
<shell:ApplicationBar.Buttons>
<cal:AppBarButton IconUri="/icons/appbar.feature.settings.rest.png" Text="ajustes" Message="GoToSettings" />
<cal:AppBarButton IconUri="/icons/appbar.favs.rest.png" Text="favoritos" Message="GoToFavs" />
</shell:ApplicationBar.Buttons>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
C# for MainViewModel
public void GoToSettings() {
navigation.UriFor<SettingsViewModel>().Navigate();
}
It fires GoToSettings code and it navigates to SettingsView page however, SettingsViewModel is never initialized and the constructor is not called. Caliburn code for binding view with view model doesn't work.
I wonder if having a Pivot in MainPage which sets a DataContext for its Sample Data could affect. I tried commenting the data context but it still didn't called SettingsViewModel constructor.
This is the Pivot XAML code
<controls:Pivot toolkit:TurnstileFeatherEffect.FeatheringIndex="0" Margin="0" Title="" DataContext="{Binding Source={StaticResource Stores}}" TitleTemplate="{StaticResource StoresPivotTitleTemplate}" HeaderTemplate="{StaticResource StoresPivotHeaderTemplate}" Style="{StaticResource StoresPivotStyle}" FontFamily="Arial Narrow" Background="{x:Null}">
<controls:PivotItem Header="Restorantes" BorderThickness="0,-20,0,0" Margin="12,0" Background="{x:Null}">
<Grid>
<ListBox toolkit:TurnstileFeatherEffect.FeatheringIndex="2" cal:Message.Attach="[Event SelectionChanged] = [Action Restaurants_SelectionChanged];" ItemTemplate="{StaticResource StoresListItemTemplate}" ItemsSource="{Binding Collection}" Margin="0" ItemContainerStyle="{StaticResource StoresListContainerStyle}" Background="{x:Null}"/>
</Grid>
</controls:PivotItem>
<controls:PivotItem Header="Tiendas" Margin="12,0">
<Grid>
<ListBox toolkit:TurnstileFeatherEffect.FeatheringIndex="2" cal:Message.Attach="[Event SelectionChanged] = [Action Stores_SelectionChanged];" ItemTemplate="{StaticResource StoresListItemTemplate}" ItemsSource="{Binding Collection}" Margin="0" ItemContainerStyle="{StaticResource StoresListContainerStyle}" FontFamily="Segoe WP"/>
</Grid>
</controls:PivotItem>
</controls:Pivot>
Stores_SelectionChanged method also navigates to a page but this time the binding works and the viewmodel constructor is fired.
Any idea why it doesn't work in the AppBarButton but it does work in the Pivot attached action when they use the same view model behind?
EDIT: added full xaml for view
<phone:PhoneApplicationPage
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro"
xmlns:controlsPrimitives="clr-namespace:Microsoft.Phone.Controls.Primitives;assembly=Microsoft.Phone.Controls"
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
x:Class="Happyshop.Views.StoresView"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True" Foreground="{x:Null}">
<toolkit:TransitionService.NavigationInTransition>
<toolkit:NavigationInTransition>
<toolkit:NavigationInTransition.Backward>
<toolkit:TurnstileFeatherTransition Mode="BackwardIn"/>
</toolkit:NavigationInTransition.Backward>
<toolkit:NavigationInTransition.Forward>
<toolkit:TurnstileFeatherTransition Mode="ForwardIn" BeginTime="0:0:0.7"/>
</toolkit:NavigationInTransition.Forward>
</toolkit:NavigationInTransition>
</toolkit:TransitionService.NavigationInTransition>
<toolkit:TransitionService.NavigationOutTransition>
<toolkit:NavigationOutTransition>
<toolkit:NavigationOutTransition.Backward>
<toolkit:TurnstileFeatherTransition Mode="BackwardOut"/>
</toolkit:NavigationOutTransition.Backward>
<toolkit:NavigationOutTransition.Forward>
<toolkit:TurnstileFeatherTransition Mode="ForwardOut" BeginTime="0:0:0.2"/>
</toolkit:NavigationOutTransition.Forward>
</toolkit:NavigationOutTransition>
</toolkit:TransitionService.NavigationOutTransition>
<phone:PhoneApplicationPage.Resources>
<Style x:Key="StoresPivotStyle" TargetType="controls:Pivot">
<Setter Property="Margin" Value="0"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<Grid/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:Pivot">
<Grid HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}">
<Grid.Background>
<SolidColorBrush Color="{StaticResource HsDarkBlue}"/>
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="65"/>
<RowDefinition Height="96"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid CacheMode="BitmapCache" Grid.RowSpan="3"/>
<Border Margin="-12,-5,0,0" BorderThickness="0,0,0,0">
<ContentPresenter ContentTemplate="{TemplateBinding TitleTemplate}" Content="{TemplateBinding Title}" Margin="12,5,0,0"/>
</Border>
<Border Grid.Row="1" toolkit:TurnstileFeatherEffect.FeatheringIndex="1" >
<controlsPrimitives:PivotHeadersControl x:Name="HeadersListElement" Background="Black"/>
</Border>
<ItemsPresenter x:Name="PivotItemPresenter" Margin="0" Grid.Row="2"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</phone:PhoneApplicationPage.Resources>
<phone:PhoneApplicationPage.Background>
<SolidColorBrush Color="{StaticResource HsDarkBlue}"/>
</phone:PhoneApplicationPage.Background>
<phone:PhoneApplicationPage.FontFamily>
<StaticResource ResourceKey="PhoneFontFamilyNormal"/>
</phone:PhoneApplicationPage.FontFamily>
<phone:PhoneApplicationPage.FontSize>
<StaticResource ResourceKey="PhoneFontSizeNormal"/>
</phone:PhoneApplicationPage.FontSize>
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True" BackgroundColor="{StaticResource HsAppBarColor}" Opacity="1">
<shell:ApplicationBar.Buttons>
<cal:AppBarButton IconUri="/icons/appbar.feature.settings.rest.png" Text="ajustes" Message="GoToSettings" />
<cal:AppBarButton IconUri="/icons/appbar.favs.rest.png" Text="favoritos" Message="GoToFavs" />
</shell:ApplicationBar.Buttons>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
<controls:Pivot toolkit:TurnstileFeatherEffect.FeatheringIndex="0" Margin="0" Title="" DataContext="{Binding Source={StaticResource Stores}}" TitleTemplate="{StaticResource StoresPivotTitleTemplate}" HeaderTemplate="{StaticResource StoresPivotHeaderTemplate}" Style="{StaticResource StoresPivotStyle}" FontFamily="Arial Narrow" Background="{x:Null}">
<controls:PivotItem Header="Restorantes" BorderThickness="0,-20,0,0" Margin="12,0" Background="{x:Null}">
<Grid>
<ListBox toolkit:TurnstileFeatherEffect.FeatheringIndex="2" cal:Message.Attach="[Event SelectionChanged] = [Action Restaurants_SelectionChanged];" ItemTemplate="{StaticResource StoresListItemTemplate}" ItemsSource="{Binding Collection}" Margin="0" ItemContainerStyle="{StaticResource StoresListContainerStyle}" Background="{x:Null}"/>
</Grid>
</controls:PivotItem>
<controls:PivotItem Header="Tiendas" Margin="12,0">
<Grid>
<ListBox toolkit:TurnstileFeatherEffect.FeatheringIndex="2" cal:Message.Attach="[Event SelectionChanged] = [Action Stores_SelectionChanged];" ItemTemplate="{StaticResource StoresListItemTemplate}" ItemsSource="{Binding Collection}" Margin="0" ItemContainerStyle="{StaticResource StoresListContainerStyle}" FontFamily="Segoe WP"/>
</Grid>
</controls:PivotItem>
</controls:Pivot>
UPDATE
It was a stupid error. When you use generate class with intellisense the class created is not marked as public. Classes are by default marked as internal so Caliburn project could not access the ViewModel and hence why it would not bind it.
Resolved. The problem was that classes created via code generation of Intellisense in Visual Studio are not marked as public.
Classes must be marked public in order to Caliburn to be able to access and instantiate one. Otherwise they use the default visibility which is internal

Select a ListViewItem by clicking on the controls in its DataTemplate

I've written a custom DataTemplate for items in a ListView, something like this:
<DataTemplate x:Key="CustomerStateTemplate">
<Grid Margin="5, 5, 5, 5">
<Grid.ColumnDefinitions>
...
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
...
</Grid.RowDefinitions>
<Image Grid.Row="0" Grid.RowSpan="2" Width="24" Height="20" ... />
<TextBox Style="{StaticResource CustomerStyle}" Grid.Column="0"
Grid.Row="0" Grid.ColumnSpan="2"
Name="nameField">
<TextBox.Text>
<Binding Path="Name" />
</TextBox.Text>
</TextBox>
...
and I've obtain my pretty style.
Now, if I want to select the item, I must click on the white space between the template controls. If I click on the textbox in the ListViewItem, it won't select like an item. So, is there a way to select a ListViewItem by clicking on the controls in its template?
Thanks thousand!
Its possible to add a trigger on the ListViewItem which selects the item always then the keyboardfocus is inside of the item. As you do that on the ListViewItem you have the same behavior for all the controls inside the DataTemplate, which should be your solution...
example:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<Grid>
<Grid.Resources>
<x:Array x:Key="Data" Type="{x:Type sys:String}">
<sys:String>first</sys:String>
<sys:String>second</sys:String>
<sys:String>third</sys:String>
</x:Array>
<Style TargetType="ListViewItem" x:Key="itemStyle">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<ListView ItemsSource="{StaticResource Data}" ItemContainerStyle="{StaticResource itemStyle}">
<ListView.ItemTemplate>
<DataTemplate DataType="{x:Type sys:String}">
<TextBox Text="{Binding .}">
</TextBox>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Page>
i hope its clear...

Categories