我将RelayCommand绑定到datagrid中的按钮时遇到了问题,并且已经将ItemSource绑定到ObservableCollection。
我的问题是RelayCommand(RemoveCommand)不可见,无法从viewmodel执行。
<Button Grid.Column="0" Style="{StaticResource MaterialDesignFloatingActionMiniDarkButton}"
ToolTip="Remove"
Command="{Binding Path=RemoveCommand}">
<materialDesign:PackIcon Kind="Close" Height="24" Width="24" />
</Button>
仅可见的是像IconName这样的类中的方法,它们是从以前的ItemSource绑定中产生的
<WindowsControl:DataGrid ItemsSource="{Binding ListOfExcelFiles ,IsAsync=True,Mode=TwoWay}"
但是我想从viewmodel中使用RelayCommand,可以通过某种方式执行此操作,或者我需要重写代码?
ViewModel:
private ObservableCollection<ExcelFile> _listOfExcelFiles;
public ObservableCollection<ExcelFile> ListOfExcelFiles
{
get { return _listOfExcelFiles; }
set
{
_listOfExcelFiles = value;
OnPropertyChanged();
}
}
型号:
public class ExcelFile : INotifyPropertyChanged
{
string _name;
string _filePath;
string _iconName = "File";
string _validationIcon = "";
bool _showbutton = false;
public bool ShowButton
{
get { return _showbutton; }
set
{
_showbutton = value;
OnPropertyChanged();
}
}
public string IconName
{
get { return _iconName; }
set
{
_iconName = value;
}
}
public string Name
{
get { return _name; }
set
{
_name = value;
}
}
public string FilePath
{
get { return _filePath; }
set
{
_filePath = value;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
我的XAML:
<WindowsControl:DataGrid ItemsSource="{Binding ListOfExcelFiles
,IsAsync=True,Mode=TwoWay}"
AutoGenerateColumns="False"
Grid.Column="0" Grid.Row="0"
SelectionMode="Single"
SelectionUnit="FullRow"
CanUserAddRows="false"
SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Icon" CellStyle="{StaticResource CenteredCell}">
<DataGridTemplateColumn.CellTemplate >
<DataTemplate >
<materialDesign:PackIcon Kind="{Binding IconName}"
Height="24" Width="24" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="FileName"
Width="*" IsReadOnly="True"
CellStyle="{StaticResource CenteredLeftCell}" >
<DataGridTextColumn.Binding>
<Binding Path="Name"
NotifyOnTargetUpdated="True"
UpdateSourceTrigger="PropertyChanged"
Mode="TwoWay">
</Binding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
<DataGridTextColumn Header="FilePath"
Binding="{Binding FilePath}"
Width="*" IsReadOnly="True"
CellStyle="{StaticResource CenteredLeftCell}"></DataGridTextColumn>
<DataGridTemplateColumn CellStyle="{StaticResource CenteredCell}" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid Visibility="{Binding ShowButton,Converter={StaticResource BoolToVisibility},FallbackValue=Hidden}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Button Grid.Column="0" Style="{StaticResource MaterialDesignFloatingActionMiniDarkButton}"
ToolTip="Remove"
Command="{Binding Path=RemoveCommand}">
<materialDesign:PackIcon Kind="Close" Height="24" Width="24" />
</Button>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</WindowsControl:DataGrid>
答案 0 :(得分:3)
在Binding中将RelativeSource与FindAncestor一起使用,并引用DataGrid的DataContext(...您的ViewModel)
Command={Binding Path=DataContext.RemoveCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}
RelativeSource使绑定在应用提供的路径之前搜索提供的类型的祖先。