2

I have a simple converter inheriting from IMultiValueConverter that takes in 2 parameters (in the values array).

public class TicketNoConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var dataContext = values[0] as ViewModel;
        var data = values[1] as String;
    }
}

I know my converter is a singleton but despite ViewModel containing different data my converter always received the same ViewModel instance (first binding parameter) and the same Remarks text.

Using this XAML the Remarks properly get displayed with the correct value.

<TextBlock
    Grid.Column="1"
    Style="{StaticResource EditorValueStyle}"
    Text="{Binding ViewModel.Remarks}" />

However, if I pass the Remarks as a binding parameter then it uses the same parameter values as the first ViewModel passed from the first item loaded. This is what the XAML look like:

   <ContentControl Grid.Column="1">
        <ContentControl.Content>
            <MultiBinding Converter="{StaticResource TicketNo}">
                <Binding Path="" />
                <Binding Path="ViewModel.Remarks" />
            </MultiBinding>
        </ContentControl.Content>
    </ContentControl>

Here is the XAML I use to register my converter:

<local:TicketNoConverter x:Key="TicketNo" />

Digging into this problem some more the 2 parameters passed to the MultiBinding are always the 2 values from the previous ViewModel that was loaded.

How can I ensure that the current instance of ViewModel gets used?

gcso
  • 2,315
  • 3
  • 28
  • 50
  • Seems you need to provide more information about your bindings.. try to post more xaml code which will help us to understand better.. your code is absolutely behaving in expected way, what ever be the data context for the content control it will sent to converter. – Bathineni Aug 25 '11 at 14:08

1 Answers1

2

Once you setup bindings I do not believe they refresh unless your data context changes. Is the data context for the view set to the view model or to itself? If it's not set to the view model - you have to force the bindings to refresh.

Do you have an INotify event being fired when your view model is changed?

tsells
  • 2,751
  • 1
  • 18
  • 20
  • I'm not sure what you mean by your first question. I have implemented `INotify` and I know it works because every other property from my view model is displayed properly, except the one I pass through my value converter. – gcso Aug 25 '11 at 17:21
  • Where is the reference to your view model being stored? In the View? Do you have one view model, a collection? How is it bound to the UI? I normally set the view model as a property on my view. Then when that changes it should fire the INotify which would update the UI. – tsells Aug 26 '11 at 02:01
  • I'm not sure where the reference to my VM is. I use a DI container that drives the entire application once I resolve my main window. My `ViewModel` object is per view, so one. I'm really stumped because removing my value converter and using the value directly the correct data is outputted so that tells me my view is property being notified of changes. And I can see that every other piece of data is reflecting what it should be. – gcso Aug 26 '11 at 17:01
  • I'm digging more into this problem and it appears to always be passing in the parameter values for the previous item that was loaded. This seems to tell me that the parameters are set before my new item is even loaded? – gcso Aug 26 '11 at 17:02