0

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=???}">
Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
William Jockusch
  • 26,513
  • 49
  • 182
  • 323
  • 1
    Possible duplicate of [Binding to static class property](http://stackoverflow.com/questions/3862455/binding-to-static-class-property)... but you'd have know that and saved time if you just typed your question title into a search engine first. – Sheridan Dec 06 '13 at 08:59
  • 1
    Why on earth would you title your question '*WPF bind to property of static class*' when you want to '*bind to a method of a static class*'? – Sheridan Dec 06 '13 at 09:49
  • This question is asking how to bind to a static class *method*, while the linked question is asking how to bind to a static class *property*. There is a difference – Rachel Feb 28 '14 at 14:44

2 Answers2

2

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.

William Jockusch
  • 26,513
  • 49
  • 182
  • 323
doerig
  • 1,857
  • 1
  • 18
  • 26
  • -1 That's not true at all... the user does *not* need to do that... they can just use the `x:Static` notation shown by @AjS. – Sheridan Dec 06 '13 at 08:56
  • The OP is binding to a static property, not to a method. How does your code help with that? – Dan Puzey Dec 06 '13 at 09:48
  • +1 Apologies @doerig, I did not notice that the question author actually wanted to bind to the output of a method seeing as their title was named *WPF bind to property of static class*. I had to edit your answer before I could remove my down vote so I just reformatted one line. – Sheridan Dec 06 '13 at 09:48
  • @DanPuzey, please take another look at this... I think that you made the same mistake as me due to this stupidly titled question. – Sheridan Dec 06 '13 at 09:50
  • @Sheridan/doerig: Thanks for pointing out my mistake. Indeed, doerig's answer is correct (and the question needs editing!). My apologies! – Dan Puzey Dec 06 '13 at 09:52
  • ups, I should have pointed out that there was an error in the question :) – doerig Dec 06 '13 at 15:51
0

Try something like this:-

{Binding Source={x:Static classnamespace:Bar}, Path=Fubar}
Sheridan
  • 68,826
  • 24
  • 143
  • 183
AjS
  • 341
  • 2
  • 13
  • "The namespace prefix 'classnamespace' is not defined" – William Jockusch Dec 06 '13 at 05:22
  • 1
    You should declare that in your xaml:-xmlns:mynamespace="clr-namespace:ApplicationName" Here ApplicationName is name of the namespace. {Binding Source={x:Static mynamespace:Bar}, Path=Fubar} – AjS Dec 06 '13 at 05:52