Converter not called when Object property inside L

2019-08-05 12:44发布

问题:

TemplateSwitcher
                 ShowAlternativeTemplate="{Binding Options, Converter={StaticResource ListToVisibilityConverter}}">
                TemplateSwitcher.AlternativeTemplate starts
                            shows a watermark  // Line A
                TemplateSwitcher.AlternativeTemplate ends
                // actual template which shows up if alternative template is not the case
                ScrollViewer
                    ListView
                        // SHows a list of options and with each option , there are two buttons - Accept option/ Reject option
                        /* Clicking on Accept sets the isOptionAccepted to true; */
                    ListView ends
                ScrollViewer ends
Tempalte switcherends
  • Options = ObservableCollection of EachOption type; EachOption has a bool? field called isOptionAccepted

What I am trying to achieve is:

  • When the page loads: If the Options field is null/Empty : Shows alternative template(watermark) as in Line A in XAML // Happens correctly
  • When the user clicks on Accept option: The list in the UI should NOT show this option. However, other options in the ObservableCollection still appear.// I have acheived this by binding the visibility of each listviewitem to the isOptionAccepted field

Now, when all the options are accepted, it would mean that no options should show up in the UI,which is happening correctly. However, as a result I want it to show the alternative template(watermark) in Line A of XAML. I am unable to implement this.

Took a look at the this example but I am still not able to solve this. With this converter, the options do not show up in the List at all. I have set a breakpoint inside the converter and I was expecting to hit the breakpoint whenever I click on Accept (because that is modifying a property of the object EachOption) Any help is appreciated.

Below is the ListToVisibilityConverter

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return true;
        else
        {
            ObservableCollection<EachOption> optionList= value as ObservableCollection;
            if (optionList != null)
            {
                foreach (EachOption eachOpt in optionList)
                {
                    if (eachOpt.isOptionAccepted!= true)
                        return true;
                    else
                        return false;
                }
            }
        }
        return true;
    }