1

In our C# WPF application (with the Caliburn.Micro framework) we have a View and a ViewModel. In the ViewModel we have a string-property and I want to show this string inside each child of an ItemsControl (these items have their own ViewModel). I know I could just pass the property to each of these items, but that shouldn't be needed.

So, here is the relevant part of the ViewModel:

using System;
...

namespace NatWa.MidOffice.Modules.Financien.Views
{
    public class BankgarantieFinancienOpsplitsenViewModel : ValidationBase<BankgarantieFinancienOpsplitsenViewModel>
    {
        ...

        public BankgarantieFinancienOpsplitsenViewModel(BankgarantieFinancienState state, ...)
        {
            ...

            Dossiernummer = state.Dossiernummer;
            Kopers = state.Kopers.Select(k =>
                {
                    var bfkvm = new BankgarantieFinancienKoperViewModel(k, adresService);
                    bfkvm.ObservePropertyChanged(koper => koper.Bedrag).Subscribe(p => CalculateOpenstaandBedrag());
                    return bfkvm;
                }).ToList();

            ...
        }

        public string Dossiernummer
        {
            get { return _dossiernummer; }
            private set
            {
                if (value == _dossiernummer) return;
                _dossiernummer = value;
                NotifyOfPropertyChange(() => Dossiernummer);
            }
        }

        ...
    }
}

The relevant part of the View:

<Window x:Class="NatWa.MidOffice.Modules.Financien.Views.BankgarantieFinancienOpsplitsenView"
        ...>

    <Grid Style="{StaticResource WindowPaddingStyle}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            ...
        </Grid.RowDefinitions>

        ...

        <ItemsControl x:Name="Kopers" Grid.Row="1">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border BorderBrush="{StaticResource BorderBrush}" BorderThickness="1" Margin="0,3" Padding="5">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>

                            ...

                            <StackPanel Grid.Column="1" Orientation="Vertical">
                                ...

                                <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="0,3" HorizontalAlignment="Right">
                                    ...
                                    <!-- THIS IS WHERE I WANT TO DISPLAY THE DOSSIERNUMMER-PROPERTY OF THE PARENT VIEWMODEL -->
                                    <TextBlock Text="{Binding ??Parent??.Dossiernummer}"/>
                                    ...
                                </StackPanel>

                                ...
                            </StackPanel>
                        </Grid>
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

        ...
    </Grid>

</Window>

I did try to replace the TextBox with the following, based on this SO-answer, but to now result:

<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Dossiernummer}"/>

I've also tried to add the DataContext binding to the Window (even though Caliburn.Micro should do this automatically):

<Window x:Class="NatWa.MidOffice.Modules.Financien.Views.BankgarantieFinancienOpsplitsenView"
        ...
        DataContext="{Binding}">
Community
  • 1
  • 1
Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
  • where are you setting the itemsSource!!!! – Nikita Shrivastava Sep 24 '15 at 06:54
  • The Caliburn.Micro framework automatically sets the ItemsSource when using `x:Name="NameOfListInViewModel"`. Still thanks for the reply, but we've found our mistake (see the answer posted)... Ah well, mistakes happen I guess.. Luckily we didn't waste too much time. – Kevin Cruijssen Sep 24 '15 at 07:12

1 Answers1

1

Ok, problem found.. >.>

Me and a co-worker tried a couple of more things, like:

Adding this to the window:

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DataContext="{d:DesignData BankgarantieFinancienOpsplitsenViewModel}" mc:Ignorable="d"

Changing the TextBlock-binding to:

<TextBlock Text="{Binding ElementName=Kopers, Path=DataContext.Dossiernummer}"/>

and a couple of more things, all to no avail.. In a couple of other Views we've used the exact same things with success, so we had no idea what could be wrong. And then it struck us..

So, what was the problem? The string in the property was null for the ViewModel I was testing this for.... Great start of the day to make such a stupid mistake.. So, we've changed setting the value in the ViewModel to this:

Dossiernummer = state.Dossiernummer ?? state.UbizzDossiernummer;

(The UbizzDossiernummer is the number of the old system we are replacing (which we've imported into our appliction), and the Dossiernummer are the new numbers from Objects made in our application. The object I've been testing this for was an imported one..)

So, I've changed it back to:

<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Dossiernummer}"/>

and it works..

Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
  • 1
    Oh! I was wondering why isn't an approved answer failed to fix your problem !!! :P – Nikita Shrivastava Sep 24 '15 at 07:15
  • We were indeed wondering the same thing.. We tried about 3-4 things, almost knowing for sure it should work (we weren't 100% sure though..), and then we've came to realization at the same time (you should have seen our faces ;).. We've used the incorrect number.. – Kevin Cruijssen Sep 24 '15 at 07:21
  • KUDOS to you both !!!! we all know that feel of spending hours behind a easy fix :P – Nikita Shrivastava Sep 24 '15 at 07:30