3

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

The following is the xaml:

                    <Button.IsEnabled>
                        <MultiBinding Converter="{StaticResource IsFunctionEnabledConverter}">
                            <Binding ElementName="{StaticResource PermissionsTable}" />
                            <Binding ElementName="btnSave" Path="Name" />
                        </MultiBinding>
                    </Button.IsEnabled>
Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
jes9582
  • 253
  • 4
  • 15

2 Answers2

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.

John Bowen
  • 24,213
  • 4
  • 58
  • 56
  • I have tried using a Converter, the problem I ran into is that the converter needed me to pass in the security object to access the permissions. I created a multivalue converter that uses the name of the control and the table for permissions. I receive this error when I try and use the converter: "Unable to cast object of type 'System.Data.DataTable' to type 'System.String'." – jes9582 Dec 01 '10 at 15:28
  • That just sounds like a problem with your converter code pulling out items from the values[] in the wrong order but I can't tell exactly what without seeing the code. – John Bowen Dec 01 '10 at 15:44
  • I posted a code sample in the original question, let me know if u see anything wrong but they should be in the correct order. Thanks! – jes9582 Dec 01 '10 at 15:53
0

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

dustyburwell
  • 5,755
  • 2
  • 27
  • 34