I've got a Grid which contains some objects and one of them is a Button.
The button has its own Command binding and everything's fine.
I've added a binding to the Grid's tap event but now its Command gets executed even if I tap on the Button.
Since I'm using MVVM Light, I've got the possibility to use PassEventArgsToCommand to get a GestureEventArgs in my code and check the source of the event before actually doing anything.
The problem is that this parameter is always null in my method, so I can't do anything with it.
Is there any way to exclude my Button from executing this Command?
Something like a CanExecute function that returns false for the Button type.
EDIT:
XAML code for the Grid
<Grid Height="100" Margin="15,0,15,17" Tag="{Binding ServiceId}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tap">
<cmd:EventToCommand Command="{Binding Path=DetailsCommand}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Image Grid.Row="0" Grid.RowSpan="3" Grid.Column="0" Source="{Binding Poster}" Stretch="UniformToFill" Height="100" Width="100"/>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Title}" HorizontalAlignment="Center" />
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Directors[0]}" HorizontalAlignment="Center" />
<ListBox Grid.Row="2" Grid.Column="1" ItemsSource="{Binding Genres}" HorizontalAlignment="Center"/>
<TextBlock Grid.Row="0" Grid.Column="2" Text="{Binding Year}" HorizontalAlignment="Center" />
<Button Grid.Row="1" Grid.Column="2" Grid.RowSpan="2" BorderThickness="0" Style="{StaticResource StarButton}" Command="{Binding SelectCommand}"/>
</Grid>
Related
I am new to WPF and suddendly my Window is not clickable anymore.
I cannot click one button and I cannot edit a text box.
I am using Visual Studio 2015 (new to it as well).
What could be the reason?
here some code:
<Window x:Class="Mxx.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:Mxx"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="26*"/>
</Grid.RowDefinitions>
<Menu>
<MenuItem Header="Log out" Click="MenuItem_Click1"/>
<MenuItem Header="Option 2">
<MenuItem Header="Load new control" Click="MenuItem_Click2a"/>
<MenuItem Header="Open new window" Click="MenuItem_Click2b"/>
</MenuItem>
</Menu>
<Border x:Name="Stage" Grid.Row="1"/>
<Grid x:Name="LoginLayer" Background="#FFFFFFFF" Grid.RowSpan="2">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center" Background="LightBlue">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="Mxx" Grid.ColumnSpan="2" FontWeight="Bold" HorizontalAlignment="Center" Margin="5" FontSize="20"/>
<TextBlock Text="Name" Grid.Row="1" Margin="3"/>
<TextBox x:Name="txtName" Grid.Row="1" Grid.Column="1" Margin="3" MinWidth="100" HorizontalAlignment="Left"/>
<TextBox x:Name="txtName1" Background="Azure" Grid.Row="4" Grid.Column="1" Margin="3" MinWidth="100" HorizontalAlignment="Left"/>
<TextBlock Text="Password" Grid.Row="2" Margin="3"/>
<PasswordBox x:Name="txtPassword" Grid.Column="1" Grid.Row="2" Margin="3" MinWidth="100" HorizontalAlignment="Left"/>
<Button Click="Login_Click" Content="Log in" Grid.Row="3" Grid.Column="1" Margin="3"/>
</Grid>
<TextBlock x:Name="lat1" Text="lat" Grid.Row="4" Margin="3" />
</Grid>
</Grid>
and here my main window
public MainWindow()
{
InitializeComponent();
_geoLocator = new Geolocator();
Utils.getNetworkAdapterId();
Debug.WriteLine("imei: " + Properties.Settings.Default.imei);
Properties.Settings.Default.imei = "imei1";
Properties.Settings.Default.Save(); // Saves settings in application setting
}
my debugger hits the last line
Your problem is with the last declared TextBlock
<TextBlock x:Name="lat1" Text="lat" Grid.Row="4" Margin="3" />
That is outside of the grid that has row and column definitions, and so it is defaulting to taking up all the space in the page as an overlay. You are unable to give focus to the controls beneath it.
You can see this in the XAML editor window. In the code view, click on that element, and in the designer you will see that the textblock is the full size of your window. Because it is declared last in the XAML, it take the uppermost position (z-index) and therefore "hides" the controls behind it.
You have a TextBlock covering the other controls. I think you want to move lat1 into the /Grid tag directly above it.
I'm developing windows phone 8.1 app, and I need to get value of textboxes named:"time_tb" and "task_text_tb", that are in my selected item. This is my Xaml code:
<ListBox x:Name="list" ItemsSource="{Binding tasks}" Background="White" Margin="-1,50,-1,63">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="1" Height="76" Tapped="Grid_Tapped" Width="389">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="83*" />
<ColumnDefinition Width="307*" />
</Grid.ColumnDefinitions>
<TextBox x:Name="time_tb" Margin="0,15,276,0" TextWrapping="Wrap" Text="{Binding time}" VerticalAlignment="Top" BorderBrush="#FF49B7DC" Width="83" Height="45" FontSize="18.667" Foreground="{x:Null}" Background="White" FontFamily="Segoe UI Emoji" IsReadOnly="True" TextAlignment="Center" HorizontalAlignment="Right" Grid.ColumnSpan="2"/>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock x:Name="task_text_tb" Margin="63,14,10,39.833" HorizontalAlignment="Stretch" TextWrapping="Wrap" Text="{Binding wht}" FontSize="16" SelectionHighlightColor="#FF1455B3" Grid.RowSpan="2" />
</Grid>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<StackPanel Height="100" Width="100"/>
</ListBox>
In your viewmodel, keep a property for the SelectedItem (binded to your list selectedItem) and 2 property for the textboxes of the current selected item that you will update when SelectedItem is changed. Then bind those 2 properties to where you want to show the values.
You may have to do some magic to find the actual selected item, because i think selected item only returns an index or something...
Hope this helps
Is there is any way to get grid view item after double click. I have not found double click event in metro gird view. Only events like ItemClick and PointerPressed are there. Any suggestion?
<GridView Name="downloadGrid"
HorizontalAlignment="Left"
ItemsSource="{Binding Source={StaticResource CollectionItems}}"
Grid.Column="0" Grid.Row="2" PointerPressed="downloadGrid_PointerPressed"
ItemContainerStyle="{StaticResource CustomGridViewItemStyle}"
Width="Auto" SelectionChanged="downloadGrid_SelectionChanged">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.ItemTemplate>
<DataTemplate>
<Grid Margin="6" Height="280" Width="200">
<Grid.RowDefinitions>
<RowDefinition Height="120"/>
<RowDefinition Height="35"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Image Source="{Binding Path=Thumbnail}" HorizontalAlignment="Left" Grid.Row="0" Stretch="Fill"/>
<Image Grid.Row="0" Source="{Binding Path=Type, Converter={StaticResource Icon}}" MaxWidth="20" MinWidth="20" VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
<TextBlock Text="{Binding Path=Caption}" TextWrapping="Wrap" Foreground="#FF017DD5" Grid.Row="1" HorizontalAlignment="Left" FontSize="11" Margin="3,0,0,0" Height="Auto"/>
<ProgressBar Grid.Row="2" Value="{Binding Path=PercentOfCompletion, Converter={StaticResource ProgressConverter}}" Width="75" Height="11" HorizontalAlignment="Left" VerticalAlignment="Center" FontFamily="Segoe UI" BorderThickness="1" Foreground="#FF78D200" Margin="3,0,0,0">
<ProgressBar.BorderBrush>
<SolidColorBrush Color="#FF78D200"/>
</ProgressBar.BorderBrush>
</ProgressBar>
<!--<TextBlock Grid.Row="2" Text="{Binding Path=PercentOfCompletion, Converter={StaticResource ProgressConverter}}" Margin="-15,0,15,0" VerticalAlignment="Center" HorizontalAlignment="Center" FontFamily="Segoe UI" Foreground="#333333" FontSize="12"/>-->
<TextBlock Grid.Row="2" HorizontalAlignment="Right" VerticalAlignment="Center" Text="{Binding Path=StatusInfo}" Style="{StaticResource MainPageTextStyle}" Margin="0,0,5,0"/>
<Button x:Name="playNow" Grid.Row="3" Content="Play Now" HorizontalAlignment="Left" VerticalAlignment="Center" Style="{StaticResource DownloadButtonStyle}" Click="playNow_Click" Margin="3,0,0,0" IsEnabled="{Binding IsPlay}"/>
<Button x:Name="pauseResumeBUtton" Grid.Row="3" Content="Pause" HorizontalAlignment="Right" VerticalAlignment="Center" Style="{StaticResource DownloadButtonStyle}" Click="pauseResumeBUtton_Click" Margin="0,4,3,2" IsEnabled="{Binding IsResume}"/>
<TextBlock Grid.Row="4" HorizontalAlignment="Left" VerticalAlignment="Center" Text="Size: " Style="{StaticResource MainPageTextStyle}" Margin="5,0,0,0"/>
<TextBlock Grid.Row="4" HorizontalAlignment="Right" VerticalAlignment="Center" Text="{Binding Path=OriginalFileSize, Converter={StaticResource FileSizeConverter}}" Style="{StaticResource MainPageTextStyle}" Margin="0,9,5,6"/>
<!--<TextBlock Grid.Row="5" HorizontalAlignment="Left" VerticalAlignment="Top" Text="Output: " Style="{StaticResource MainPageTextStyle}" Margin="5,0,0,0"/>
<TextBlock Grid.Row="5" HorizontalAlignment="Right" VerticalAlignment="Top" Text="iPhone / iPod" Foreground="#FF017DD5" FontSize="12" Margin="0,2,5,0"/>-->
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
The problem with using a Tapped (or DoubleTapped) event is that these events only fire when it detects a Tap gesture. "Well, yeah. That's exactly what I want", you may say.
Buuuuuut if you are using a mouse and you click on your item and the mouse moves a little bit (or a lot) before you release your finger, then that event is not recognized as a Tap. So that event will not fire. This can lead to the user not understanding why his clicks are not being recognized. This can be reproduced easily and consistently.
Potential Hacky Solution
The only way I can currently think of implementing what you would to do is by using the ItemClick event. When a GridViewItem is clicked, start a timer. The next time the ItemClick event fires, check if the same item was clicked within a reasonable amount of time (500ms? Less? I'm sure this is a quick Google away).
Of course you have to make sure you take care of stopping the timer and all that stuff.
As VasileMarianFălămaș wrote in the comments to your question you can use the double tapped event.
however you should question yourself if you want double tap events on a grid item. One of the best Windows 8 features is that app apps function the same way. Users shouldn't have to think if they should double tap or just tap in this application.
What action do you want to do when double tapping? opening the item? this is normally done by single tap.
here is an overview of when which gestures are used in Windows 8 apps:
http://msdn.microsoft.com/en-us/library/windows/apps/hh761498.aspx
I have a ListBox bound to some data, all with an ItemTemplate set, I want to fire PointerPressed Event from this list by pressing anywhere in the ListBox area (cause I just need that for some purpose), but apparently the Selection of the items is preventing that, (I'm using commands) here's my code
<ScrollViewer x:Name="sv"
x:FieldModifier="public"
VerticalScrollBarVisibility="Visible"
VerticalScrollMode="Enabled"
HorizontalScrollBarVisibility="Disabled"
HorizontalScrollMode="Disabled">
<ListBox x:Name="lb"
ItemsSource="{Binding Path=Tweets}">
<WinRTBehaviors:Interaction.Behaviors>
<Win8LnBehaviors:EventToCommandBehavior Event="PointerPressed"
Command="svPointerPressed"
PassEventArgsToCommand="True" />
</WinRTBehaviors:Interaction.Behaviors>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Height="65">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="65"></ColumnDefinition>
<ColumnDefinition Width="2*"></ColumnDefinition>
<ColumnDefinition Width="3*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="2*"></RowDefinition>
</Grid.RowDefinitions>
<Image x:Name="Img_ProfilePicture"
Grid.Row="0"
Grid.Column="0"
Grid.RowSpan="2"
Stretch="Fill"
Source="{Binding ProfilePictureSource}"
Margin="2">
</Image>
<TextBlock x:Name="Tb_ProfileName"
Grid.Row="0"
Grid.Column="1"
Text="{Binding UserName}"
Margin="5,0,0,0"
FontFamily="Segoe UI Mono"
FontSize="12"
FontWeight="Bold" />
<TextBlock x:Name="Tb_FeedTime"
Grid.Row="0"
Grid.Column="2"
Text="{Binding StatusDateTime}"
Margin="5,0,0,0"
FontFamily="Segoe UI Light"
FontSize="10" />
<TextBlock x:Name="Tb_FeedData"
Grid.Row="1"
Grid.Column="1"
Grid.ColumnSpan="2"
Text="{Binding Status}"
Margin="10,0,0,0"
FontFamily="Wasco Sans"
TextWrapping="Wrap">
</TextBlock>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
The code behind :
public RelayCommand<RoutedEventArgs> svPointerPressed
{
get
{
return new RelayCommand<RoutedEventArgs>((routedEventArgs) =>
{
_dispatcher = Window.Current.Dispatcher;
_dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
{
MessageDialog m = new MessageDialog("Tapped !");
m.ShowAsync();
});
});
}
private set{}
}
I even tried to fire the PointerPressed event from one of the components like those TextBoxes but still not firing.
I'd be so thankful , thx
I'd go and just give up at this point. A ScrollViewer filters out pointer events and you will be better off figuring out an alternative design.
I've got a few ListBoxItems that have an image and a textbox in them which highlights when clicked. What I'm having trouble figuring out is how to make whole listbox item doubleclick event fire to a hyperlink. Can someone assist me in this?
I've been looking at this but it seems that it is for the listbox as a whole rather than an item -- http://jarloo.com/code/wpf/wpf-listbox-doubleclick/.
Here is one of my listboxitems:
<ListBoxItem >
<Grid HorizontalAlignment="Stretch">
<Grid Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Border Grid.RowSpan="2"
BorderBrush="LightGray" BorderThickness="0"
Margin="0,0,5,0">
<Image Source="/IDE;component/Resources/Images/test1.ico" Height="64" Width="64" />
</Border>
<TextBlock Text="Google.com"
FontWeight="Bold"
Grid.Column="2"
Margin="0,0,0,5"/>
<TextBlock TextWrapping="Wrap" Text="To learn more information doubleclick this box to open the website."
Grid.Column="2" Grid.Row="2"/>
</Grid>
<Line X1="0" Y1="0" X2="0" Y2="0" Stretch="Uniform"
Stroke="DarkGray"
VerticalAlignment="Bottom"/>
</Grid>
</ListBoxItem>
You seem to be specifying concrete items rather than having them generated from an ItemsSource, the link you named does not apply. I don't quite understand what you mean when you refer to a Hyperlink since i cannot see any in your code.
To handle a normal double click you could assign a handler in the ListBoxItem itself:
<ListBoxItem MouseDoubleClick="ListBoxItem_DoubleClick">
...
Is that what you want?