I have something like this:
XAML (for one item):
<ListView Margin="10,0,10,10" Width="360" Height="510" VerticalAlignment="Bottom" RenderTransformOrigin="0.479,0.497">
<ListViewItem RenderTransformOrigin="0.719,0.534">
<StackPanel Orientation="Horizontal">
<Image Source="/Assets/1.png" Margin="0,0,5,0" />
<TextBlock FontSize="20" Width="302" RenderTransformOrigin="0.698,0.49" SelectionChanged="TextBlock_SelectionChanged" IsHoldingEnabled="False" IsDoubleTapEnabled="False">
<Run Text="Stotis–Oro uostas"/>
</TextBlock>
C#:
private void TextBlock_SelectionChanged(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(SecondPage));
}
I want to make like that: When I click on 1st item it should send me to another page.
You have to add tapped listener to the listview items like this:
<ListView Margin="10,0,10,10" Width="360" Height="510" VerticalAlignment="Bottom" RenderTransformOrigin="0.479,0.497">
<ListViewItem x:Name="ListViewItem1" Tapped="ListViewItem1_Tapped">
<!--add your image and text and ...-->
</ListViewItem>
<ListViewItem x:Name="ListViewItem2" Tapped="ListViewItem2_Tapped">
...
</ListViewItem>
<ListViewItem x:Name="ListViewItem3" Tapped="ListViewItem3_Tapped">
...
</ListViewItem>
</ListView>
Than you can add in the code behind file this:
private void ListViewItem1_Tapped(object sender, TappedRoutedEventArgs e)
{
this.Frame.Navigate(typeof(FirstPage));
}
private void ListViewItem2_Tapped(object sender, TappedRoutedEventArgs e)
{
this.Frame.Navigate(typeof(SecondPage));
}
private void ListViewItem3_Tapped(object sender, TappedRoutedEventArgs e)
{
this.Frame.Navigate(typeof(ThirdPage));
}
I hope this is like you wanted it.
Related
I am trying to detect which item in a listview is focused, but I am not getting the events detected. I am developing for Xbox One UWP, so I cannot use mouse or keyboard events, only focus can be used.
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" GotFocus="StackPanel_GotFocus" >
<StackPanel Name="Imagestack" Orientation="Horizontal">
<Image Source="{Binding Image}" Height="144" Width="256" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Title}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
private void StackPanel_GotFocus(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Image focus");
Image img = sender as Image;
Bgimage.Source = img.Source;
}
You should register to the ListView.GotFocus event.
The OriginalSource from the event parameter will be the ListViewItem which has received the focus. You can then retrieve the item content using ListViewItem.Content.
XAML:
<ListView x:Name="list" GotFocus="list_GotFocus">
<ListView.ItemTemplate>...</ListView.ItemTemplate>
</ListView>
Code behind:
private void list_GotFocus(object sender, RoutedEventArgs e)
{
var focusedItem = (e.OriginalSource as ListViewItem)?.Content;
}
You don't need to get focus state to get data from the clicked ListViewItem, the ItemClick event of the ListView may be what you're looking for:
<ListView x:Name="LV_Items"
IsItemClickEnabled="True"
ItemClick="LV_Items_ItemClick"
>
</ListView>
private void LV_Items_ItemClick(object sender, ItemClickEventArgs e)
{
// Get instance of the model in the clicked ListViewItem
MyModel myModel = (MyModel)e.ClickedItem;
Image img = myModel.Image;
}
I'm using:
<WebBrowser Height="387" VerticalAlignment="Top" Width="468" Name="Camera1" />
I'm have and button start in XAML:
<Button x:Name="startButton" Content="Start" Width="75" Height="20" Margin="4,0,0,0" Click="startButton_Click" />
In xaml.cs class I have method startButton_Click:
private void startButton_Click(object sender, RoutedEventArgs e)
{
Camera1.Navigate("http://96.10.1.168/mjpg/1/video.mjpg?timestamp=1470753862585");
}
How to make click on last element?
Explanation : user click on button and move mouse in mode of click to another button. So click should be executed for last button, not for first one.
In WPF I have tried all possible events to get this (MouseUp seems to be right choice for this, but this doesn't do what I expect).
How to resolve this problem?
UPD: 2 Buttons in xaml:
<Button Grid.Column="1" Grid.Row="3" Click="ButtonThigh_OnClick" >
<Grid Width="150" Height="75">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding Path=TrackingThighCircumference, Mode=OneWay}"/>
<TextBlock MouseRightButtonDown="Thigh_EditStarted" Height="65" Grid.RowSpan="2">
<TextBox Name="sizeThigh" Background="Transparent" Height="40" IsEnabled="False" ContextMenu="{x:Null}" ContextMenuService.IsEnabled="false" DataObject.Pasting="TextBox_Pasting" MaxLength="6" PreviewTextInput="TextBox_PreviewTextInput" LostFocus="TextBox_LostFocus" KeyUp="TextBox_KeyUp" Tag="ThighCircumferenceUI" Text="{Binding Path=ThighCircumferenceUI, Mode=TwoWay}"></TextBox>
<LineBreak/>
<TextBlock Name="ThighLb" Text="{x:Static strings:Resource.Thigh}"/><Span> (</Span><Span><TextBlock Text="{Binding ElementName=MeasurenentText, Path=Text}"/></Span><Span>)</Span>
</TextBlock>
</Grid>
</Button>
<Button Grid.Column="0" Grid.Row="4" Click="ButtonWaist_OnClick" >
<Grid Width="150" Height="75">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding Path=TrackingJacketWaistCircumference, Mode=OneWay}"/>
<TextBlock Grid.Row="1" MouseRightButtonDown="Waist_EditStarted">
<TextBox Name="sizeWaist" Background="Transparent" Height="40" IsEnabled="False" ContextMenu="{x:Null}" ContextMenuService.IsEnabled="false" DataObject.Pasting="TextBox_Pasting" MaxLength="6" PreviewTextInput="TextBox_PreviewTextInput" LostFocus="TextBox_LostFocus" KeyUp="TextBox_KeyUp" Tag="JacketWaistCircumferenceUI" Text="{Binding Path=JacketWaistCircumferenceUI, Mode=TwoWay}"></TextBox>
<LineBreak/>
<TextBlock Name="WaistLb" Text="{x:Static strings:Resource.Waist}"/><Span> (</Span><Span><TextBlock Text="{Binding ElementName=MeasurenentText, Path=Text}"/></Span><Span>)</Span>
</TextBlock>
</Grid>
</Button>
Button Clicks :
private void ButtonThigh_OnClick(object sender, RoutedEventArgs e)
{
_resultViewModel.SelectMeasurement(si => si.ThighCircumference, vm => vm.ThighImg);
}
private void ButtonWaist_OnClick(object sender, RoutedEventArgs e)
{
_resultViewModel.SelectMeasurement(si => si.JacketWaistCircumference, vm => vm.WaistImg);
}
You can try it on buttons:
private void button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("1");
}
private void button2_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("2");
}
private void Button1_OnPreviewMouseUp(object sender, MouseButtonEventArgs e)
{
button1_Click(sender,e);
}
private void Button2_OnPreviewMouseUp(object sender, MouseButtonEventArgs e)
{
button2_Click(sender,e);
}
private void Button1_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
private void Button2_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
After that, your PreviewMouseUp will work as you expect:
I am trying to optimize this code (for recurring procedures in C# (WPF)). Is there an opportunity to create one method, that does all this code? Just a bit smarter? I do not want to copy and paste this code into DoMyCode(). My problem is, that there are 20 ListViewItems I click. Every ListViewItem does the same (just with another Label).
private void listViewItem_Group0_Selected(object sender, RoutedEventArgs e)
{
label_Position.Content = label_Group0.Content.ToString();
SetDataGridItems(label_Group0.Content.ToString());
}
private void listViewItem_Group1_Selected(object sender, RoutedEventArgs e)
{
label_Position.Content = label_Group1.Content.ToString();
SetDataGridItems(label_Group1.Content.ToString());
}
private void listViewItem_Group2_Selected(object sender, RoutedEventArgs e)
{
label_Position.Content = label_Group2.Content.ToString();
SetDataGridItems(label_Group2.Content.ToString());
}
private void listViewItem_Group3_Selected(object sender, RoutedEventArgs e)
{
label_Position.Content = label_Group3.Content.ToString();
SetDataGridItems(label_Group3.Content.ToString());
}
private void listViewItem_Group4_Selected(object sender, RoutedEventArgs e)
{
label_Position.Content = label_Group4.Content.ToString();
SetDataGridItems(label_Group4.Content.ToString());
}
My XAML code looks like this:
<ListViewItem Selected="listViewItem_Group0_Selected">
<DockPanel>
<Image Source="/Apptivities;component/Images/interessenten_.png" Stretch="None" />
<Label Content="Interessenten Sponsoren" Name="label_Group0" />
</DockPanel>
</ListViewItem>
<ListViewItem Selected="listViewItem_Group1_Selected">
<DockPanel>
<Image Source="/Apptivities;component/Images/interessenten_.png" Stretch="None" />
<Label Content="Interessenten Teilnehmer" Name="label_Group1" />
</DockPanel>
</ListViewItem>
<ListViewItem Selected="listViewItem_Group2_Selected">
<DockPanel>
<Image Source="/Apptivities;component/Images/keine_kooperation.png" Stretch="None" />
<Label Content="Keine Kooperation" Name="label_Group2" />
</DockPanel>
</ListViewItem>
<ListViewItem Selected="listViewItem_Group3_Selected">
<DockPanel>
<Image Source="/Apptivities;component/Images/potenzielle_.png" Stretch="None" />
<Label Content="Potenzielle Sponsoren" Name="label_Group3" />
</DockPanel>
</ListViewItem>
<ListViewItem Selected="listViewItem_Group4_Selected">
<DockPanel>
<Image Source="/Apptivities;component/Images/potenzielle_.png" Stretch="None" />
<Label Content="Potenzielle Teilnehmer" Name="label_Group4" />
</DockPanel>
</ListViewItem>
Do you know, how to do this?
All your code can be easily converted in one method based on sender object
private void listViewItem_Group_Selected(object sender, RoutedEventArgs e)
{
ListViewItem lv = sender as ListViewItem;
DockPanel dockpanel = (lv.Content) as DockPanel;
Label label = (dockpanel.Children[1]) as Label;
label_Position.Content = label.Content.ToString();
SetDataGridItems(label.Content.ToString());
}
Change all the Selected events to a common event as "listViewItem_Group_Selected" and as the dockpanel is the child of ListViewItem, Label can be read by iterating through the dockpanel child controls.
You could reference all listviewitem selected events in your front-end (XAML/WinForm) to just one code-behind method and then distinguish between them using the sender object.
I'm trying to load data into a GridView after a TextBlock from another GridView on the page has been tapped/clicked. The first GridView containing the list of TextBlocks loads correctly.
Here is my XAML code for both GridViews, my Bindings seem to be correct:
<GridView x:Name="CourseNoGridView" Margin="50,50,0,0" Grid.Row="1" VerticalAlignment="Top" Height="568" ItemsSource="{Binding Distinct_CourseNo}" SelectionMode="Single" Padding="0,0,0,10" HorizontalAlignment="Left" Width="525" SelectionChanged="CourseNoGridView_SelectionChanged">
<GridView.ItemTemplate>
<DataTemplate>
<Border BorderBrush="White">
<TextBlock x:Name="CourseNoTextBlock" Text="{Binding CourseNo}" TextWrapping="NoWrap" FontSize="24" Width="200" Height="Auto" Padding="10" Tapped="CourseNoTextBlock_Tapped"/>
</Border>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
<GridView x:Name="SectionsGridView" Margin="580,50,0,0" Grid.Row="1" VerticalAlignment="Top" Height="568" ItemsSource="{Binding Clicked_CourseNo_Sections}" SelectionMode="Single" Padding="0,0,0,10" HorizontalAlignment="Left" Width="776" SelectionChanged="CourseNoGridView_SelectionChanged">
<GridView.ItemTemplate>
<DataTemplate>
<Border BorderBrush="White">
<TextBlock x:Name="SectionTextBlock" Text="{Binding Get_Section}" TextWrapping="NoWrap" FontSize="24" Width="200" Height="Auto" Padding="10"/>
</Border>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
Here is my code for handling the clicking/tapping of an item in the first GridView:
private void CourseNoGridView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
clickedSection = (Sections)e.AddedItems[0];
}
private void CourseNoTextBlock_Tapped(object sender, TappedRoutedEventArgs e)
{
this.Clicked_CourseNo_Sections = (from s in Roster_Sections
where s.CourseNo.Equals(clickedSection.CourseNo)
select s).ToList();
}
What you want to do is use an ObservableCollection and bind your your Grid View to this. Then in your "Tapped" event handler you clear the existing items from this collection and add the new items.
Something like this:
private readonly ObservableCollection<Sections> currentSections = new ObservableCollection<Sections>();
//This is what we bind to
public ObservableCollection<Sections> CurrentSections { get { return currentSections; } }
private void CourseNoGridView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
clickedSection = (Sections)e.AddedItems[0];
}
private void CourseNoTextBlock_Tapped(object sender, TappedRoutedEventArgs e)
{
var courseSections = (from s in Roster_Sections
where s.CourseNo.Equals(clickedSection.CourseNo)
select s);
CurrentSections.Clear();
CurrentSections.AddRange(courseSections);
}
There's some documentation here:
http://msdn.microsoft.com/en-us/library/windows/apps/hh758320.aspx
It seems like adding the last line of code below fixed the problem.
private void CourseNoTextBlock_Tapped(object sender, TappedRoutedEventArgs e)
{
this.Clicked_CourseNo_Sections = (from s in Roster_Sections
where s.CourseNo.Equals(clickedSection.CourseNo)
select s).ToList();
SectionsGridView.ItemsSource = Clicked_CourseNo_Sections;
}