开发者

Binding a buttons IsEnabled/Visibility properties using its x:Name as a parameter in a method in the code-behind

I need to pass in the controls name to a method in a security object that returns a boolean value for the IsEnabled property and another method that returns its Visibility(Collapsed, Hidden, or Visible). These both have to be checked for permission purposes.

I have tried using an ObjectDataProvider but all the examples show only user input from a textbox for the parameters. I specifically need to pass a control name to the method based off the button's x:Name property.

What is the simplest and most efficient way of handling this problem. Thanks in advance.

UPDATE: I am trying to use a converter and this is the convert method I came up with:

    public object Convert(object[] values, Type targetType, object paramet开发者_运维百科er, 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;
    }

The following is the xaml:

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


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.


There may be different ways to approach this problem depending on the way you've architected your application. If you're using user control views and depending on code-behind your easiest route may be to call the security object's methods directly from the code behind and set properties directly on the controls in question.

If you're using MVVM or you're just not a fan of code-behind, another way around this problem may be to ditch using the name of the button and go with an attached property. Attached properties are a way of using the WPF dependency property framework to store data about an object or control that the object or control didn't originally declare itself.

Along with attached properties comes a concept called attached behaviors. Essentially, when you create an attached property, you're given a hook for a callback that's called whenever the property is set on an object. When this callback is called, you receive the object the property was set on as well as new and old values for the property.

You can use the callback as an opportunity to check the value of the property against your security object and set the enabled and visible properties as you see fit.

-- HTH Dusty

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜