我是winRT的新手并且在这方面苦苦挣扎。
我创建了一个DemoViewModel,我在其中声明了如下所示的DemoClickCommand。该命令的类型为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);
}
我已将此命令绑定到我的表单上的一个按钮,该按钮本身可以正常工作但在包含在使用datatemplate的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;
}
答案 0 :(得分:0)
在从底部到顶部完成的WPF值继承中。例如,当您将特定字体设置为堆栈面板时,堆栈面板内的所有子控件默认情况下将获得相同的字体。在带有datatemplate的代码段中,你有一个绑定到DemoClickCommand的Button,它是ListView控件的子代,其中ItemsSource是 DemoItems 。 DemoItems没有名为DemoClickCommand的属性。所以你需要明确提到按钮的祖先类型。请在您的DataTemplate代码中尝试以下XAML代码段
<Button Content="Edit"
BorderBrush="Black"
Background="Green"
Height="40"
Width="80"
Margin="0,20,0,0"
Command="{Binding Path=DemoClickCommand, Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type Window}}}}"
CommandParameter="{Binding ID}" />
或者您可以使用BindingElementName属性执行相同操作。为您的ManGrid命名并使用
<Button Content="Edit"
BorderBrush="Black"
Background="Green"
Height="40"
Width="80"
Margin="0,20,0,0"
Command="{Binding ElementName="MyGrid" Path=DemoClickCommand, Mode=OneWay}"
CommandParameter="{Binding ID}" />