Xaml 从DataTemplate执行命令
我有以下代码:Xaml 从DataTemplate执行命令,xaml,mvvm,Xaml,Mvvm,我有以下代码: <StackPanel Orientation="Vertical"> <GridView Background="Azure" x:Name="ContactList" It
<StackPanel Orientation="Vertical">
<GridView
Background="Azure"
x:Name="ContactList"
ItemsSource="{Binding Path=Users}"
SelectedItem="{Binding SelectedUser, Mode=TwoWay}">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="SelectionChanged">
<core:InvokeCommandAction
Command="{Binding MyCommandOnTheViewModel, Mode=OneWay}"/>
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
<GridView.ItemTemplate>
<DataTemplate>
<Border Width="300" Height="Auto" BorderThickness="1">
<StackPanel Orientation="Vertical">
<TextBlock HorizontalAlignment="Left">
<Run FontWeight="Bold" Text="{Binding Name}" />
<Run Text="{Binding Age}" />
</TextBlock>
<StackPanel Orientation="Horizontal">
<Button Content="Button1" HorizontalAlignment="Left"></Button>
<Button Content="Button2" HorizontalAlignment="Right"></Button>
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</StackPanel>
但似乎没有这样的效果。
任何提示谢谢
这就是我的工作原理:
<Button Content="MyButton"
Command ="{Binding ElementName=ContactList,Path=DataContext.MyCommand}"></Button>
感谢所有帮助@Utsav您可以为每个按钮提供
命令
属性。通过这种方式,您可以在单击事件时使用两个按钮轻松地在viewmodel中绑定两个单独的命令
请参阅下面的代码
<StackPanel Orientation="Vertical">
<GridView
Background="Azure"
x:Name="ContactList"
ItemsSource="{Binding Path=Users}"
SelectedItem="{Binding SelectedUser, Mode=TwoWay}">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="SelectionChanged">
<core:InvokeCommandAction
Command="{Binding MyCommandOnTheViewModel, Mode=OneWay}"/>
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
<GridView.ItemTemplate>
<DataTemplate>
<Border Width="300" Height="Auto" BorderThickness="1">
<StackPanel Orientation="Vertical">
<TextBlock HorizontalAlignment="Left">
<Run FontWeight="Bold" Text="{Binding Name}" />
<Run Text="{Binding Age}" />
</TextBlock>
<StackPanel Orientation="Horizontal">
<Button Content="Button1" HorizontalAlignment="Left" Command="{Binding CommandLeftBtn}"></Button>
<Button Content="Button2" HorizontalAlignment="Right" Command="{Binding CommandRightBtn}"></Button>
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</StackPanel>
谢谢您的回答。但是,我不能这样在按钮上添加命令,因为我在GridView中。我没有直接访问vm的权限。请将您的页面名称指定为page。然后试试这个:谢谢,不幸的是,我只能从GridView中绑定到Gridviews ItemSource。你试过我在上面的评论中建议的吗?这应该行得通,对我来说行得通。是的,我行。也许它不适合我,因为我使用MVVM lights viewModelLocator。我的DataContext如下所示:DataContext=“{Binding Source={StaticResource Locator},Path=UserViewModel}”
<StackPanel Orientation="Vertical">
<GridView
Background="Azure"
x:Name="ContactList"
ItemsSource="{Binding Path=Users}"
SelectedItem="{Binding SelectedUser, Mode=TwoWay}">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="SelectionChanged">
<core:InvokeCommandAction
Command="{Binding MyCommandOnTheViewModel, Mode=OneWay}"/>
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
<GridView.ItemTemplate>
<DataTemplate>
<Border Width="300" Height="Auto" BorderThickness="1">
<StackPanel Orientation="Vertical">
<TextBlock HorizontalAlignment="Left">
<Run FontWeight="Bold" Text="{Binding Name}" />
<Run Text="{Binding Age}" />
</TextBlock>
<StackPanel Orientation="Horizontal">
<Button Content="Button1" HorizontalAlignment="Left" Command="{Binding CommandLeftBtn}"></Button>
<Button Content="Button2" HorizontalAlignment="Right" Command="{Binding CommandRightBtn}"></Button>
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</StackPanel>