私はwinRTが初めてで、これに苦労しています。
以下のように DemoClickCommand を宣言する DemoViewModel を作成しました。コマンドはタイプ DelegateCommand です。
public DelegateCommand DemoClickCommand { get; private set; }
protected virtual void OnClickCommandExecuted(object parameter)
{
var obj = parameter;
}
protected virtual bool OnClickCommandCanExecute(object parameter)
{
return true;
}
public DemoViewModel()
{
this.DemoClickCommand = new DelegateCommand(this.OnClickCommandExecuted, this.OnClickCommandCanExecute);
}
このコマンドをフォームのボタンにバインドしましたが、それ自体は正常に機能しますが、データ テンプレートを使用する listView に含まれていると機能しません。
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<!-- THIS BUTTON WORKS FINE-->
<Button Content="Button"
HorizontalAlignment="Left"
Height="143"
Margin="1354,424,0,0"
VerticalAlignment="Top"
Width="307"
Background="#FFC11A1A"
FontSize="36"
Command="{Binding DemoClickCommand, Mode=OneWay}" />
<!-- THIS LISTVIEW WHICH USES THE DATATEMPLATE WONT WORK -->
<ListView x:Name="lvDemoItems"
SelectionChanged="lvDemoItems_SelectionChanged"
Grid.Column="0"
Margin="0,140,0,0"
ItemTemplate="{StaticResource DemoTemplate}"
ItemsSource="{Binding DemoItems}"
SelectionMode="Single"
HorizontalAlignment="Left"
Width="904">
</ListView>
</Grid>
これは、私のページ リソースで定義されているデータ テンプレートです。
<Page.Resources>
<DataTemplate x:Name="DemoTemplate">
<Grid Height="110"
Width="480"
Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}"
Width="110"
Height="110">
<Image Source="{Binding ImageURI}"
Stretch="UniformToFill"
AutomationProperties.Name="{Binding Title}" />
</Border>
<StackPanel Grid.Column="1"
VerticalAlignment="Top"
Margin="10,0,0,0">
<TextBlock Text="{Binding Name}"
Style="{StaticResource TitleTextStyle}"
TextWrapping="NoWrap" />
<TextBlock Text="{Binding Description}"
Style="{StaticResource BodyTextStyle}"
MaxHeight="160" />
<Button Content="Edit"
BorderBrush="Black"
Background="Green"
Height="40"
Width="80"
Margin="0,20,0,0"
Command="{Binding DemoClickCommand, Mode=OneWay}"
CommandParameter="{Binding ID}" />
</StackPanel>
</Grid>
</DataTemplate>
</Page.Resources>
アタッチされた依存関係プロパティを使用する必要があると思いますが、これを実装する方法がわかりません。
これは私がこれまでに持っているものですが、それを機能させる方法がわかりません。これを助けて説明してもらえますか?ありがとう
public ICommand DemoClickCommand { get; private set; }
public static readonly DependencyProperty DemoClickCommand =
DependencyProperty.RegisterAttached("DemoClickCommand", typeof(ICommand), typeof(Button), new PropertyMetadata(null, OnItemCommandPropertyChanged));
private static void OnClickCommandPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ICommand command = e.NewValue as ICommand;
}