英文:
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:
<Button Content="View"
Command="{Binding DataContext.GetSettingsCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}"
CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource AncestorType=DataGrid}}" ... />
You can't refer to the parent `DataGrid` using an `ElementName` in the binding in the `CellTemplate` because it's in a different naming scope. And the `AncestorType` should be set to a type of an element in the visual tree.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论