Why do some WPF binding fail against IronRuby properties?
-
05-10-2020 - |
Question
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?
Solution
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
OTHER TIPS
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.