c# - 特定类型的数据触发器

标签 c# wpf xaml datagrid triggers

我有一个需要指定函数的场景

void SomeFunction(int value)

为此,我使用了两个 DataGrid

  • 左边的 DataGrid 包含函数
  • 右侧的 DataGrid 包含所选函数的参数

我只希望在函数 DataGrid 中选择有效函数时启用参数 DataGrid。如果 NewItemPlaceHolder (当 CanUserAddRows="True" 用于 DataGrid 时的最后一行) 被选中,或者如果选择为空我希望它被禁用。我尝试使用 DataTrigger,但无法让它工作

<Style TargetType="DataGrid">
    <Setter Property="IsEnabled" Value="False"/>
    <Style.Triggers>
        <DataTrigger Binding="{Binding ElementName=functionDataGrid,
                                       Path=SelectedItem}"
                     Value="{x:Type systemData:DataRowView}">
            <Setter Property="IsEnabled" Value="True"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

是否可以检查 Binding 产生的值是否属于特定的 Type?否则,有人对此有任何其他解决方案吗?截至目前,我正在使用 SelectedCellsChanged 事件处理此问题,但我宁愿不使用代码隐藏

谢谢

最佳答案

如果其他人遇到同样的问题,这就是我所做的解决方法。我创建了一个 TypeOfConverter,它返回 Binding 产生的值的 Type

<DataTrigger Binding="{Binding ElementName=functionsDataGrid,
                               Path=SelectedItem,
                               Converter={StaticResource TypeOfConverter}}"
             Value="{x:Type data:DataRowView}">
    <Setter Property="IsEnabled" Value="True"/>
</DataTrigger>

TypeOfConverter

public class TypeOfConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (value == null) ? null : value.GetType();
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

关于c# - 特定类型的数据触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5091695/

相关文章:

c# - 使用 XPath 查找所有 JavaScript 类型脚本元素

c# - 序列化 api 数据时出错 : cannot be serialized to JSON because its IsReference setting is 'True'

c# - 无法将 winform 控件大小传递给非托管代码

c# - WPF 列表框两个项目并排

wpf - 缩放控件到 WPF 表单

wpf - 为什么 KeyUp 和 KeyDown 事件变慢?

.net - 如何创建 UI 设计器实用程序?

c# - 将 xaml 模板中的属性绑定(bind)到 viewmodel (RibbonTextBox)

c# - 在声明数组时将 C++ 代码转换为 C#

wpf - 如何为绑定(bind)到 viewmodel 属性的 WPF 控件设置动画?