我正在开发一个MVVM项目,我在其中一个视图中有这个代码:
<GroupBox Header="Defaut" BorderBrush="#FF4EA8DE" FontSize="16" Foreground="#FF436EFF" >
<DataGrid Background="Transparent" FontSize="14" CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="True" AutoGenerateColumns="False" Style="{x:Null}"
ItemsSource="{Binding ErrorList}">
<DataGrid.Columns>
<DataGridTextColumn Width="0.5*" Header="{DynamicResource Numéro Cordon}" Binding="{Binding BeadName}"></DataGridTextColumn>
<DataGridTextColumn Width="0.5*" Header="{DynamicResource Indice Image}" Binding="{Binding IndiceImage}"></DataGridTextColumn>
<DataGridTextColumn Width="0.5*" Header="{DynamicResource Défaut}" Binding="{Binding DispDefault}"></DataGridTextColumn>
<DataGridTemplateColumn Header="{DynamicResource Criticité}" Width="0.5*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding IsError, Converter={StaticResource IsErrorToCriticityLevel}, Mode=OneWay}"></Label>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="CmdB:CommandBehavior.Event" Value="MouseDown" />
<Setter Property="CmdB:CommandBehavior.Command" Value="{Binding DataContext.RobotErrorSelectionChangedCommand,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type controls:MetroWindow}}}"/>
<Setter Property="CmdB:CommandBehavior.CommandParameter" Value="{Binding Path=SelectedItem, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="BorderBrush" Value="#FF6593CF" />
<Setter Property="Background" Value="#FF6593CF" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding RobotErrorSelectionChangedCommand}" CommandParameter="{Binding Path=SelectedItem, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
</GroupBox>
我想要做的是能够取消选择此列表中的所选项目,但我无法找到如何访问它。
以下是与此列表相关的ViewModel中的代码:
ObservableCollection<Erreur> _ErrorList;
public ObservableCollection<Erreur> ErrorList
{
get { return _ErrorList; }
set { _ErrorList = value; RaisePropertyChanged("ErrorList");}
}
private RelayCommand<Erreur> _RobotErrorSelectionChangedCommand;
public RelayCommand<Erreur> RobotErrorSelectionChangedCommand
{
get
{
return _RobotErrorSelectionChangedCommand
?? (_RobotErrorSelectionChangedCommand = new RelayCommand<Erreur>(
(Erreur err) =>
{
if (err != null)
{
viewservice.OpenDialog(new ErreurImageViewModel(err), ServiceLocator.Current.GetInstance<MainViewModel>());
}
}));
}
}
感谢您提供任何帮助或建议。
答案 0 :(得分:4)
您可以将Datagrid中的SelectedItem
属性绑定到VM中的属性,并清除当前选择,您可以将属性设置为:null
。这样,您可以随时通过代码取消选择SelectedItem
。
您可以在视图中将其绑定到这样:
<DataGrid ItemsSource="{Binding ErrorList}" SelectedItem="{Binding SelectedError}" ...>
然后在ViewModel中添加:
private Erreur _selectedError = null;
public Erreur SelectedError
{
get => _selectedError;
set
{
if (_selectedError == value) return;
_selectedError = value;
RaisePropertyChanged(nameof(SelectedError));
}
}
每当您想要清除选择时,您可以这样做:
SelectedError = null;
如果你想从代码中选择一个特定的实例,你可以这样做:
SelectedError = myInstanceOfError;
答案 1 :(得分:0)
将SelectedError
属性绑定到XAML中的SelectedItem
属性。
XAML:
<DataGrid Background="Transparent" FontSize="14" CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="True" AutoGenerateColumns="False" Style="{x:Null}" ItemsSource="{Binding ErrorList}" SelectedItem="{Binding SelectedError}">
C#属性:
private Erreur _SelectedError;
public Erreur SelectedError
{
get { return _SelectedError; }
set {
if(_SelectedError != value)
{
_SelectedErrorList = value;
RaisePropertyChanged("SelectedError");
}
}
}
答案 2 :(得分:0)
谢谢,两个答案都是正确的,稍加修改,我添加了:
SelectedItem="{Binding SelectedError}" in the XAML code.
我必须对此部分进行评论以禁用该命令:
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding RobotErrorSelectionChangedCommand}" CommandParameter="{Binding Path=SelectedItem, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
现在SelectedError获取Selected Item.Thanx