Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将值从DataGrid:Cell绑定到DataTrigger_C#_Wpf_Xceed Datagrid - Fatal编程技术网

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

C# 如何将值从DataGrid:Cell绑定到DataTrigger,c#,wpf,xceed-datagrid,C#,Wpf,Xceed Datagrid,我的问题是: 我有一个带有按钮的XceedDataGrid列,它绑定到一个命令(该部分工作) 现在,我希望根据当前数据行中的特定值启用/禁用按钮 到目前为止,根据我使用的DataContext,我可以获得命令bind work或DataTrigger,但不能同时获得两者 我被以下代码困住了: <xcdg:Column FieldName="OpenInPiWebIdentifier" Title="PiWeb Report" Visi

我的问题是:

  • 我有一个带有按钮的Xceed
    DataGrid
    列,它绑定到一个命令(该部分工作)
  • 现在,我希望根据当前
    数据行中的特定值启用/禁用按钮

  • 到目前为止,根据我使用的
    DataContext
    ,我可以获得命令bind work或
    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));

主要思想是使用。最初,按钮根据方法(绑定的
ICommand
实例)的值启用或禁用。此外,激发还可用于通知用户界面(
按钮
类)有关属性更改的信息

结构的实施:

internal sealed class ResultData
{
    private readonly string piWebReport;

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

    public string PiWebReport
    {
        get { return this.piWebReport; }
    }
}
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.
    }
}
内部密封类MainViewModel
{
私有只读IEnumerable items=new[]
{
新结果数据(“报告00”),
新结果数据(“报告01”),
新结果数据(“报告02”),
新结果数据(空)
};
专用只读ICommand和openPiWebCommand;
公共主视图模型()
{
openPiWebCommand=新的RelayCommand(
OpenPiWeb,
x=>x!=null&&x.PiWebReport!=null);
}
公共数字项目
{
获取{返回项;}
}
公共ICommand和OpenPiWebCommand
{
获取{return openPiWebCommand;}
}
私有void OpenPiWeb(结果数据)
{
//实现命令逻辑。
}
}
XAML:


参考资料:

  • 列的示例。DisplayMemberBindingInfo
    用法:

希望这对你有用

“但不能同时做到这两个。”你想实现什么?“两者”同时出现?能否请您发布
ViewModel
的实现,其中包含
OpenPiWebCommand
属性的定义?通过添加视图模型实现更新帖子。谢谢!但是关于“两者”还有一个悬而未决的问题(请看第一条评论)。好的,对不起,没有阅读第一条评论!我指的是“OpenPiWebCommand”的绑定和DataTrigger的绑定。非常感谢您的提示和示例!CanExecute方法绝对是正确的方法,但即使尝试了,我也只启用/禁用了列的所有按钮,而不仅仅是每个单元格。我最终认为我必须选择特定的上下文:
,这非常有效,但无论如何,我现在将其更改为CanExecute方法方法,以使其符合应有的方式。
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.
    }
}
<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>