0

I am trying to bind an XmlDataProvider with a Source attribute to a static function in another form.

Here's XmlDataProvider line -

<XmlDataProvider x:Key="Lang" Source="/lang/english.xml" XPath="Language/MainWindow"/>

I would like it's Source attribute to be binded to a static function called: "GetValue_UILanguage" in a form called: "Settings"

Wenfang Du
  • 8,804
  • 9
  • 59
  • 90
dinbrca
  • 1,101
  • 1
  • 14
  • 30

1 Answers1

1

See this question's answer for a converter that allows you to bind to methods.

You could probably modify it to be able to access static methods of any class as well.

Edit: Here's a modified converter that should find the method via reflection.

(Note: You would be better off using a markup extension instead, as you do not actually bind any value.)

public sealed class StaticMethodToValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        try
        {
            var methodPath = (parameter as string).Split('.');
            if (methodPath.Length < 2) return DependencyProperty.UnsetValue;

            string methodName = methodPath.Last();

            var fullClassPath = new List<string>(methodPath);
            fullClassPath.RemoveAt(methodPath.Length - 1);
            Type targetClass = Assembly.GetExecutingAssembly().GetType(String.Join(".", fullClassPath));

            var methodInfo = targetClass.GetMethod(methodName, new Type[0]);
            if (methodInfo == null)
                return value;
            return methodInfo.Invoke(null, null);
        }
        catch (Exception)
        {
            return DependencyProperty.UnsetValue;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException("MethodToValueConverter can only be used for one way conversion.");
    }
}

Usage:

<Window.Resources>
    ...
    <local:StaticMethodToValueConverter x:Key="MethodToValueConverter"/>
    ...
</Window.Resources>

...

<ListView ItemsSource="{Binding Converter={StaticResource MethodToValueConverter}, ConverterParameter=Test.App.GetEmps}">
...

The method in the App class:

namespace Test
{
    public partial class App : Application
    {
        public static Employee[] GetEmps() {...}
    }
}

I tested this and it works, it is important to use the full class path though, App.GetEmps alone would not have worked.

Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • if not from a static function, is it possible to take a value from static variable? how? – dinbrca Feb 16 '11 at 20:55
  • For that you can use `{x:Static namespace:SomeClass.StaticProperty}` – H.B. Feb 16 '11 at 20:59
  • I have already tried to do that but when i start the program it gives me a bug report. I have added: xmlns:local="clr-namespace:Visual_Command_Line" as namespace. And my code: – dinbrca Feb 16 '11 at 21:42
  • What is the error you get? Is the `Settings` class in the namespace you specified? – H.B. Feb 16 '11 at 21:48
  • the "Settings" class is in the namespace i specified.. I don't get any error, right when I start the program i get a message saying my program has stopped working with 3 buttons: "check online for a solution", "close the program", "debug the program" - the usual windows don't send / send bug report – dinbrca Feb 16 '11 at 22:03
  • Why is that? Aren't you using any debugger? – H.B. Feb 16 '11 at 22:09
  • Obvious follow up question: Whyyyyy? I mean seriously, how can you even get anything done without a debugger? And i can't help you either if all i know is that your program, for which i don't have any code, crashed. – H.B. Feb 16 '11 at 22:51
  • i tried using the debugger in c# it gives me the following message: "XamlParseException occurred" : "Cannot convert the value in attribute 'Source' to object of type 'System.Uri'. Object of type 'System.String' cannot be converted to type 'System.Uri'. Error at object 'Lang' in markup file 'Visual_Command_Line;component/mainwindow.xaml'." what should i do? – dinbrca Feb 16 '11 at 23:16
  • well, I guess i should have used C# debugger - it said everything i need to do. xD. thanks! – dinbrca Feb 16 '11 at 23:23