[英]UWP - Command from DataGrid is not called
UWP Application (Important because there is no AncestorType) I can't bind command (neither other values) of the ViewModel from a DataGridTemplateColumn. UWP 应用程序(重要,因为没有 AncestorType) 我无法从 DataGridTemplateColumn 绑定 ViewModel 的命令(也没有其他值)。 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>
I have also tried我也试过
<Button Content="Modifier" Command="{Binding ElementName=DataGrid, Path=DataContext.EditOrderCommand}" CommandParameter="{Binding}" Style="{StaticResource PrimaryButton}"/>
There is no error but my Command is not runned and my command is working if i move the Button outside the DataGrid..没有错误,但我的命令没有运行,如果我将按钮移到 DataGrid 之外,我的命令正在工作。
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) DataGridTemplateColumn DataContext 是 ProviderOrder object ,所以我需要访问 ViewModel (显然不能从 ProviderOrder 对象访问)
Thanks in advance:)提前致谢:)
Great question, this known issue in DataGrid
control.很好的问题, DataGrid
控件中的这个已知问题。 Currently, there is a workaroung for this scenario that bind command for button in CellTemplate
, please add the command in the datasouce.目前,此方案有一个解决方法,即在CellTemplate
中为按钮绑定命令,请在数据源中添加该命令。
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 Code Xaml 代码
<controls:DataGridTemplateColumn>
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button
Command="{Binding BtnCommand}"
Content="Click"
/>
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>
Update更新
If not specific DataGrid
, you could use listview to replace, and Binding ElementName
will work.如果不是特定的DataGrid
,您可以使用 listview 替换,并且Binding ElementName
将起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.