Go to previous Tab using button in WPF - c#

How to go previous tab if I click back button?
XAML
<Button x:Name="btnBack" Margin="239,418,333,11 Click="btnBack_Click">
<Button.Template>
<ControlTemplate>
<Image x:Name="backImg" Source="Images/back.png" />
</ControlTemplate>
</Button.Template>
</Button>
Code
private void btnBack_Click(object sender, RoutedEventArgs e)
{
}
Tab item
<TabControl HorizontalAlignment="Left" Height="403" BorderThickness="0"
Background="Transparent" Margin="10,10,0,0"
VerticalAlignment="Top" Width="622">
<TabItem Header="List" Margin="0,0,-19,0">
//object
</TabItem>
<TabItem Header="Register" Margin="0,0,-19,0">
</TabItem>
//object
</TabControl>
It is possible to do that?

In the TabControl object you have a SelectedIndex that you can use to navigate to any element within the list. Try something like the following:
private void btnBack_Click(object sender, RoutedEventArgs e)
{
if (tabControl.SelectedIndex == 0)
{
return;
}
else
{
tabControl.SelectedIndex -= 1;
}
}
In this case we are calling the tab control, checking if we can reverse in the items it has and if we can we will go backward by 1 item.
Keeping in mind that your XAML TabControl needs to be given a name to be called by from the code behind:
<TabControl x:Name="tabControl">

Related

Changing Text value of TextBlock embedded within a Control Template

I have a TextBlock that is inside a Control Template. I want to change the Text for said TextBlock with the Text value of a TextBox. The value is meant to be set within a button click event however, with the way I've tried to do this it doesn't work. The click event will give out an error stating that text is null.
I am new to WPF and would appreciate any help.
XAML for Control Template:
<Window.Resources>
<ControlTemplate x:Key="panel" TargetType="Button">
<Grid>
<Rectangle x:Name="rectangle" Width="auto" Height="55" RadiusX="10" RadiusY="10"
Fill="White">
</Rectangle>
<TextBlock x:Name="txtBlk" Text="" Margin="10,10,0,0" />
</Grid>
</ControlTemplate>
</Window.Resources>
C# for Button_Click event:
private void panelBtn_Click(object sender, RoutedEventArgs e)
{
var text = (TextBlock)this.Template.FindName("txtBlk", this);
text.Text = txtBox.Text;
}
You should reference the template of the button like this..
private void panelBtn_Click(object sender, RoutedEventArgs e)
{
if (sender is Button btn)
{
var text = btn.Template.FindName("txtBlk", btn) as TextBlock;
text.Text = txtBox.Text;
}
}
#MuhammadSulaiman answered you correctly, but I would suggest that you change the implementation.
Rather than looking for an element in a template, it's better to add a resource to which this element will refer and change this resource.
<Window.Resources>
<sys:String x:Key="button.Text">Some Text</sys:String>
<ControlTemplate x:Key="panel" TargetType="Button">
<Grid>
<Rectangle x:Name="rectangle" Width="auto" Height="55" RadiusX="10" RadiusY="10"
Fill="White">
</Rectangle>
<TextBlock x:Name="txtBlk"
Text="{DynamicResource button.Text}"
Margin="10,10,0,0" />
</Grid>
</ControlTemplate>
</Window.Resources>
private void panelBtn_Click(object sender, RoutedEventArgs e)
{
if (sender is FrameworkElement elm)
{
elm.Resources["button.Text"] = txtBox.Text;
}
}
You can also change the initial text in XAML:
<Button Template="{DynamicResource panel}">
<Buttun.Resources>
<sys:String x:Key="button.Text">Other Text</sys:String>
</Buttun.Resources>
</Buttun>
In the same way, you can set a common initial text for all buttons located in one common container, through the resources of this container.

XAML detect if listview item is focused or not

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;
}

Wpf: Update content using scroll position

You have a stackpanel with many buttons arranged vertically. The buttons contain either a date or a time. They are intermingled: "Monday", "1pm", "7pm", "Tuesday", "3pm", "11pm", "Wednesday". You can scroll down the list of buttons with a scrollviewer.
Tricky part: The top button must always show a day (i.e "Tuesday") not a time. This is to know which day the times belong to as you scroll down. For example: If you scrolled down one button the example would read: "1pm", "7pm", "Tuesday", "3pm", "11pm", "Wednesday". And you wouldn't know what day 1pm belonged to anymore without scrolling up.
Here is what I have so far:
XAML:
<DockPanel>
<Button DockPanel.Dock="Top" Content="Monday" Background="Red"/>
<Grid>
<ScrollViewer Name="sv1" CanContentScroll="True" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
<StackPanel Name="sp1">
<Button Content="1pm"/>
<Button Content="7pm"/>
<Button Content="Tuesday" Background="Red"/>
<Button Content="3pm"/>
<Button Content="11pm"/>
<Button Content="Wednesday" Background="Red"/>
</StackPanel>
</ScrollViewer>
</Grid>
</DockPanel>
C#
public partial class myWindow : Window
{
public myWindow()
{
InitializeComponent();
}
//"Logical scrolling"
private void spLineUp(object sender, RoutedEventArgs e)
{
((IScrollInfo)sp1).LineUp();
}
private void spLineDown(object sender, RoutedEventArgs e)
{
((IScrollInfo)sp1).LineDown();
}
}
How would you change the content using the scroll position?
Any help appreciated!
I found a property called "VerticalOffset" that keeps track of the row in logical scrolling. Knowing the row number then allowed updating the content.
C#
private void sChanged(object sender, ScrollChangedEventArgs e)
{
TopButton.Content = e.VerticalOffset.ToString();
}
XAML
<Button DockPanel.Dock="Top" Content="Date" Background="Red" Name="TopButton"/>
<Grid>
<ScrollViewer Name="sv1" VerticalScrollBarVisibility="Visible" CanContentScroll="True" ScrollChanged="sChanged">
<StackPanel Name="sp1">
</StackPanel>
</ScrollViewer>
</Grid>

Accessing Silverlight Control template items codebehind

I have a control template in page Layout as following.
<Grid x:Name="LayoutRoot">
<Grid.Resources>
<ControlTemplate x:Key="myTemplate" TargetType="esri:MapTip">
<Border CornerRadius="10" Background="#DDFFEEEE" BorderThickness="4" BorderBrush="#99FF0000">
<StackPanel Background="#DDFFFFFF">
<sdk:TabControl Height="180" Margin="5" Name="tabControl1" Width="300">
<sdk:TabItem Header="Info" Name="infoTab">
<TextBlock x:Name="cityInfoTxt" Tag="{Binding [City_ID]}"/>
</sdk:TabItem>
</sdk:TabControl>
</StackPanel>
</Border>
</ControlTemplate>
In code behind how can I access the ??
I tried this,
private void button1_Click(object sender, RoutedEventArgs e)
{
var te = this.LayoutRoot.Resources["myTemplate"] as ControlTemplate;
}
but can not access the textblock in the tab control.
override the method OnAplyTemplate() on code behind and try to find your component.
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
var myTextBlock = GetTemplateChild("cityInfoTxt") as TextBlock;
}
Hope it helps

make data grid visible on click

I have this data grid where I am placing all my buttons
<Grid x:Name="ButtonGrid" HorizontalAlignment="Left" Margin="0,90,0,4" Width="186">
<Button x:Name="B1" Content="B1" Height="18" Margin="73,0,59,16" VerticalAlignment="Bottom" Click="B1"/>
<Button x:Name="B2" Content="B2" Height="18" Margin="0,0,-2,16" VerticalAlignment="Bottom" Click="B2_Click" HorizontalAlignment="Right" Width="57"/>
</Grid>
I have the grid collapased on start. But when a button {testGrid} is clicked, I want the grid to ne visible.
Here is my code
namespace project.Test
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
EDUTED
private void testGrid_Click(object sender, System.Windows.RoutedEventArgs e)
{
FrameworkElement ButtonGrid = (sender as FrameworkElement).FindName("ButtonGrid") as FrameworkElement;
if ( ButtonGrid.Visibility == System.Windows.Visibility.Collapsed)
ButtonGrid.Visibility = System.Windows.Visibility.Visible;
else
ButtonGrid.Visibility = System.Windows.Visibility.Collapsed;
}
}
}
I think if you move your Grid outside of your DataTemplate it will work. :)
However if you really need to put it in a DataTemplate, as long as your Button is at the same level as the Grid, you should still be able to find it.
Say your xaml code looks like this,
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="controlstoryboardactionrefissue.MainPage" Width="640" Height="480">
<UserControl.Resources>
<DataTemplate x:Key="DataTemplate1">
<Grid x:Name="myGrid" Height="128" Background="#FFE7C0C0" Width="333">
<Button x:Name="myButton" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="31,29,0,0" Click="myButton_Click" />
</Grid>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<ContentControl HorizontalAlignment="Left" VerticalAlignment="Top" Margin="175,198,0,0" ContentTemplate="{StaticResource DataTemplate1}" />
</Grid>
</UserControl>
Then the code behind,
private void myButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
var myButton = (Button)sender;
var grid = myButton.Parent as Grid;
if (grid != null)
{
// do stuff
}
}
Hope it helps. :)

Categories

Resources