Use SampleCommand class dont change DataContext
This commit is contained in:
Родитель
5aa3a84ca5
Коммит
54944a7405
|
@ -3,10 +3,13 @@
|
|||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="using:Microsoft.Toolkit.Uwp.SampleApp.SamplePages"
|
||||
xmlns:ui="using:Microsoft.Toolkit.Uwp.UI"
|
||||
mc:Ignorable="d">
|
||||
|
||||
<Page.Resources>
|
||||
<local:SampleCommand x:Key="SampleCommand" />
|
||||
|
||||
<DataTemplate x:Name="NormalTemplate">
|
||||
<TextBlock Text="{Binding}" Foreground="Green" VerticalAlignment="Center" FontWeight="Bold"></TextBlock>
|
||||
</DataTemplate>
|
||||
|
@ -29,7 +32,7 @@
|
|||
Margin="12"
|
||||
ItemTemplate="{StaticResource NormalTemplate}"
|
||||
IsItemClickEnabled="True"
|
||||
ui:ListViewExtensions.Command="{Binding SampleCommand}"
|
||||
ui:ListViewExtensions.Command="{StaticResource SampleCommand}"
|
||||
ui:ListViewExtensions.AlternateColor="#33AAAAAA"
|
||||
ui:ListViewExtensions.AlternateItemTemplate="{StaticResource AlternateTemplate}"
|
||||
ui:ListViewExtensions.ItemContainerStretchDirection="Both">
|
||||
|
|
|
@ -2,11 +2,14 @@
|
|||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="using:Microsoft.Toolkit.Uwp.SampleApp.SamplePages"
|
||||
xmlns:ui="using:Microsoft.Toolkit.Uwp.UI"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
mc:Ignorable="d">
|
||||
|
||||
<Page.Resources>
|
||||
<local:SampleCommand x:Key="SampleCommand" />
|
||||
|
||||
<DataTemplate x:Name="NormalTemplate">
|
||||
<TextBlock Text="{Binding}" Foreground="Green" VerticalAlignment="Center" FontWeight="Bold"></TextBlock>
|
||||
</DataTemplate>
|
||||
|
@ -22,7 +25,7 @@
|
|||
Margin="12"
|
||||
ui:ListViewExtensions.AlternateColor="#33AAAAAA"
|
||||
ui:ListViewExtensions.AlternateItemTemplate="{StaticResource AlternateTemplate}"
|
||||
ui:ListViewExtensions.Command="{Binding SampleCommand}"
|
||||
ui:ListViewExtensions.Command="{StaticResource SampleCommand}"
|
||||
ui:ListViewExtensions.ItemContainerStretchDirection="Both"
|
||||
IsItemClickEnabled="True"
|
||||
ItemTemplate="{StaticResource NormalTemplate}" />
|
||||
|
|
|
@ -5,8 +5,6 @@
|
|||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Windows.Input;
|
||||
using Microsoft.Toolkit.Uwp.SampleApp.Common;
|
||||
using Microsoft.Toolkit.Uwp.SampleApp.Data;
|
||||
using Microsoft.Toolkit.Uwp.UI;
|
||||
using Windows.UI.Popups;
|
||||
using Windows.UI.Xaml;
|
||||
|
@ -21,8 +19,6 @@ namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
|
|||
this.InitializeComponent();
|
||||
}
|
||||
|
||||
public ICommand SampleCommand => new DelegateCommand<string>(OnExecuteSampleCommand);
|
||||
|
||||
public async void OnXamlRendered(FrameworkElement control)
|
||||
{
|
||||
var sampleListView = control.FindChild("SampleListView") as ListView;
|
||||
|
@ -48,14 +44,6 @@ namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
|
|||
{
|
||||
sampleListView.ItemsSource = GetOddEvenSource(500);
|
||||
}
|
||||
|
||||
// Transfer Data Context so we can access SampleCommand
|
||||
control.DataContext = this;
|
||||
}
|
||||
|
||||
private async void OnExecuteSampleCommand(string item)
|
||||
{
|
||||
await new MessageDialog($"You clicked {item} via the 'ListViewExtensions.Command' binding", "Item Clicked").ShowAsync();
|
||||
}
|
||||
|
||||
public ObservableCollection<string> GetOddEvenSource(int count)
|
||||
|
@ -71,4 +59,30 @@ namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
|
|||
return oddEvenSource;
|
||||
}
|
||||
}
|
||||
|
||||
#pragma warning disable SA1402 // File may only contain a single class
|
||||
internal class SampleCommand : ICommand
|
||||
#pragma warning restore SA1402 // File may only contain a single class
|
||||
{
|
||||
event EventHandler ICommand.CanExecuteChanged
|
||||
{
|
||||
add { }
|
||||
remove { }
|
||||
}
|
||||
|
||||
public bool CanExecute(object parameter) => true;
|
||||
|
||||
public void Execute(object parameter)
|
||||
{
|
||||
if (parameter is string s)
|
||||
{
|
||||
OnExecuteSampleCommand(s);
|
||||
}
|
||||
}
|
||||
|
||||
private static async void OnExecuteSampleCommand(string item)
|
||||
{
|
||||
await new MessageDialog($"You clicked {item} via the 'ListViewExtensions.Command' binding", "Item Clicked").ShowAsync();
|
||||
}
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче