views:

71

answers:

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?

+1  A: 

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.

Ryan Riley
@Ryan: 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
Yeah, I just noticed that. I am trying to determine whether you may have discovered a name-mangling bug in IronRuby.
Ryan Riley
@Brian: Sorry for answering the wrong question. I'll try to attach a test converter and see what, if anything, _is_ binding.
Ryan Riley
+2  A: 

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
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
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
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
Cool! glad I could help you get to the solution!
Shay Friedman