3

我需要将控件名称传递给安全对象中的一个方法,该方法返回 IsEnabled 属性的布尔值和另一个返回其可见性(折叠、隐藏或可见)的方法。出于许可目的,必须检查这两者。

我尝试使用 ObjectDataProvider,但所有示例仅显示来自文本框的用户输入参数。我特别需要根据按钮的 x:Name 属性将控件名称传递给方法。

处理这个问题的最简单和最有效的方法是什么。提前致谢。

更新: 我正在尝试使用转换器,这是我想出的转换方法:

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (values != null)
        {
            DataTable tblPermissions = (DataTable)values[0];
            string sFunctionName = values[1].ToString();

            DataRow[] rows = tblPermissions.Select("fun_name = '" + sFunctionName + "'");
            if ((bool)rows[0]["fun_enable"])
                return true;
            else
                return false;
        }

        return string.Empty;
    }

以下是xaml:

                    <Button.IsEnabled>
                        <MultiBinding Converter="{StaticResource IsFunctionEnabledConverter}">
                            <Binding ElementName="{StaticResource PermissionsTable}" />
                            <Binding ElementName="btnSave" Path="Name" />
                        </MultiBinding>
                    </Button.IsEnabled>
4

2 回答 2

2

You can write an IValueConverter to make the method call and pass in the control itself using {Binding RelativeSource={RelativeSource Self}, Converter={StaticResource MyConverter}}. In the Convert method you can then cast value to Control and access the Control's Name property to pass to the security method. By checking the targetType you can determine whether to output a boolean (for IsEnabled) or Visibility enum.

***UPDATE

I assume that the "PermissionTable" resource used with your converter binding is actually the DataTable but you're trying to use it as a string to identify an element by name as the Binding source. Try using Source="{StaticResource PermissionsTable}" instead to pass the DataTable resource itself.

于 2010-12-01T15:10:21.543 回答
0

根据您构建应用程序的方式,可能有不同的方法来解决此问题。如果您正在使用用户控件视图并且取决于代码隐藏,那么您最简单的方法可能是直接从后面的代码调用安全对象的方法并直接在相关控件上设置属性。

如果您正在使用 MVVM 或者您只是不喜欢代码隐藏,则解决此问题的另一种方法可能是放弃使用按钮的名称并使用附加属性。附加属性是一种使用 WPF 依赖属性框架来存储有关对象或控件的数据的方法,该对象或控件最初并未声明其自身。

与附加属性一起出现的概念称为附加行为。本质上,当您创建附加属性时,您将获得一个回调挂钩,只要在对象上设置该属性,就会调用该挂钩。调用此回调时,您会收到设置属性的对象以及该属性的新旧值。

您可以使用回调作为一个机会,根据您的安全对象检查属性的值,并根据需要设置启用和可见的属性。

-- HTH 尘土飞扬

于 2010-12-01T15:06:26.443 回答