UWP: UserControl override GridView item click - c#

I have a UserControl inside of each GridView item. It's like below.
<GridView Name="SymbolItemsGridView"
Grid.Column="0"
Background="#333333"
SelectionMode="None"
IsItemClickEnabled="True"
ItemsSource="{x:Bind Items}"
HorizontalAlignment="Stretch"
Margin="15,5,10,15"
ItemClick="SymbolGridView_ItemClick">
<GridView.ItemTemplate>
<DataTemplate x:DataType="data:SymbolItem">
<local:SymbolControl Margin="10"/>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
The SymbolControl above is a UserControl, I changed its ContentPresenter style to round
<ContentPresenter CornerRadius="40"
Height="40"
Width="40"
Background="Cyan"
x:Name="ContentPresenter"
Content="{TemplateBinding Content}"
Padding="0" />
So the only one button inside this UserControl is of round shape.
<Button Name="SymbolButton"
Content="{x:Bind PhoneticSymbolItem.Text}"
Style="{StaticResource VowelSymbolButton}" />
My question is that the ItemClick="SymbolGridView_ItemClick" event handler only works when I click the corner of the GridView Item (the black part above). If I click the round button, it doesn't work.
I know it's probably because the button is on top of the GridView item and clicking the button only triggers the button click event. But the media resource I want to set is in ItemsSource="{x:Bind Items}" of the GridView. I don't know how to pass it to my UserControl and used by the button click event.
Or if there's a way to pop up the button click event to GridView item, it should work as well. I hope my question is clear, any ideas?

Try IsHitTestVisible="False" to your round SymbolControl .

Related

set SelectionChanged in ListView Clickmode = "pressed" in uwp

I have a ListView and i want ListView to be selected when Mouse pressed. (Like ClickMode property of Buttons). is there any solution ?
I tried to put button in ListView template and change its ClickMode property
The SelectionChanged is triggered when mouse released which is by default. It seems like we cannot change it. So we can put button in ListView item template and change its click mode as what you thought about, or register a PointerPressed event for the container inside item template which will be triggered when mouse pressed not released.
but i couldn't access object that is clicked in the ListView
To access which item is clicked you still can use property like SelectedItem of ListView to get the selected item. For example:
XAML Code
<ListView Name="CategoryLIstView" Grid.Row="1" ItemsSource="{x:Bind categories}" HorizontalAlignment="Center" Margin="10" VerticalAlignment="Top" IsItemClickEnabled="True" SelectionChanged="CategoryLIstView_SelectionChanged" ItemClick="CategoryLIstView_ItemClick" PointerPressed="CategoryLIstView_PointerPressed">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:Category">
<StackPanel Margin="0" PointerPressed="StackPanel_PointerPressed">
<StackPanel Orientation="Vertical" >
<TextBlock Text="{x:Bind Name}" Foreground="Blue" FontWeight="Bold" FontFamily="Yu Gothic" FontSize="17"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Code behind:
private async void StackPanel_PointerPressed(object sender, PointerRoutedEventArgs e)
{
await new Windows.UI.Popups.MessageDialog("point press").ShowAsync();
System.Diagnostics.Debug.WriteLine(CategoryLIstView.SelectedIndex);
}

SlectedItem when I click in a textbox or button in Datatemplate of a listview

I have a textbox and a button in a datatemplate as follows:
<DataTemplate x:Key="MyTemp1" x:DataType="data:Test">
<StackPanel Width="400">
<TextBox x:Name="tBoxName" Text="{Binding PhoneNumebr, Mode=TwoWay}" />
<Button x:Name="myButton" Content="textbutton" Click="myButtonButton_Click"/>
</StackPanel>
</DataTemplate>
and here my listView:
<ListView x:Name="myList" ItemTemplate="{StaticResource MyTemp1}"
SelectionChanged="myList_SelectionChanged"
IsItemClickEnabled="False"/>
for a listView in an UWP app, so I need a solution, that when I select the textbox or button, then selecteditem is accrued. In fact selectionchanged event is not fired when textbox or button is selected. is there any way to get selected item when textbox or button is selected? thanks
My problem has been solved as MikeBMcL explaind at this ilink:
https://social.msdn.microsoft.com/Forums/windowsapps/en-US/a5191023-fcdd-4e94-924e-6dee8e888267/uwp-xaml-button-on-list-item?forum=wpdevelop

How to open Popup by clicking on grid view item in windows store app?

I want to open popup by clicking on button in gridView element, inside popup I have three options by clicking on that option I want to navigate to another page with the id of the element
i want to open popup on click on button inside the gridView element my code looks like :-
<GridView x:Name="Test" Grid.Row="1" Margin="20,20,20,20" >
<GridView.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Left" Width="250" Height="250">
<Button Background="Red" HorizontalAlignment="Left" VerticalAlignment="Top" Height="100" Content="OpenPopup" Click="Button_PointerPressed"></Button>
<Popup x:Name="Mypopup">
<TextBlock Text="hi"/>
</Popup>
<StackPanel VerticalAlignment="Bottom" Background="{ThemeResource ListViewItemOverlayBackgroundThemeBrush}">
<TextBlock Text="{Binding Title}" Foreground="{ThemeResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextBlockStyle}" Height="60" Margin="15,0,15,0"/>
<TextBlock Text="{Binding Subtitle}" Foreground="{ThemeResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource CaptionTextBlockStyle}" TextWrapping="NoWrap" Margin="15,0,15,10"/>
</StackPanel>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
on click event
private void Button_PointerPressed(object sender, RoutedEventArgs e)
{
Mypopup.isopen = true;
}
Error:-The name 'Mypopup' does not exist in the current context
I am a newbie.. so help me
All you have to do is on the Onclick event of your gridview's button do the following code :
popup.isopen = true
Then in your popup if you want to navigate to another page all you have to do is use Navigate on the proper event
Frame.Navigate(typeof(YourPage),YourID);
EDIT : If the buttons in your gridview are added through code and you want to do the event on those then make sure that they have different names such as : GrdButton1/GrdButton2 and then give them an event dynamicly
If they are added in the XAML then simply add it an event there
<Button x:Name="ButtonTest"
Click="ButtonTest_Click"/>
If none of this answers your question please add more details to it

How to show particular textblock on button click in WPF?

i am working on an application using C# WPF and MVVM.The problem i want to show summary on button click event in textblocks and textblocks are inside ListBox as follows:
<ListBox>
<TextBlock
TextWrapping="Wrap"
Height="350"
Text="{Binding ShowVlan}"
</TextBlock>
<TextBlock
TextWrapping="Wrap"
Height="350"
Text="{Binding ShowRouting}">
</TextBlock>
</ListBox>
I have two buttons and i want that if i click 1st button then 1st textblock should be display and on second button click i want to display second textblock .It is working now but the actual problem is that i want to display to on same positions but these are displaying one after another .I am also attaching screenshot for better understanding.
You could simply put both TextBlock at the same Grid Cell and set their Visibility to true/False based on the selected Button :
<TextBlock x:Name="ShowVlanTb" Visibility="Hidden"
TextWrapping="Wrap"
Height="350"
Text="{Binding ShowVlan}" Grid.Row=1 Grid.Colomn=2
/>
<TextBlock x:Name="ShowRoutingTb" Visibility="Hidden"
TextWrapping="Wrap"
Height="350"
Text="{Binding ShowRouting}" Grid.Row=1 Grid.Colomn=2>
/>
and on the button Click event handler set the Visiblity to Visible:
ShowVlanTb.Visibility=Visibility.Visible
ShowRoutingTb.Visibility=Visibility.Hidden

How can get the value of Radio Button Inside Listbox Click on Button in Windows Phone Using C#

I have Radio Group of Three Radio Button and a submit button also
inside of ListBox. I want to get the selected Radio Button Value from
the Radio Group on Button Click. Below my listbox item view. please
help me.
<ListBox x:Name="OptionListView" SelectionChanged="OptionListView_SelectionChanged" Height="720" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,0,0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Background="#99000000" Width="480" Margin="0,10,0,0">
<StackPanel Margin="10">
<TextBlock Text="{Binding FeaturedQuestion}" TextWrapping="Wrap" />
<RadioButton Content="Yes" GroupName="OpinionPoll" Foreground="White"/>
<RadioButton Content="No" GroupName="OpinionPoll" Foreground="White"/>
<RadioButton Content="Not Decide" GroupName="OpinionPoll" Foreground="White"/>
<Button x:Name="btnSubmit" Click="btnSubmit_Click" Background="#d44740" Content="Submit" Style="{StaticResource TileButtonTemplate}" Height="65" VerticalAlignment="Top" HorizontalAlignment="Left" Width="200" Foreground="White"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
When you work with a listbox you need to have a List of something like a object.
This object represent your 3 radio button. For exemple you can work with an enum for get the checked value or create a custom radio button with 3 states.
Steps:
- understand how to use and bind control inside listbox
understand how to get the checked value (Exemple => IsChecked="{Binding IsSelected, Mode=TwoWay}"/>)
You could simply access the properties, by binding the ListBox's selected item.
Reference: how do I access a radiobutton inside a ListBoxItem in windows phone 7

Categories