wpf 在DataGrid内的by Button的Command属性中遇到绑定错误

j2datikz  于 2023-04-22  发布在  其他
关注(0)|答案(1)|浏览(222)

我尝试在WPF DataGrid中使用按钮。我的datagrid ItemsSource绑定到一个类,但我希望按钮在ViewModel中调用ICommand。我已经看了十几篇关于这个的文章,但仍然无法让它工作。
我的DataGrid xaml:

<DataGrid Name="gridSettingsData" FontSize="13" BorderThickness="0"  AutoGenerateColumns="False" CanUserAddRows="False" CanUserReorderColumns="False" SelectedItem="{Binding SelectedSettingsStatus, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  ItemsSource="{Binding lstDetailedSettingsStatus, UpdateSourceTrigger=PropertyChanged}" ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto"  >
                            <DataGrid.Columns>
                                <DataGridTextColumn IsReadOnly="True" Binding="{Binding Path=FileType}" ClipboardContentBinding="{x:Null}" Header="FileType" Width="160" />
                                <DataGridTextColumn IsReadOnly="True" Binding="{Binding Path=FileStatus}" ClipboardContentBinding="{x:Null}" Header="FileStatus" Width="580" />
                                <DataGridTextColumn IsReadOnly="True" Binding="{Binding Path=ExpectedVersion}" ClipboardContentBinding="{x:Null}" Header="ExpectedVersion" Width="120" />
                                <DataGridTextColumn IsReadOnly="True" Binding="{Binding Path=LastLoadedVersion}" ClipboardContentBinding="{x:Null}" Header="LastLoadedVersion" Width="120" />

                                <DataGridTemplateColumn Header="View" Width="50">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <Button Content="View"
                                                    Command="{Binding GetSettingsCommand, RelativeSource={RelativeSource AncestorType=viewmodels:vmProgramming}}" 
                                                    CommandParameter="{Binding ElementName=gridSettingsData, Path=SelectedItem}"  >
                                            </Button >
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>

                            </DataGrid.Columns>
                        </DataGrid>

从我的ViewModel:

public class vmProgramming : BaseViewModel, IPageViewModel
{
    public ICommand GetSettingsCommand { get; private set; }

       public vmProgramming()
       {
          // the relevant part... 
          GetSettingsCommand = new CommunityToolkit.Mvvm.Input.RelayCommand<object>(GetSettings);
       }

       private async void GetSettings(object obj)
       {
          // a bunch of stuff, including some async things... 
       }
}

当我运行这个时,我在datagrid按钮的Command属性上得到了绑定错误。我确实在视图上的另一个按钮上使用了GetSettings()方法,而这个按钮不在DataGrid上。视图和视图模型之间的其他所有东西都工作了,datagrid显示了我期望的数据。只是按钮不工作。

mqkwyuun

mqkwyuun1#

试试这个:

<Button Content="View"
        Command="{Binding DataContext.GetSettingsCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}" 
        CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource AncestorType=DataGrid}}"  ... />

你不能在CellTemplate的绑定中使用ElementName引用父DataGrid,因为它在不同的命名范围内。并且AncestorType应该设置为可视化树中的元素的类型。

相关问题