UWP-来自DataGrid的命令未调用 [英] UWP - Command from DataGrid is not called
问题描述
UWP应用程序(重要,因为没有AncestorType)我无法从DataGridTemplateColumn绑定ViewModel的命令(其他任何值).这是我当前的代码(我已经尝试过,几乎所有内容)
UWP Application (Important because there is no AncestorType) I can't bind command (neither other values) of the ViewModel from a DataGridTemplateColumn. Here is my current code (i have tried, literally everything)
<controls:DataGrid
x:Name="DataGrid"
Grid.Row="2"
Height="Auto"
Margin="12"
AutoGenerateColumns="False"
HorizontalContentAlignment="Center"
ItemsSource="{Binding ProviderOrders}">
<controls:DataGrid.Columns>
<controls:DataGridTemplateColumn Header="Actions" Width="*">
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Modifier" Command="{Binding DataContext.EditOrderCommand, RelativeSource={RelativeSource Mode=Self}}" CommandParameter="{Binding}" Style="{StaticResource PrimaryButton}"/>
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>
</controls:DataGrid.Columns>
</controls:DataGrid>
我也尝试过
<Button Content="Modifier" Command="{Binding ElementName=DataGrid, Path=DataContext.EditOrderCommand}" CommandParameter="{Binding}" Style="{StaticResource PrimaryButton}"/>
没有错误,但是如果我将按钮移到DataGrid之外,则我的命令没有运行,并且我的命令正在运行.
There is no error but my Command is not runned and my command is working if i move the Button outside the DataGrid..
DataGridTemplateColumn DataContext是ProviderOrder对象,因此我需要访问ViewModel(显然无法从ProviderOrder对象访问它)
The DataGridTemplateColumn DataContext is the ProviderOrder object and so i need to access of the ViewModel (which is obviously not accessible from the ProviderOrder object)
先谢谢您了:)
推荐答案
很好的问题, DataGrid
控件中的此已知问题.当前,在这种情况下,有一种工作方式是绑定 CellTemplate
中的按钮命令,请将该命令添加到数据源中.
Great question, this known issue in DataGrid
control. Currently, there is a workaroung for this scenario that bind command for button in CellTemplate
, please add the command in the datasouce.
public class Item
{
public string ID { get; set; }
public ICommand BtnCommand
{
get
{
return new CommadEventHandler<Item>((s) => BtnClick(s));
}
}
private void BtnClick(Item s)
{
}
}
Xaml代码
<controls:DataGridTemplateColumn>
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button
Command="{Binding BtnCommand}"
Content="Click"
/>
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>
更新
如果不是特定的 DataGrid
,则可以使用listview进行替换,并且 Binding ElementName
将起作用.
If not specific DataGrid
, you could use listview to replace, and Binding ElementName
will work.
这篇关于UWP-来自DataGrid的命令未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!