Question

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?

Was it helpful?

Solution

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top