. Model, View ViewModel.
, , , Windows 8.1, WP 8.1. ListView, Project MainPage.xaml .
<ListView x:Name="myListView">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Artist}"></TextBlock>
<TextBlock Text="{Binding Song}"></TextBlock>
<Button Command="{Binding ElementName=myListView, Path=DataContext.SimpleCommand}"
CommandParameter="{Binding Extra}"
x:Name="mybutton" Width="200" Height="50" FontFamily="Global User Interface" Content="Click Me"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
, Button Command CommandParameter. - ViewModel, , . CommandParameter - , . TAG.
using System.Collections.ObjectModel;
using System.Windows.Input;
( )
public class MySimpleCommand : ICommand
{
public void Execute(object parameter)
{
string tag = parameter as string;
if(tag == "WHATEVER_YOU_WANT")
{
}
Frame rootFrame = Window.Current.Content as Frame;
rootFrame.Navigate(typeof(YOUR_PAGE));
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
}
( )
public class sample_model
{
public sample_model(string artist, string song, string extra = "")
{
this.Artist = artist;
this.Song = song;
this.Extra = extra;
}
public string Artist { get; set; }
public string Song { get; set; }
public string Extra { get; set; }
}
ViewModel ListView (s). ( )
public class sample_viewmodel
{
public sample_viewmodel()
{
this.DataSource = CreateData();
this.SimpleCommand = new MySimpleCommand();
}
private ObservableCollection<sample_model> CreateData()
{
ObservableCollection<sample_model> my_list = new ObservableCollection<sample_model>();
my_list.Add(new sample_model("Faith + 1", "Body of Christ", "A Track"));
my_list.Add(new sample_model("Faith + 1", "Christ Again", "A Track"));
my_list.Add(new sample_model("Faith + 1", "A Night With the Lord", "A Track"));
my_list.Add(new sample_model("Faith + 1", "Touch Me Jesus", "A Track"));
my_list.Add(new sample_model("Faith + 1", "I Found Jesus (With Someone Else)", "A Track"));
my_list.Add(new sample_model("Faith + 1", "Savior Self", "A Track"));
my_list.Add(new sample_model("Faith + 1", "Christ What a Day", "A Track"));
my_list.Add(new sample_model("Faith + 1", "Three Times My Savior", "A Track"));
my_list.Add(new sample_model("Faith + 1", "Jesus Touched Me", "A Track"));
my_list.Add(new sample_model("Faith + 1", "Lord is my Savior", "A Track"));
my_list.Add(new sample_model("Faith + 1", "I Wasn't Born Again Yesterday", "A Track"));
my_list.Add(new sample_model("Faith + 1", "Pleasing Jesus", "A Track"));
my_list.Add(new sample_model("Faith + 1", "Jesus (Looks Kinda Hot)", "A Track"));
my_list.Add(new sample_model("Butters", "What What", "B Track"));
return my_list;
}
public ICommand SimpleCommand { get; set; }
public ObservableCollection<sample_model> DataSource{ get; set; }
}
, , ListView ViewModel, "Artist, Song Button (Command and CommandParamters)". .
private void Page_Loaded(object sender, RoutedEventArgs e)
{
sample_viewmodel viewmodel = new sample_viewmodel();
myListView.DataContext = viewmodel;
myListView.ItemsSource = viewmodel.DataSource;
}
, , , , .
