DataGrid中的属性索引器

时间:2015-03-26 14:16:57

标签: c# wpf xaml mvvm

我有一个我想在我的数据网格中使用的上下文菜单。在每一行,我都希望菜单显示

  1. 如果IsRunToTest == false,则仅显示第一个菜单项
  2. 如果IsRunToTest == true,则仅显示第二个菜单项
  3. 是否可以根据属性IsRunToTest设置每行显示两个菜单项之一?我想知道是否需要将我的IsRunToTest属性设置为索引器,但不确定如何执行此操作并使其适用于每一行。

    <DataGrid ItemsSource="{Binding TestResults}" 
        SelectedItem="{Binding SelectedTestResult}">
    <DataGrid.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Run To Selected Test" 
                Command="{Binding RunToSelectedTestAction}"
                Visibility="{Binding IsRunToTestSet, 
                Converter={StaticResource BoolToVisibilityConverter}, 
                ConverterParameter=Visible}" />
    
            <MenuItem Header="Remove Run To Selected Test" 
                Command="{Binding RemoveRunToSelectedTestAction}" 
                Visibility="{Binding IsRunToTestSet, 
                Converter={StaticResource BoolToVisibilityConverter}, 
                ConverterParameter=Collapsed}" />
        </ContextMenu>
    </DataGrid.ContextMenu>
    
    ...Defined Columns
    
    </DataGrid>
    
    ViewModel Properties
    
    public ObservableCollection<TestResultsModel> TestResults
    {
        get
        {
            return this.testResults;
        }
    
        set
        {
            this.testResults = value;
    
            this.NotifyPropertyChanged(m => m.TestResults);
        }
    }
    
    public TestResultsModel SelectedTestResult
    {
        get
        {
            return this.selectedTestResult;
        }
    
        set
        {
            this.selectedTestResult = value;
    
            this.NotifyPropertyChanged(m => m.SelectedTestResult);
        }
    }
    
    public bool IsRunToPCodeSet
    {
        get
        {
            return this.SelectedTestResult.IsRunToPCodeSet;
        }
    
        set
        {
            this.SelectedTestResult.IsRunToPCodeSet = value;
    
            this.NotifyPropertyChanged(m => m.IsRunToPCodeSet);
        }
    }
    

0 个答案:

没有答案