I have a page with a KeyDown handler, but I noticed that the handler is not invoked if the page does not contain a button.
E.g., the handler is not invoked in this case:
<Page
...
KeyDown="Page_KeyDown">
<StackPanel>
<TextBlock />
</StackPanel>
</Page>
But it is invoked in this case:
<Page
...
KeyDown="Page_KeyDown">
<StackPanel>
<TextBlock />
<Button>xxxxx</Button>
</StackPanel>
</Page>
Any idea why does this happen? Can I make the KeyDown event work even without a button?
Thanks.
Related
Can anyone suggest what is wrong with this xaml?
<phone:PivotItem Header="Test" x:Name="TestTab">
<Grid Margin="0,0,0,0">
<phone:LongListSelector x:Name="UserProducts" ItemsSource="{Binding Path=ProductList}">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<Grid x:Name="ProductDetailsGrid">
<Button x:Name="DisplayProductDetails" Content="{Binding Id}" Tap="DisplayProductDetailsButton_OnTap"/>
<phone:LongListSelector x:Name="ListSelector">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<TextBlock x:Name="TextBlock" Text="Hi"/>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</Grid>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</Grid>
</phone:PivotItem>
If I comment the inner LongList, it works fine and generate the buttons for each Item in ProductList. Does the inner list have to be bind as well? can I not bind it on run time?
IDEA:
What I want to achieve is following
when the user clicks on the particular button, I display the product details. so when user clicks on product a. I can at run time get details of product A and bind it to inner list.
what seems to happen is that LongListSelector is not direct child of the PivotItem, you don't need the Grid, remove it, that was causing the problem.
I'm currently writing app for Windows Phone 8.0 and I have a problem with RelayCommand from MVVM Toolkit light and popup.
I have a LongListSelector on the popup:
<Popup x:Name="popUp" Grid.Row="0" Grid.RowSpan="3" Opened="PopupOpened">
<Grid>
<ScrollViewer Grid.Row="2" Margin="10">
<phone:LongListSelector ItemsSource="{Binding ItemsSelections}"
ItemTemplate="{StaticResource DetailsItemTemplate}" />
</ScrollViewer>
</Grid>
</Popup>
And item's template:
<DataTemplate x:Key="DetailsItemTemplate">
<Button VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" Margin="5,5,5,5"
Command="{Binding ElementName=LayoutRoot,Path=DataContext.ItemExecutionCommand}"
CommandParameter="{Binding Type}">
</Button>
</DataTemplate>
When I open popup for the first time, everything works fine, CanExecute returns false, buttons are disabled and the disable style is applied (foreground is gray). But, when I close popup and open again, buttons are disabled, but have normal style (foreground is white). Executing RaiseCanExecuteChanged method after every popup opening does not help.
Anyone has this problem?
Testing Windows Phone 8 Apps using MVVM framework.
ListPage.xaml:
<phone:PanoramaItem x:Name="mpnP" Width="475">
<Grid Style="{StaticResource GridPanoPage}" >
<ListBox x:Name="pkList" Style="{StaticResource ListBoxHeadlines}" >
<ListBox.ItemTemplate>
<DataTemplate >
<ListBoxItem Style="{StaticResource ListBoxItemHub}">
<StackPanel Orientation="Horizontal">
<Image x:Name="pkImage" Source="{Binding ImagePath}" />
<StackPanel>
<TextBlock x:Name="pkHead" Text="{Binding Head}"/>
</StackPanel>
</StackPanel>
</ListBoxItem>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</phone:PanoramaItem>
On List Box selection changed , navigate to detail page on the basis of ID-
if (!string.IsNullOrEmpty(Session.ID))
NavigationService.Navigate(new Uri("/Views/Detail.xaml", UriKind.Relative));
Now I want to navigate to next ID on Swipe screen. means -
if ID -- 3 is currently on screen and if user Swipe the screen then next ID detail will display on screen.
This process done till list box last id.
On the details page, can with the help of a FlipView navigate between several items. And every time the event SelectionChange occurs change the Id in ViewModel.
<toolkit:FlipView x:Name="FlipViewImages" ItemsSource="{Binding Images}" >
<toolkit:FlipView.ItemTemplate>
<DataTemplate>
<utils:ImageZoomControl Url="{Binding}"/>
</DataTemplate>
</toolkit:FlipView.ItemTemplate>
<toolkit:FlipView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</toolkit:FlipView.ItemsPanel>
</toolkit:FlipView>
The FlipView is not a native control, you have to import through the nuget Manager, Windows Phone ToolKit.
If your Item Source is a list of objects do not need to do anything else.
In a Xaml/C# WinRT application, whenever a ListViewItem is selected, the first letter on the left gets chopped off. How can I prevent this?
<ListView>
<ListViewItem Content="Hello World"/>
</ListView>
Unselected and Full:
Selected and Truncated:
You can apply a margin to an item within the ListViewItem itself like below, which will stop the cut off. Alternatively you could use a ContentTemplate as the second example.
Example 1:
<ListView>
<ListViewItem>
<TextBlock Text="Hello World" Margin="5,0,0,0"/>
</ListViewItem>
</ListView>
Example 2:
<ListView>
<ListViewItem Content="Hello World!">
<ListViewItem.ContentTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" Margin="5,0,0,0"/>
</DataTemplate>
</ListViewItem.ContentTemplate>
</ListViewItem>
</ListView>
As a follow up to Brandon Scott's answer, if you would rather not apply this inline, you can set this behavior as a style and then apply that style to individual list items like so
<Application.Resources>
<ResourceDictionary>
<Style x:Key="ListViewItemMagin" TargetType="ListViewItem">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding}" Margin="5,0,0,0"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Application.Resources>
Then, apply the style to your list view items
<ListView>
<ListViewItem Content="Hello World" Style="{StaticResource ListViewItemMagin}"/>
</ListView>
If you want to enable this by default, you can omit the x:Key property on the style and it will automatically apply to any element matching the TargetType; like so:
<Style TargetType="ListViewItem">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding}" Margin="5,0,0,0"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
From what I have read, there appears to be no functionality to achieve an underline for TextBlocks or HyperlinkButtons or the like in Windows RT, which appears ludicrous, but anyway, does anybody have an elegant approach to tackling this, specifically to create a link which runs a Click event or binding command?
As you can see out-of-the-box support doesn't appear to exist: http://social.msdn.microsoft.com/Forums/en-CA/winappswithcsharp/thread/cba0c363-60da-4e
This is how I've solved this problem before.
<HyperlinkButton x:Name="ExamplesLink" Click="ExamplesLink_Click"
Extensions:FrameworkElementExtensions.SystemCursor="Hand">
<TextBlock>
<Underline>
<Run Text="Examples"/>
</Underline>
</TextBlock>
</HyperlinkButton>
If you have the WinRT XAML toolkit you can set your cursor with the extension like above as well.
If you need (as in my case) to template the HyperlinkButton, keeping your bindings in your view, in you can do that in this way:
<Style TargetType="HyperlinkButton"
x:Key="StandardHyperlinkButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<TextBlock Style="{StaticResource BaseEntityDetailTextStyle}">
<Underline>
<Run Text="{Binding Path=Content, Mode=OneWay, RelativeSource={RelativeSource Mode=TemplatedParent}}" />
</Underline>
</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
...and then in your XAML view, for example:
<HyperlinkButton Style="{StaticResource StandardHyperlinkButton}"
Content="{Binding Path=myContent, Mode=OneWay}"
Command="{StaticResource myCommand}"
CommandParameter="{Binding Path=myContent, Mode=OneWay}" />
In this way you can also solve #sacha problem with bindings!
You can use a RichTextBlock with embedded HyperlinkButtons. If you look at RichTextBlockExtensions - you can use it to bind html text fragments (with anchor tags) to automatically populate a RichTextBlock with a linked text setup.
The problem with the above Hyperlinkbutton, AFAIK is that the Run Text is not bindable, so you end up having to repeat that TextBlock/Underline/Run pattern for every hyperlink button. Far better would be to make it a style.
For example if you try this, it will not work and an UnhandledException occurs
<HyperlinkButton Width="Auto" Height="Auto" Margin="2"
Content="{Binding DoctorName}"
Command="{Binding ElementName=scheduleView,
Path=DataContext.NavigateToAppointmentsDetailCommand}"
CommandParameter="{Binding DoctorName}">
<HyperlinkButton.Template>
<ControlTemplate>
<TextBlock>
<Underline>
<Run Text="{TemplateBinding Content}"/>
</Underline>
</TextBlock>
</ControlTemplate>
</HyperlinkButton.Template>
</HyperlinkButton>
The only way I found to fix this where the hyperlink text was also bindable was to use a fake underline rectangle and a regular button. I then show the underline (fake rectangle) based on whether the Pointer is over the button. Here is the relevant code:
Not the best solution but it is bindable for the content (link text), and its a generic solution.
<Button Width="Auto" Height="Auto" Margin="2"
Style="{StaticResource HyperLinkButtonStyle}"
Content="{Binding DoctorName}">
</Button>
<Storyboard>
<ObjectAnimationUsingKeyFrames
Storyboard.TargetProperty="(UIElement.Visibility)"
Storyboard.TargetName="rect">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates"/>
</VisualStateManager.VisualStateGroups>
<StackPanel Orientation="Vertical">
<TextBlock x:Name="txt"
Text="{TemplateBinding Content}"
HorizontalAlignment="Center"
FontFamily="Segoe UI" FontSize="18" FontWeight="Thin"/>
<Rectangle x:Name="rect" Fill="White" Height="2" Visibility="Collapsed"
VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>