Datagrid&中的命令参数menucontext给出null

时间:2018-02-09 11:15:16

标签: c# wpf mvvm datagrid

我在使用MVVM尝试从DataMrid中的ContextMenu获取参数时花费了几个小时。

来自CommandParameter的参数始终为null,不在set {Binding}之内,但它不是我想要的。

我是WPF的新手,所以对我来说,从这里和其他人的问题中阅读答案是没有帮助的。它始终保持为空。 我的代码如下:

 <DataGrid  Grid.Row="2"  Margin="25,0,0,4" SelectionMode="Single" AlternationCount="2"   Name="dgAltPart"  AutoGenerateColumns="False" ItemsSource="{Binding Path=AltPartResult}" HorizontalAlignment="Left"   VerticalAlignment="Top" 
                      ScrollViewer.CanContentScroll="True" 
                      ScrollViewer.VerticalScrollBarVisibility="Auto"
                      ScrollViewer.HorizontalScrollBarVisibility="Auto">

                        <DataGrid.ContextMenu>
                            <ContextMenu >
                                <MenuItem Header="Delete" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget.manufacturer}"
                                  Command="{Binding DeleteManufacturerCommand}"/>
                            </ContextMenu>
                        </DataGrid.ContextMenu>

                        <DataGrid.Columns>


                            <DataGridTextColumn Header="Manufacturer" Width="175"  Binding="{Binding manufacturer}"></DataGridTextColumn>
                            <DataGridTextColumn Header="Manufacturer Part Number" Width="200"  Binding="{Binding manufacturer_pn}"></DataGridTextColumn>
                            <DataGridTextColumn Header="Price" Width="100"  Binding="{Binding price}"></DataGridTextColumn>

                        </DataGrid.Columns>


                    </DataGrid>

我的ViewModel:

 private bool canExecute = true;
    public ICommand DeleteManufacturerCommand
    {
        get
        {
            if (_deleteManufacturerCommand == null)
            {
                _deleteManufacturerCommand = new RelayCommand(DeleteManufacturer, param => this.canExecute);

            }
            return _deleteManufacturerCommand;
        }
    }

    public void DeleteManufacturer(object obj)
    {

    }

在DeleteManufacturer()中,我总是得到带有null的obj。

到目前为止尝试了各种相关来源。 我错过了什么?

2 个答案:

答案 0 :(得分:1)

如果在行级别定义set ContextMenu属性,则可以直接绑定到每行的DataContext。试试这个:

<DataGrid Grid.Row="2" Margin="25,0,0,4" SelectionMode="Single" AlternationCount="2"
                Name="dgAltPart"  AutoGenerateColumns="False" 
                ItemsSource="{Binding Path=AltPartResult}" HorizontalAlignment="Left"   VerticalAlignment="Top" 
                ScrollViewer.CanContentScroll="True" 
                ScrollViewer.VerticalScrollBarVisibility="Auto"
                ScrollViewer.HorizontalScrollBarVisibility="Auto">
    <DataGrid.ItemContainerStyle>
        <Style TargetType="DataGridRow">
            <Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}}" />
            <Setter Property="ContextMenu">
                <Setter.Value>
                    <ContextMenu>
                        <MenuItem Header="Delete"
                                          CommandParameter="{Binding}"
                                          Command="{Binding PlacementTarget.Tag.DataContext.DeleteManufacturerCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
                    </ContextMenu>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGrid.ItemContainerStyle>
    <DataGrid.Columns>
        <DataGridTextColumn Header="Manufacturer" Width="175"  Binding="{Binding manufacturer}"></DataGridTextColumn>
        <DataGridTextColumn Header="Manufacturer Part Number" Width="200"  Binding="{Binding manufacturer_pn}"></DataGridTextColumn>
        <DataGridTextColumn Header="Price" Width="100"  Binding="{Binding price}"></DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

答案 1 :(得分:0)

private Manufacturer selectedManufacturer;

    public Manufacturer SelectedManufacturer
    {
        get { return selectedManufacturer; }
        set { selectedManufacturer = value; OnPropertyChanged(); }
    }

并且您的ViewModel应该具有如下属性:

.tooltip .tooltip-inner {
  background-color: green;
}

.tooltip .arrow::before {
  border-left-color: green;
}