简体   繁体   English

如何将值从DataGrid:Cell绑定到DataTrigger

[英]How to bind value from DataGrid:Cell to DataTrigger

My Problem: 我的问题:

  • I have a Xceed DataGrid column with a button, which is bound to a Command (that part works) 我有一个带有按钮的Xceed DataGrid列,该列绑定到Command(该部分有效)
  • Now I want the button to be enabled/disabled depending on a certain value in the current DataRow . 现在,我希望根据当前DataRow的某个值来启用/禁用该按钮。
  • So far I can either get the command bind work or the DataTrigger depending on the DataContext I use, but not both at the same time. 到目前为止,我可以根据我使用的DataContext获得命令绑定工作或DataTrigger ,但不能同时使用两者。

I am stuck with the following code: 我坚持下面的代码:

<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>

And the view model implementation of the command: 和视图模型的命令执行:

public ICommand OpenPiWebCommand
{
    get
    {
        return _OpenPiWebCommand;
    }

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

private ICommand _OpenPiWebCommand;

In the constructor I initialize the command: 在构造函数中,我初始化命令:

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

The property PiWebReport belongs to a class ResultDataV1 . PiWebReport属性属于ResultDataV1类。 All data is put into this property ResultData = new ObservableCollection<ResultDataV1>(); 所有数据都放入此属性中ResultData = new ObservableCollection<ResultDataV1>(); which is displayed in the DataGrid as columns/rows. DataGrid显示为列/行。 So, now I can access either the property value PiWebReport or the command, depending on the context I use, but not both at the same time. 因此,根据我使用的上下文,现在我可以访问属性值PiWebReport或命令,但不能同时访问两者。

The data 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; }
}

The main idea is to use the ICommand.CanExecute Method . 主要思想是使用ICommand.CanExecute方法 Initially the button becomes enabled or disabled based on the value of the method (of the bound ICommand instance). 最初,按钮会根据(绑定的ICommand实例的)方法的值启用或禁用。 Also, firing ICommand.CanExecuteChanged Event can be used to notify the user-interface ( Button class) about the property change. 同样,触发ICommand.CanExecuteChanged事件可用于将属性更改通知用户界面( Button类)。

The implementation of the structure: 执行结构:

internal sealed class ResultData
{
    private readonly string piWebReport;

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

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

The implementation of the ViewModel class: ViewModel类的实现:

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: 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>

References: 参考文献:

Hope this is useful for you! 希望这对您有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM