1
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;
    }          
user5566364
  • 173
  • 2
  • 12
  • given these information I can only guess. ` the options do not show up in the List at all` so I think there is something wrong with the way u are initiating or changing the list. if it's and observable collection, then it must be initiated only once and if you want to change it you must only add/remove to/from it. – Bizhan Apr 05 '18 at 20:58
  • I understand the reason why it does not show up options in the list. It is because of the line if (eachOpt.isOptionAccepted!= true) return true; – user5566364 Apr 05 '18 at 21:04
  • Initially, all *EachOption*'s have isOptionAccepted is set to false. So, even if I change that, I don't see that I am hitting a breakpoint inside the converter if I click on *Accept option*. – user5566364 Apr 05 '18 at 21:20
  • you said the breakpoint is not hitting. so it can't be because of that line. correct? – Bizhan Apr 05 '18 at 21:58
  • sorry I think i misunderstood the question. I think what you are looking for is `TemplateSelector` – Bizhan Apr 05 '18 at 22:01
  • I think it would be easier to implement if I can figure out a way to invoke my converter when the list element(in this case - an object) is modified. Or another option would be to explicitly raise property changed on the list element(in this case - an object) – user5566364 Apr 05 '18 at 22:34
  • You can manually implement that however you should ask another question with the related code you have tried, also I suggest trying template selector first as it does exactly what you need. – Bizhan Apr 06 '18 at 05:34

0 Answers0