MoominTroll
MoominTroll

Reputation: 2552

Bind to a method in code behind

I have a custom control template for a ListView that puts an extra line in for each record, thats defined something like this in Window.Resources...

<ControlTemplate TargetType="ListBoxItem">
    <Border>
        <StackPanel>
            <GridViewRowPresenter>
            <TextBlock Name="myTextBlock" />
        </StackPanel>
     </Border>
     <ControlTemplate.Triggers>
         //Triggers here
     </ControlTemplate.Triggers>
</ControlTemplate>

My problem is that I want to bind the text in the TextBlock to a different ItemsSource than the one that will be bound to the actual ListBox when its instantiated. Binding programatically is impossible. I've tried substituting the TextBlock for another ListView and binding to a method, but I couldn't work out how to use ObjectDataProvider and bind to a method in my code behind (which contains a method that would return a list of things I want to bind too), but ran into problems with this as well.

A quick step by step in case I'm not being clear:

So - is there a way to straight up bind to the results of a method defined in my code behind, which I could reference in the template?

Upvotes: 3

Views: 247

Answers (1)

levanovd
levanovd

Reputation: 4165

Ok, that is the idea:

1) create list box which is bound to Foo with such item template:

-------------
| DATA HERE |
|           |
-------------

2) create list box which is bound to completely different itemsource with such item template:

-------------
|           |
| DATA HERE |
-------------

3) draw the first list box behind the second one.

If there are the same count of items (this is your case as I understand), you will achieve the visual effect you want. Hope it helps.

EDIT
This method does not correspond your present template, but it is a variant of solution.

Upvotes: 1

Related Questions