William Jockusch
William Jockusch

Reputation: 27295

Bind to result of static class' method

I have the following:

namespace Foo {
  public static class Bar {
    public static int Fubar() {
      return 100;
    }
  }
}

Now I'm in xaml. I want to use that method to set the height of my rectangle.

<Rectangle Height="{Binding Source=???}">

Upvotes: 0

Views: 386

Answers (2)

doerig
doerig

Reputation: 1857

You need an ObjectDataProvider to bind to a method.

Example (adjust to your NameSpace / Class / Method):

<Window x:Class="SerialPortBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ports="clr-namespace:System.IO.Ports;assembly=System"
        Title="MainWindow" SizeToContent="WidthAndHeight">
    <Window.Resources>
        <ObjectDataProvider ObjectType="{x:Type ports:SerialPort}" 
            MethodName="GetPortNames" x:Key="portNames"/>
    </Window.Resources>
    <ComboBox ItemsSource="{Binding Source={StaticResource portNames}}"/>
</Window>

The assembly portion of the xmnls at the top may not be needed; ignore it if it does not come up in CodeSense.

Upvotes: 2

AjS
AjS

Reputation: 339

Try something like this:-

{Binding Source={x:Static classnamespace:Bar}, Path=Fubar}

Upvotes: 0

Related Questions