2

So, lets say I have two nearly identical classes in C# and Ruby:

C#

public class Test
{
    public Test()
    {
        ImageLocation = "http://www.ironruby.net/@api/deki/site/logo.png";
    }

    public string ImageLocation { get; set; }
}

Ruby

class Test
    attr_accessor :ImageLocation

    def initialize
      @ImageLocation = "http://www.ironruby.net/@api/deki/site/logo.png"
    end
end

When I bind to the "ImageLocation" property in C#, all three controls bind properly. When I bind to the same property with the IronRuby object, it works for TextBlock, but fails for TextBox and Image. Here is my XAML:

<Image Source="{Binding ImageLocation}" Width="50" />
<TextBlock Text="{Binding ImageLocation}" />
<TextBox Text="{Binding ImageLocation}" />

Why does the binding work properly for one control, but not the others?

Brian Genisio
  • 47,787
  • 16
  • 124
  • 167

2 Answers2

1

Ivan Porto Carrero's book IronRuby in Action provides the solution to your problem. See databinding.rb from the book's source code.

This definitely doesn't solve the problem you're having, and I've repro'd it myself.

Update: Shay's answer worked for me, too.

  • I'm not clear to what the 'solution' is. I see that he is solving the `PropertyNotify` problem... but I don't see how he solves the binding problem I am having. I fully expect that all three controls in my XAML should be populated on start-up... without a change notification. Unfortunately, this is not happening. – Brian Genisio Jul 31 '10 at 02:10
  • Yeah, I just noticed that. I am trying to determine whether you may have discovered a name-mangling bug in IronRuby. –  Jul 31 '10 at 02:12
  • @Brian: Sorry for answering the wrong question. I'll try to attach a test converter and see what, if anything, _is_ binding. –  Jul 31 '10 at 02:53
1

IronRuby types have a few problems with WPF binding... let's say that it's not perfect yet :)

To solve your problem, I recommend you to use CLR classes and types. For example, to make your sample code work just convert this line:

@ImageLocation = "http://www.ironruby.net/@api/deki/site/logo.png"

To this:

@ImageLocation = "http://www.ironruby.net/@api/deki/site/logo.png".to_clr_string
Shay Friedman
  • 4,808
  • 5
  • 35
  • 51
  • Thanks, Shay. This is unfortunate :( Calling that everywhere throughout the Ruby space will be cumbersome. Is this a problem with the IronRuby interop? Or is it a problem with the WPF databinding system? I suppose there are two ways to make this more automated... create a decorator for all classes that are passed up to the View, or create a converter that the view uses that does this for you. – Brian Genisio Jul 31 '10 at 11:59
  • Its interesting, actually. Really quickly, I added a ValueConverter that calls `(value as dynamic).to_clr_string();`. It said that it failed to find the method :( I want to avoid calling `to_clr_string` all over my beautiful Ruby code :) – Brian Genisio Jul 31 '10 at 21:46
  • So, I was just being dumb. I created a value converter called ToStringConverter that simply returns `value != null ? value.ToString() : null;` and I plug it into my XAML. All is good. Thanks, Shay! – Brian Genisio Aug 06 '10 at 14:49
  • Cool! glad I could help you get to the solution! – Shay Friedman Aug 06 '10 at 17:04