# ListViewItem DataTemplate

**URL:** <https://www.ruby-forum.com/t/listviewitem-datatemplate/179469>\
**Category:** IronRuby\
**Created:** [November 25, 2009, 8:08pm UTC](https://www.ruby-forum.com/t/listviewitem-datatemplate/179469 "2009-11-25T20:08:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patrick\_Brown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9a140/32.png) [@Patrick\_Brown](https://www.ruby-forum.com/u/Patrick_Brown)\
**Post date:** [November 25, 2009, 8:08pm UTC](https://www.ruby-forum.com/t/listviewitem-datatemplate/179469/1 "2009-11-25T20:08:27Z")

</div>

Hello

Has anyone gotten a datatemplate for a ListView under WPF? I created  
a  
very simple C# sample and converted it to IronRuby, the C# works  
perfectly,  
the IronRuby example displays my class name, the datatemplate didn’t  
load  
and bind up to the properties as I hoped.

C#  
public class StatusListView : ListView  
{  
public StatusListView()  
{  
var items = new[]  
{  
new { message = “One”, messageType = “Status” },  
new { message = “Two”, messageType = “Error” },  
new { message = “Shree”, messageType = “Status” }  
};  
FrameworkElementFactory textblock = new  
FrameworkElementFactory(typeof(TextBlock));

```
        Setter setter = new Setter();
        setter.Property = TextBlock.FontSizeProperty;
        setter.Value = 18.0;

        DataTrigger dataTrigger = new DataTrigger();
        dataTrigger.Binding = new Binding("messageType");
        dataTrigger.Value = "Status";
        dataTrigger.Setters.Add(setter);

        Style style = new Style(typeof(TextBlock));
        style.Triggers.Add(dataTrigger);

        textblock.SetValue(TextBlock.StyleProperty, style);
        textblock.SetBinding(TextBlock.TextProperty, new

```

Binding(“message”));  
DataTemplate template = new DataTemplate();  
template.VisualTree = textblock;  
ItemsSource = items;  
ItemTemplate = template;  
}  
}

IronRuby - in this sample my listview itemsource is actually set in  
another piece of code as so. As I state above, the item does show up in  
the  
listview but the datatemplate is not applied against it.

self.buildMessages = Array.new  
self.buildMessages.push(BuildMessage.new(“Test”,“Status”))  
self.statusListView.ItemsSource = self.buildMessages

class StatusListView \< ListView  
def initialize()  
HorizontalAlignment = HorizontalAlignment.Stretch  
Margin = Thickness.new(0,5,0,5)  
MinHeight = 200

BuildItemTemplate()  
end

def BuildItemTemplate()  
begin  
textblock = FrameworkElementFactory.new(TextBlock.to\_clr\_type)

setter = Setter.new  
setter.Property = TextBlock.FontSizeProperty  
setter.Value = 14.0

dataTrigger = DataTrigger.new  
dataTrigger.Binding =  
System::Windows::Data::Binding.new(“messageType”)  
dataTrigger.Value = “Status”  
dataTrigger.Setters.Add(setter)

style = Style.new(TextBlock.to\_clr\_type)  
style.Triggers.Add(dataTrigger)

textblock.SetValue(TextBlock.StyleProperty, style)  
textblock.SetBinding(TextBlock.TextProperty,  
System::Windows::Data::Binding.new(“message”))

template = DataTemplate.new  
template.VisualTree = textblock  
ItemTemplate = template  
rescue Exception =\> e  
puts “#{e}”  
end  
end  
end

Thanks for your time and thoughts  
Patrick

---

<div class="post-metadata">

**Author:** ![Patrick\_Brown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9a140/32.png) [@Patrick\_Brown](https://www.ruby-forum.com/u/Patrick_Brown)\
**Post date:** [November 25, 2009, 9:25pm UTC](https://www.ruby-forum.com/t/listviewitem-datatemplate/179469/2 "2009-11-25T21:25:08Z")

</div>

Hi

Ok, this confuses me a bit, I can get it to work by not deriving from  
ListView and setting all my settings in the initialize method and  
instead  
having a listview member and assigning all the values on it. Can anyone  
see  
a reason for this?

def BuildListView()  
self.statusListView = ListView.new  
self.statusListView.HorizontalAlignment = HorizontalAlignment.Stretch  
self.statusListView.Margin = Thickness.new(0,5,0,5)  
self.statusListView.Height = 200

textblock = FrameworkElementFactory.new(TextBlock.to\_clr\_type)

setter = Setter.new  
setter.Property = TextBlock.FontSizeProperty  
setter.Value = 14.0

dataTrigger = DataTrigger.new  
dataTrigger.Binding =  
System::Windows::Data::Binding.new(“messageType”)  
dataTrigger.Value = “Status”  
dataTrigger.Setters.Add(setter)

style = Style.new(TextBlock.to\_clr\_type)  
style.Triggers.Add(dataTrigger)

textblock.SetValue(TextBlock.StyleProperty, style)  
textblock.SetBinding(TextBlock.TextProperty,  
System::Windows::Data::Binding.new(“message”))

template = DataTemplate.new  
template.VisualTree = textblock

self.statusListView.ItemTemplate = template  
end

---

<div class="post-metadata">

**Author:** ![Patrick\_Brown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9a140/32.png) [@Patrick\_Brown](https://www.ruby-forum.com/u/Patrick_Brown)\
**Post date:** [November 26, 2009, 9:52am UTC](https://www.ruby-forum.com/t/listviewitem-datatemplate/179469/3 "2009-11-26T09:52:28Z")

</div>

There is no link between the DLR objects and CLR objects so if you  
derive  
from a control in ruby code there is no way for XAML to figure out what  
has  
been configured etc. There is also no class it can link to as the  
classes  
don’t exist in the same way in a DLR based language as in a CLR  
language.

The following control tries to make that integration easier but it’s  
still  
very early days and all help is wanted.  
[http://dynamicscriptcontrol.codeplex.com/](http://dynamicscriptcontrol.codeplex.com/)  
[http://code.google.com/p/dynamic-script-control/](http://code.google.com/p/dynamic-script-control/)

Nick Rickets may be of help, I think he’s got a WPF application with  
IronRuby that is deployed AFAIK

I think it’s important to understand that there is a difference between  
a  
DLR and a CLR based language. CLR classes map straight to CLR-types but  
this  
isn’t always the case in a DLR based language because they have a  
difference  
on opinion with the CLR on how classes should behave.  
The CLR says ALL classes are CLOSED that means you can’t modify anything  
once it’s compiled then it only does its job and doesn’t learn new  
tricks  
The DLR (for IronRuby and IronPython at least) says ALL classes are  
OPEN,  
you can freeze some making them closed. Meaning once you’re program is  
running you can modify classes by adding or removing methods and so on.  
Because of this reason x:Class doesn’t know where to go because for the  
CLR  
the type may very well not exist.

This doesn’t help you much but it’s a start to figure out what’s going  
on  
and why things don’t work the same as with C#

I’m sorry but I keep deleting the following message from every email I  
sent  
to help you:

Naming conventions!  
in ruby stuff is lowercased\_and\_underscored except for constants or  
there is  
a particularly good reason for you to want an uppercased name like a DSL  
entry point. UpperCased names are constants in Ruby and as such people  
wanting to help you (in this case me) have an easier time getting  
through  
your code as they don’t have to workout what they are looking at.  
Constants  
include ModuleNames, ClassNames and CONSTANT\_VALUES

in C# stuff is CamelCasedAndNotUnderscored.

## .

Met vriendelijke groeten - Best regards - Salutations  
Ivan Porto C.  
Blog: [http://flanders.co.nz](http://flanders.co.nz)  
Google Wave: removed\_email\_address@domain.invalid  
Twitter: [http://twitter.com/casualjim](http://twitter.com/casualjim)  
Author of IronRuby in Action ([http://manning.com/carrero](http://manning.com/carrero))

---

<div class="post-metadata">

**Author:** ![Patrick\_Brown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9a140/32.png) [@Patrick\_Brown](https://www.ruby-forum.com/u/Patrick_Brown)\
**Post date:** [November 27, 2009, 6:24pm UTC](https://www.ruby-forum.com/t/listviewitem-datatemplate/179469/4 "2009-11-27T18:24:54Z")

</div>

Yeah sorry for that… I guess I’m not a morning person

## keep sending questions, helps me to understand the areas people have trouble with 🙂

Met vriendelijke groeten - Best regards - Salutations  
Ivan Porto C.  
Blog: [http://flanders.co.nz](http://flanders.co.nz)  
Google Wave: removed\_email\_address@domain.invalid  
Twitter: [http://twitter.com/casualjim](http://twitter.com/casualjim)  
Author of IronRuby in Action ([http://manning.com/carrero](http://manning.com/carrero))

---

<div class="post-metadata">

**Author:** ![Patrick\_Brown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9a140/32.png) [@Patrick\_Brown](https://www.ruby-forum.com/u/Patrick_Brown)\
**Post date:** [November 27, 2009, 6:11pm UTC](https://www.ruby-forum.com/t/listviewitem-datatemplate/179469/5 "2009-11-27T18:11:27Z")

</div>

Hi

Thank you, this explanation is very helpful. I will start working on  
following the naming conventions, I have actually been purposefully  
avoiding  
them to this point as following the C# standards was helping me to map  
commonalities between the two languages and focus on learning the  
language  
and tools themselves.

Thanks, I appreciate all of your help  
Patrick
