如何将 DataGrid:Cell 中的值绑定到 DataTrigger

How to bind value from DataGrid:Cell to DataTrigger

我的问题:

我被下面的代码卡住了:

<xcdg:Column FieldName="OpenInPiWebIdentifier"
             Title="PiWeb Report"
             VisiblePosition="6">
    <xcdg:Column.CellContentTemplate>
        <DataTemplate>
            <Button DataContext="{Binding RelativeSource={RelativeSource Self}, Path=(xcdg:DataGridControl.ParentDataGridControl).DataContext}"
                    Content="Open PiWeb"
                    Command="{Binding OpenPiWebCommand}"
                    CommandParameter="{Binding DataContext.ResultData/PiWebReport, Mode=OneWay, RelativeSource={RelativeSource Self}}">
                <Button.Style>
                    <Style TargetType="Button">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding PiWebReport}"
                                         Value="{x:Null}">
                                <Setter Property="IsEnabled"
                                        Value="False" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>
        </DataTemplate>
    </xcdg:Column.CellContentTemplate>
</xcdg:Column>

以及命令的视图模型实现:

public ICommand OpenPiWebCommand
{
    get
    {
        return _OpenPiWebCommand;
    }

    set
    {
        if (value != _OpenPiWebCommand)
        {
            _OpenPiWebCommand = value;
            OnPropertyChanged("OpenPiWebCommand");
        }
    }
}

private ICommand _OpenPiWebCommand;

在构造函数中我初始化命令:

OpenPiWebCommand = new RelayCommand(new Action<object>(OpenPiWeb));

属性PiWebReport属于classResultDataV1。所有数据都放入这个属性ResultData = new ObservableCollection<ResultDataV1>();DataGrid 中显示为 columns/rows。 因此,现在我可以访问 属性 值 PiWebReport 或命令,具体取决于我使用的上下文,但不能同时访问两者。

数据class:

public class ResultDataV1
{
    public ResultDataV1(long dataId, DateTime dateTime, DateTime? endDateTime, string partId, string scanId, bool measurementNotVolumejoin, string piWebReport, FileInfo volumeFileInfo, FileInfo volumeVglFileInfo, DirectoryInfo volumeDirectoryInfo, string inspectionPlanName, string paletteName, AutomationStateEnum? automationState);

    public AutomationStateEnum? AutomationState { get; }
    public long DataId { get; }
    public DateTime DateTime { get; }
    public DateTime? EndDateTime { get; }
    public string InspectionPlanName { get; }
    public bool MeasurementNotVolumejoin { get; }
    public string PaletteName { get; }
    public string PartId { get; }
    public string PiWebReport { get; }
    public string ScanId { get; }
    public List<SubResultDataV1> SubResults { get; }
    public DirectoryInfo VolumeDirectoryInfo { get; }
    public FileInfo VolumeFileInfo { get; }
    public FileInfo VolumeVglFileInfo { get; }
}

主要思想是使用 ICommand.CanExecute Method. Initially the button becomes enabled or disabled based on the value of the method (of the bound ICommand instance). Also, firing ICommand.CanExecuteChanged Event 可用于通知用户界面 (Button class) 关于 属性 更改。

结构的实现:

internal sealed class ResultData
{
    private readonly string piWebReport;

    public ResultData(string piWebReport)
    {
        this.piWebReport = piWebReport;
    }

    public string PiWebReport
    {
        get { return this.piWebReport; }
    }
}

执行 ViewModel class:

internal sealed class MainViewModel
{
    private readonly IEnumerable<ResultData> items = new[]
        {
            new ResultData("Report 00"),
            new ResultData("Report 01"),
            new ResultData("Report 02"),
            new ResultData(null)
        };

    private readonly ICommand openPiWebCommand;

    public MainViewModel()
    {
        openPiWebCommand = new RelayCommand<ResultData>(
            OpenPiWeb,
            x => x != null && x.PiWebReport != null);
    }

    public IEnumerable<ResultData> Items
    {
        get { return items; }
    }

    public ICommand OpenPiWebCommand
    {
        get { return openPiWebCommand; }
    }

    private void OpenPiWeb(ResultData data)
    {
        // Implement the command logic.
    }
}

XAML:

<Window x:Class="..."
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
        ...>
    <Grid>
        <xcdg:DataGridControl ItemsSource="{Binding Items}">
            <xcdg:DataGridControl.Columns>
                <xcdg:Column FieldName="PiWebReport"
                             Title="PiWeb Report">
                    <xcdg:Column.DisplayMemberBindingInfo>
                        <xcdg:DataGridBindingInfo Path="." ReadOnly="True" />
                    </xcdg:Column.DisplayMemberBindingInfo>

                    <xcdg:Column.CellContentTemplate>
                        <DataTemplate>
                            <Grid>
                                <Button Content="Open PiWeb"
                                        CommandParameter="{Binding}"
                                        Command="{Binding RelativeSource={RelativeSource Self}, Path=(xcdg:DataGridControl.ParentDataGridControl).DataContext.OpenPiWebCommand}" />
                            </Grid>
                        </DataTemplate>
                    </xcdg:Column.CellContentTemplate>
                </xcdg:Column>
            </xcdg:DataGridControl.Columns>
        </xcdg:DataGridControl>
    </Grid>
</Window>

参考文献:

希望这对你有用!