i am developing app for windows phone. i am using flipview to show detail. this flipview contains three controls in it and changing view using swipe. i want to change the view on click of button.
See my Code :
<FlipView x:Name="flip" Grid.Row="1" Grid.ColumnSpan="4" Grid.Column="0" Margin="0,0,0,0.333" Grid.RowSpan="2">
<FlipViewItem>
<Button Foreground="White" Background="Black" x:Name="btnAdd" Content="Add" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" Tapped="btnAdd_Tapped" ></Button>
</FlipViewItem>
<FlipViewItem>
<Button x:Name="btny2" Content="mov" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" Tapped="btny2_Tapped" ></Button>
</FlipViewItem>
<FlipViewItem>
<Button x:Name="btnUpdate" Content="Upd" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Right" Tapped="btnupdate_Tapped" ></Button>
</FlipViewItem>
<FlipViewItem>
<Button x:Name="btnRemove" Content="Rem" Grid.Row="1" Grid.Column="3" HorizontalAlignment="Center" Tapped="btnRemove_Tapped" ></Button>
</FlipViewItem>
</FlipView>
private void btnAdd_Tapped(object sender, TappedRoutedEventArgs e)
{
flip.SelectedIndex++;
}
private void btnupdate_Tapped(object sender, TappedRoutedEventArgs e)
{
flip.SelectedIndex++;
}
private void btny2_Tapped(object sender, TappedRoutedEventArgs e)
{
flip.SelectedIndex++;
}
private void btnRemove_Tapped(object sender, TappedRoutedEventArgs e)
{
flip.SelectedIndex++;
}
but it is not working at all.
On button click event just increase/decrease selected index..
private void Button_Click_1(object sender, RoutedEventArgs e)
{
SelectedIndex++;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
SelectedIndex--;
}
Try something like this
in .xaml
<FlipView x:Name="flip" Grid.Row="1" Grid.ColumnSpan="4" Grid.Column="0" Margin="0,0,0,0.333" Grid.RowSpan="2">
<Button Foreground="White" Background="Black" x:Name="btnAdd" Content="Add" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" Tapped="btnAdd_Tapped" ></Button>
<Button x:Name="btny2" Content="mov" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" Tapped="btny2_Tapped" ></Button>
<Button x:Name="btnUpdate" Content="Upd" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Right" Tapped="btnupdate_Tapped" ></Button>
<Button x:Name="btnRemove" Content="Rem" Grid.Row="1" Grid.Column="3" HorizontalAlignment="Center" Tapped="btnRemove_Tapped" ></Button>
</FlipView>
in .cs
private void Button_Click_1(object sender, RoutedEventArgs e)
{
var newItemIndex = (flip.SelectedIndex + 1)
flip.SelectedIndex = newItemIndex;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var newItemIndex = (flip.SelectedIndex - 1)
flip.SelectedIndex = newItemIndex;
}
I found some work around for same if you put the button outside the flipview and then try to change the index of same then its work
Please look into the following code , Using Grid or Canvas we can easily put the button on Flipview
xaml
<Grid x:Name="LayoutRoot">
<FlipView x:Name="SlideHub" ManipulationMode="Rotate" FlowDirection="LeftToRight" UseTouchAnimationsForAllNavigation="True" Grid.Row="1" >
<FlipViewItem >
</FlipViewItem>
<FlipViewItem >
</FlipViewItem>
<FlipViewItem >
</FlipViewItem>
<FlipViewItem >
</FlipViewItem>
</FlipView>
<Image Tapped="slideBankingNext_tapped" Grid.Row="0" Width="40" Margin="0 10 10 20" HorizontalAlignment="Right" VerticalAlignment="Bottom" Source="/Images/welcome_next.png" />
</Grid>
C# Code
private void slideBankingNext_tapped(object sender, TappedRoutedEventArgs e)
{
var newItemIndex = (SlideHub.SelectedIndex + 1);
SlideHub.SelectedIndex = newItemIndex;
}
Related
I am working on Windows phone 8.1 winRT App and try to load FilpviewItem from code behind using the index property but its not working for me how we slide flipviewitem from c#
Following is my code which is not working
<FlipView x:Name="flip" Grid.Row="1" Grid.ColumnSpan="4" Grid.Column="0" Margin="0,0,0,0.333" Grid.RowSpan="2">
<FlipViewItem>
<Button Foreground="White" Background="Black" x:Name="btnAdd" Content="Add" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" Tapped="btnAdd_Tapped" ></Button>
</FlipViewItem>
<FlipViewItem>
<Button x:Name="btny2" Content="mov" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" Tapped="btny2_Tapped" ></Button>
</FlipViewItem>
<FlipViewItem>
<Button x:Name="btnUpdate" Content="Upd" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Right" Tapped="btnupdate_Tapped" ></Button>
</FlipViewItem>
<FlipViewItem>
<Button x:Name="btnRemove" Content="Rem" Grid.Row="1" Grid.Column="3" HorizontalAlignment="Center" Tapped="btnRemove_Tapped" ></Button>
</FlipViewItem>
</FlipView>
private void btnAdd_Tapped(object sender, TappedRoutedEventArgs e)
{
flip.SelectedIndex++;
}
private void btnupdate_Tapped(object sender, TappedRoutedEventArgs e)
{
flip.SelectedIndex++;
}
private void btny2_Tapped(object sender, TappedRoutedEventArgs e)
{
flip.SelectedIndex++;
}
private void btnRemove_Tapped(object sender, TappedRoutedEventArgs e)
{
flip.SelectedIndex++;
}
The amusing thing here is that your code works perfectly well when I copy paste it on my system. However, you could use data binding to achieve this in a cleaner manner.
How To do it with data binding:
create a property named FlipSelectedIndex for example.
private int flipSelectedIndex = 0;
public int FlipSelectedIndex
{
get { return flipSelectedIndex; }
set
{
if (value <= flip.Items.Count - 1)
{
flipSelectedIndex = value;
RaisePropertyChanged("FlipSelectedIndex");
}
}
}
Add an INotifyPropertyChangedinterface and implement it implicitly and then add an event to it called RaisePropertyChanged do this by adding a ,INotifyPropertyChangedinterface to ClassName : Page declaration and add the following method:
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string property)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
Please Note: The PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); Only works with C# 6.0 which is available in Visual Studio 2015. For any editions you will have to use
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
Once done now it's simple: at a button tap simply use FlipSelectedIndex++; or FlipSelectedIndex--; as required.
Update the XAML with adding the SelectedIndex property of the Flipview to data binding like this: SelectedIndex="{Binding FlipSelectedIndex,Mode=TwoWay}" Use two way binding to ensure your FlipSelectedIndex also updates if you change the selected view on the Flipview from the swipe action instead of the button.
Please do remember to add that data context of the page to the code behind using DataContext="{Binding RelativeSource={RelativeSource Self}}" Else the binding won't work.
For more information you could refer to my answer on the FlipView here
I have created a page, in which I have placed inside the Grid the above:
<ProgressRing x:Name="Ring" HorizontalAlignment="Center" VerticalAlignment="Center" IsEnabled="True"/>
But when I do Ring.IsActive = true; in my code nothing happens! Possibly I am missing something very simple here, but I can't find out what it is.
This is my xaml:
<Page
x:Class="Diakopes_v2._0.AddExpense"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Diakopes_v2._0"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Background="#FF0277BD">
<Page.BottomAppBar>
<CommandBar>
<AppBarButton Icon="Accept" Label="Accept" Click="Accept_Click"/>
<AppBarButton Icon="Cancel" Label="Cancel" Click="Cancel_Click"/>
</CommandBar>
</Page.BottomAppBar>
<Grid>
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF0277BD" Offset="0"/>
<GradientStop Color="#FFE1F5FE" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<TextBlock Text="New Expense" TextAlignment="Center" FontSize="46" FontFamily="/Assets/Fonts/BebasNeue Bold.ttf#Bebas Neue"></TextBlock>
<StackPanel HorizontalAlignment="Left" Height="564" Margin="0,76,0,0" VerticalAlignment="Top" Width="400">
<TextBox x:Name="Name" PlaceholderText="Name"></TextBox>
<TextBox x:Name="Price" InputScope="Number" PlaceholderText="Price"></TextBox>
</StackPanel>
<ProgressRing x:Name="Ring" HorizontalAlignment="Center" VerticalAlignment="Center" IsEnabled="True"/>
</Grid>
</Page>
and this is my .cs method were I want to use the ProgressRing:
private void Accept_Click(object sender, RoutedEventArgs e) {
Ring.IsActive = true;
Name.Text = Name.Text.Trim();
Price.Text = Price.Text.Trim();
DBHelper dbHelper = new DBHelper();
Price.Text = Price.Text.Replace(",", ".");
ExpensesTable NewExpense = new ExpensesTable(Place.Id, Name.Text, Convert.ToSingle(Price.Text));
dbHelper.InsertExpense(NewExpense);
Ring.IsActive = false;
Frame.Navigate(typeof(Expenses), Place.Name);
}
I just created sample with code that you provided with simple modifications:
Turn on:
private void Accept_Click(object sender, RoutedEventArgs e)
{
Ring.IsActive = true;
}
Turn off:
private void Cancel_Click(object sender, RoutedEventArgs e)
{
Ring.IsActive = false;
}
Look at the screenshot from emulator:
Also, to remove black rect, you should change Background property to Transparent
So, I guess your code works so fast that you haven't time to see how works ProgressRing.
i have a listbox with some contact number in listbox now i want to add Show and Hide button to each of contact item in list box when user press Hide button contact will be Hide and when user press Show button contact will be show and vise versa how i can do this in WP8..
i am using following code please help me..
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" Margin="0, 10, 0, 10">
<Border BorderThickness="0" HorizontalAlignment="Left" VerticalAlignment="Center" BorderBrush="{StaticResource PhoneAccentBrush}" Margin="5, 0, 0, 0">
<Image Source="{Binding ImageUrl, Converter={StaticResource kconverter}}" Width="48" Height="48" Stretch="Fill"/>
</Border>
<TextBlock x:Name="item_name" Text="{Binding Name, Mode=OneWay}" FontSize="{StaticResource PhoneFontSizeLarge}" Margin="10, 0, 0, 0" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0, 10, 0, 10">
<Border BorderThickness="0" HorizontalAlignment="Left" VerticalAlignment="Center" BorderBrush="{StaticResource PhoneAccentBrush}" Margin="355, 0, 0, 0">
<Image Name="onimg" Visibility="Visible" Source="/Assets/Images/blue.button.png" Width="85" Height="20" Tap="Image_TapOn" Stretch="Fill"/>
</Border>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0, 10, 0, 10">
<Border BorderThickness="0" HorizontalAlignment="Left" VerticalAlignment="Center" BorderBrush="{StaticResource PhoneAccentBrush}" Margin="355, 0, 0, 0">
<Image Name="offimg" Visibility="Collapsed" Source="/Assets/Images/setting-h.png" Width="85" Height="48" Tap="Image_TapOff" Stretch="Fill"/>
</Border>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Hi Use this code Hope so it works for you.
Note : Bind only ObservableCollection to ListBox to Use INotifyPropertyChanged
XAML :
<Grid Grid.Row="1">
<ListBox x:Name="lstContact">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Border Background="#404040"
Height="45"
Width="60"
Tag="{Binding ContactId}"
Tap="_Collapsed">
<TextBlock Text="-"
FontSize="24"
VerticalAlignment="Center"
TextAlignment="Center"
Foreground="White" />
</Border>
<Border Name="BrCount"
Width="160"
Height="45"
BorderBrush="#404040"
BorderThickness="0"
Margin="10,0">
<TextBlock Name="LblCont"
Text="{Binding ContactName}"
FontSize="24"
VerticalAlignment="Center"
TextAlignment="Center"
Foreground="#404040"
Visibility="{Binding _Visibility}" />
</Border>
<Border Background="#404040"
Height="45"
Width="60"
Tag="{Binding ContactId}"
Tap="_Visible">
<TextBlock Text="+"
FontSize="24"
VerticalAlignment="Center"
TextAlignment="Center"
Foreground="White" />
</Border>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
C# :
private ObservableCollection<Contact> _contact = new ObservableCollection<Contact>();
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
_contact = new ObservableCollection<Contact>();
for (int i = 0; i < 10; i++)
{
_contact.Add(new Contact() { ContactId = i, ContactName = "Name " + i, _Visibility = Visibility.Visible });
}
lstContact.ItemsSource = _contact;
}
private void _Collapsed(object sender, System.Windows.Input.GestureEventArgs e)
{
Border bdr = (Border)sender;
_contact.Where(X => X.ContactId == Convert.ToInt32(bdr.Tag.ToString())).First()._Visibility = Visibility.Collapsed;
}
private void _Visible(object sender, System.Windows.Input.GestureEventArgs e)
{
Border bdr = (Border)sender;
_contact.Where(X => X.ContactId == Convert.ToInt32(bdr.Tag.ToString())).First()._Visibility = Visibility.Visible;
}
public class Contact : INotifyPropertyChanged
{
public int ContactId { get; set; }
public string ContactName { get; set; }
public Visibility _visibility;
public Visibility _Visibility
{
get { return _visibility; }
set { SetProperty(ref _visibility, value); }
}
protected void SetProperty<T>(ref T storage, T value, [System.Runtime.CompilerServices.CallerMemberName] String propertyName = null)
{
if (!object.Equals(storage, value))
{
storage = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
protected void RaisePropertyChanged([System.Runtime.CompilerServices.CallerMemberName] String propertyName = null)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
i'm creating simple canvas application which is to get the signature from canvas which is named as "ContentPanelCanvas" which is from Grid named as "pop-up" and to set that signature from "Pop-up" and to set on canvas named as "maincanvas".
If you know Please help me to resolve this issue how to get the canvas context and set it to blank canvas.Here is the code i used.
In Xaml:
<Grid x:Name="ContentPanel">
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Canvas Name="maincanvas">
</Canvas>
</Grid>
<Grid Name="Popup" Visibility="Collapsed" Grid.Row="0" Width="480" Height="300" Background="Black">
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Canvas Name="ContentPanelCanvas" Background="Beige">
</Canvas>
</Grid>
<Grid Grid.Row="1" HorizontalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" x:Name="btnOK" Content="OK" Tap="btnOK_Tap"/>
<Button Grid.Column="1" x:Name="btnCancel" Content="Clear" Tap="btnCancel_Tap"/>
</Grid>
</Grid>
</Grid>
Here is the .CS file:
private Point currentPoint;
private Point oldPoint;
Line line;
public SingleImage()
{
InitializeComponent();
this.ContentPanelCanvas.MouseMove += new MouseEventHandler(ContentPanelCanvas_MouseMove);
this.ContentPanelCanvas.MouseLeftButtonDown += new MouseButtonEventHandler(ContentPanelCanvas_MouseLeftButtonDown);
}
void ContentPanelCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
currentPoint = e.GetPosition(ContentPanelCanvas);
oldPoint = currentPoint;
}
void ContentPanelCanvas_MouseMove(object sender, MouseEventArgs e)
{
currentPoint = e.GetPosition(this.ContentPanelCanvas);
line = new Line() { X1 = currentPoint.X, Y1 = currentPoint.Y, X2 = oldPoint.X, Y2 = oldPoint.Y };
line.Stroke = new SolidColorBrush(Colors.Purple);
line.StrokeThickness = 3;
this.ContentPanelCanvas.Children.Add(line);
oldPoint = currentPoint;
}
private void Button_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
Popup.Visibility = Visibility.Visible;
ContentPanelCanvas.Children.Clear();
}
private void btnOK_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
Popup.Visibility = Visibility.Collapsed;
// Here i want to set signature to main canvas how to pass signature
from one canvas to another?//
}
private void btnCancel_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
ContentPanelCanvas.Children.Clear();
}
private void btnOK_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
Popup.Visibility = Visibility.Collapsed;
int k = ContentPanelCanvas.Children.Count;
for (int i = 0; i < k; i++)
{
line =(System.Windows.Shapes.Line) ContentPanelCanvas.Children[0];
line.StrokeThickness = 3;
ContentPanelCanvas.Children.RemoveAt(0);
maincanvas.Children.Add(line);
}
}
My Longlistselector DataTemplate
<DataTemplate x:Key="NotesListBoxItemTemplate">
<Grid HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.5*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<CheckBox
Grid.Column="0" Grid.Row="0" Grid.RowSpan="2"
x:Name="chkDelete"
Visibility="Visible" Tap="chkDelete_Tap" Margin="0,36,0,0" />
<TextBlock
Text="{Binding NoteName}"
FontSize="{StaticResource PhoneFontSizeLarge}"
FontFamily="Segoe WP"
Grid.Row="0" Grid.Column="1" Margin="12,24,0,0" />
</Grid>
</DataTemplate>
and my Longlist selector is
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ScrollViewer>
<phone:LongListSelector
x:Name="MainListBox"
ItemsSource="{Binding AllData}"
Margin="12, 0, 12, 0"
ItemTemplate="{StaticResource ListBoxItemTemplate}" />
</ScrollViewer>
</Grid>
How do I loop through the items and get the Checkbox checked state in each item? Previously I used ListBox and its worked correctly. And I am able to find out the checkbox value of the each item using below code
private T FindFirstElementInVisaulTree<T>(DependencyObject parentElement) where T:DependencyObject
ListBoxItem passed as a DependencyObject. The only problem with ListBox is scrolling. So trying for LongListSelector.
Please how do i loop through the items in LongListSelector.
Thank you
You can also Data Bind the state of the checkbox to your Model. Then you can just loop through the MainListBox.ItemsSource. If you do it this way you need to set the Binding Mode=Two Way or the collection will not change once someone taps the Checkbox. I would also recommend you use a Command to handle the Tap event on the Checkbox so you can handle in your ViewModel rather then Code behind. Here's my quick example modified from previous solutions I have posted:
// Namespaces used
using System.Collections.ObjectModel; // ObservableCollection<T>
using System.ComponentModel; // INotifyPropertyChanged
// sample_data class
public class sample_data : INotifyPropertyChanged
{
// simple constructor
public sample_data(string noteName, Boolean checkboxState)
{
this.NoteName = noteName;
this.CheckboxState = checkboxState;
}
// implement the INotify
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
// {Binding Properties}
Boolean checkbox_state;
public Boolean CheckboxState
{
get { return checkbox_state; }
set { checkbox_state = value; NotifyPropertyChanged("CheckboxState"); }
}
string note_name;
public string NoteName
{
get { return note_name; }
set { note_name = value; NotifyPropertyChanged("NoteName"); }
}
}
// create a sample set of data to show
private ObservableCollection<sample_data> CreateData()
{
ObservableCollection<sample_data> my_list = new ObservableCollection<sample_data>();
my_list.Add(new sample_data("one", false));
my_list.Add(new sample_data("two", true));
my_list.Add(new sample_data("three", false));
my_list.Add(new sample_data("four", true));
my_list.Add(new sample_data("five", false));
my_list.Add(new sample_data("six", true));
my_list.Add(new sample_data("seven", false));
my_list.Add(new sample_data("eight", true));
return my_list;
}
public MainPage()
{
InitializeComponent();
MainListBox.ItemsSource = CreateData(); // set the data bind
}
/// You can loop through the items like this, use any convention you want.
private void LoopThroughItems()
{
foreach (sample_data sd in MainListBox.ItemsSource)
{
Boolean is_check = sd.CheckboxState;
}
}
Your DataTemplate needs to change so it Databinds the checkbox with two-way binding.
<DataTemplate x:Key="NotesListBoxItemTemplate">
<Grid HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.5*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<CheckBox Grid.Column="0" IsChecked="{Binding CheckboxState, Mode=TwoWay}" Grid.Row="0" Grid.RowSpan="2" x:Name="chkDelete" Visibility="Visible" Margin="0,36,0,0" />
<TextBlock Text="{Binding NoteName}" FontSize="{StaticResource PhoneFontSizeLarge}" FontFamily="Segoe WP" Grid.Row="0" Grid.Column="1" Margin="12,24,0,0" />
</Grid>
</DataTemplate>