[Ironruby-core] Databinding with WPF and Silverlight
Ivan Porto Carrero
ivan at flanders.co.nz
Sun Apr 27 03:54:25 EDT 2008
Hi
Will plain ruby objects be able to participate in the WPF databinding
scenario or I may be doing it wrong ? I'm asking because I tried the
following and the C# class works but the ruby class doesn't. If I replace
Person.new with CSPerson.new it works.
I can bind to arrays with simple types and I've tried binding to Name or
name.
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Testing 123" Height="500" Width="826" >
<Grid>
<TextBlock Text="Testing 123" />
<ListBox x:Name="list">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name, FallbackValue=Name should be
here}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
public class CSPerson
{
public CSPerson(int id, string name, int age){
Id=id;
Name=name;
Age=age;
}
public int Id{get; set;}
public string Name{get; set;}
public int Age{get; set;}
}
# get the necessary assemblies loaded for .NET
require 'mscorlib'
require 'System, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089'
require 'System.Xml, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089'
require 'PresentationFramework, Version=3.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35'
require 'PresentationCore, Version=3.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35'
require File.expand_path('./csperson.dll' )
# include some namespaces for easy access
include System
include System::Windows
include System::Windows::Markup
include System::Xml
class Person
attr_accessor :name, :age, :id
def initialize(id, name, age)
@id, @name, @age = id, name, age
end
end
#run the application
xaml_path = "default.xaml"
obj = XamlReader.load XmlReader.create(xaml_path)
people = []
people << Person.new(1, "ivan", 30)
people << Person.new(2, "jeff", 25)
people << Person.new(3, "mark", 38)
people << Person.new(4, "vicky", 33)
obj.find_name('list').items_source = people
Application.new.run obj
Thanks
Ivan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20080427/8654999e/attachment-0001.html>
More information about the Ironruby-core
mailing list