TableCreek
TableCreek

Reputation: 799

bind value from another object in ListView

In a ListView I want to x:Bind a value from another object than the ListView ItemsSource is of.

var objectList1 = new List<Object1>();
var object2 = new Object2();

<ListView ItemsSource="{x:Bind objectList1}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding PropertyOfObject1}"/>
            <TextBlock Text="{x:Bind object2.PropertyOfObject2}"/>              
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

How can I archieve this?

Upvotes: 0

Views: 152

Answers (3)

Stamos
Stamos

Reputation: 3998

You will have to reference the Object2 in your Object1. Example:

public class Object1
{
    public Object1(Object2 object2)
    {
        this.object2=object2;
    }

    public Object2 object2 {get;set;}

    public string PropertyOfObject1 {get;set;}
}


public class Object2
{
    public string PropertyOfObject2 {get;set;}
}


List<Object1> objectList1 = new List<Object1>();

Object2 o2 = new Object2();
Object1 item1 = new Object1(o2);
objectList1.Add(item1);

Upvotes: 0

Kiran Paul
Kiran Paul

Reputation: 885

I believe that this can be achieved by relative source binding, although I could not figure out.

But I can give you an alternative solution.

1) Use a hidden element in the XAML and bind it to desired view model property.(this could be easily possible as it is out side of the data template).

2) Then bind (second item in data template) to that hidden element as an element binding.

I already used this concept. If you want i can share pseudo code for that. Happy coding.

Upvotes: 1

suulisin
suulisin

Reputation: 1434

please create a viewmodel to encapsulate the two list.

public class viewmodel{
public List<Object1>{get;set;}
public List<Object1>{get;set;}

} then you can bind like this

<ListView ItemsSource="{x:Bind viewmodel}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding PropertyOfObject1}"/>
            <TextBlock Text="{x:Bind object2.PropertyOfObject2}"/>              
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Upvotes: 0

Related Questions