1

I'm stuck again at some data-binding issue.
This time I want to bind a ListView to the SelectedItem of a GridView. I already suceed with this type of data-binding but now my ListView, which should show some details about my selected item in my GridView just stays empty. There are no items in it although they should exist.
The GridView binds just fine at the property in my MainViewModel.
Substituting the ElementName attribute with x:Resouces doesn't seem to be an option, because it doesn't work either.
The source view:

<GridView x:Name="gridViewOrderYears" 
          ItemsSource="{Binding SelectedCustOrders, Mode=TwoWay}"
          HorizontalAlignment="Left" 
          Grid.Row="1" 
          VerticalAlignment="Top" 
          Width="316" 
          Height="63" 
          Margin="657,316,0,0"
          SelectionMode="Single">
    <GridView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Border BorderThickness="1" BorderBrush="Aquamarine">
                    <StackPanel>
                        <TextBlock Text="{Binding Year}" FontSize="20"/>
                        <TextBlock Text="{Binding OrderCount}"/>
                    </StackPanel>
                </Border>
            </Grid>
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>

this View doesnt bind:

<ListView HorizontalAlignment="Left" 
          Height="231" 
          Margin="657,401,0,0" 
          Grid.Row="1" 
          VerticalAlignment="Top" 
          Width="316"
          DataContext="{Binding SelectedItem, ElementName=gridViewOrderYears}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding DoneOrders.Order_Date, ElementName=gridViewOrderYears}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

SelectedCustOrders porperty is an IList<OrderYears>.
OrderYears is following data value object defined in my MainViewModel:

public class OrderYears
    {
        public int? Year { get; set; }
        public IList<Orders> DoneOrders { get; set; }
        public int OrderCount { get; set; }
    }
JiBéDoublevé
  • 4,124
  • 4
  • 36
  • 57
Zhedar
  • 3,480
  • 1
  • 21
  • 44

2 Answers2

1

I think the problem is in the ListView binding, because you try to bind to a property named "Orders", which does not exist in the OrderYears object. You have a property named DoneOrders which you can bind to (don't confuse the property name with the type of elements inside the list!), but if you bind a TextBlock to a IList you will just get the guid for the IList object.

Try something like this, replacing you ListView with a ListBox (which is enough for what you are trying to do here):

 <ListBox DataContext="{Binding ElementName=gridViewOrderYears, 
                                Path=SelectedItem.DoneOrders}" 
          DisplayMemberPath="Order_Date"/>

There is no need to create a template, the items inside the ListBox will be displayed like a TextBlock. Note that you can benefit from binding to nested properties like MainProperty.SubProperty.

Let me know if this was helpful, bindings can be such a headache when you are starting...

JiBéDoublevé
  • 4,124
  • 4
  • 36
  • 57
Hannish
  • 1,482
  • 1
  • 21
  • 33
  • actually that was a mistake I made when I copied it to SO. It WAS named `Orders` but I changed the name, so that I will not confuse it. I will edit my post to the correct names. The template was a placeholder, because I wanted to show more then just `Order_Date`. Maybe the `ListView` isn't the problem itself, since changing it to your `ListBox` example didn't solve the problem. – Zhedar Dec 28 '12 at 20:49
  • Of course, the problem is not about ListBox/ListView, they are practically the same and what is not working is the binding. Please post the correction and we'll try to help you out. – Hannish Dec 29 '12 at 01:27
  • Well, I've tried many things till now but the code above is still what I stick to (with the corrected name I already edited ofc). I noticed that binding a TextBlock like the one I'm using in my data template alone does bind correctly. Just like `` – Zhedar Dec 29 '12 at 01:35
  • Finally got it. I will post it soon. Easier then I thought... well, I'm really thankful for your help, though :) – Zhedar Dec 29 '12 at 01:43
0

Finally I got it.
After hours of trial-and-error finally substituting DataContext with ItemsSource did the trick...
Sometimes things are easier than you think :)

Zhedar
  • 3,480
  • 1
  • 21
  • 44
  • Man, you just beat me to it! I came here to suggest you just that. I'm glad that you could solve it, it feels good when you get it on your own ;-) Thank you for the upvote! – Hannish Dec 29 '12 at 10:01