在DataGrid中的Button的Command属性中遇到绑定错误。

huangapple go评论47阅读模式
英文:

Experiencing binding errors in the Command property of by Button within a DataGrid

问题

以下是你要翻译的部分:

My 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>

From my 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... 
       }
}

When I run this, I get binding errors on the Command property of the datagrid button. I do use the GetSettings() method on another button on the view that is not on the DataGrid. Everything else between the view and the viewmodel works, the datagrid is showing the data I expect. It's just the button that's not working.

英文:

I'm trying to use a button in a WPF DataGrid. My datagrid ItemsSource is bound to a class, but I want the button to call an ICommand in a ViewModel. I've looked at a dozen or more posts on this, and still can't get this to work.

My 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>

From my 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... 
       }
}

When I run this, I get binding errors on the Command property of the datagrid button. I do use the GetSettings() method on another button on the view that is not on the DataGrid. Everything else between the view and the viewmodel works, the datagrid is showing the data I expect. It's just the button that's not working.

答案1

得分: 1

尝试这样做:

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

在`CellTemplate`中,您不能使用绑定中的`ElementName`引用父级`DataGrid`,因为它位于不同的命名范围中。`AncestorType`应该设置为可视树中元素的类型。

<details>
<summary>英文:</summary>

Try this:

    &lt;Button Content=&quot;View&quot;
            Command=&quot;{Binding DataContext.GetSettingsCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}&quot; 
            CommandParameter=&quot;{Binding SelectedItem, RelativeSource={RelativeSource AncestorType=DataGrid}}&quot;  ... /&gt;

You can&#39;t refer to the parent `DataGrid` using an `ElementName` in the binding in the `CellTemplate` because it&#39;s in a different naming scope. And the `AncestorType` should be set to a type of an element in the visual tree.

</details>



huangapple
  • 本文由 发表于 2023年4月19日 22:09:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055518.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定