3

We have a UWP app using Template10. There is a textblock and textbox which show a discount. We wish to hide the textblock when ViewModel.Discount is null.

In App.xaml we have defined a converter

<T10Converters:ValueWhenConverter x:Key="HideWhenNullConverter" When="{x:Null}">
    <T10Converters:ValueWhenConverter.Value>
        <Visibility>Collapsed</Visibility>
    </T10Converters:ValueWhenConverter.Value>
    <T10Converters:ValueWhenConverter.Otherwise>
        <Visibility>Visible</Visibility>
    </T10Converters:ValueWhenConverter.Otherwise>
</T10Converters:ValueWhenConverter>

In the View we set the visibility of the TextBlock

Visibility="{x:Bind ViewModel.Discount, Converter={StaticResource HideWhenNullConverter}}"

In the ViewModel:

public class ViewModel : ViewModelBase
{
    decimal? _Discount = default(decimal?);
    public decimal? Discount
    {
        get
        {
            return _Discount;
        }
        set
        {
            if (value == 0) value = null;
            Set(ref _Discount, value);
        }
    }

However the textblock is always visible even if the value of ViewModel.Discount is null. How do we hide the textblock when ViewModel.Discount is null

Vague
  • 2,198
  • 3
  • 18
  • 46
  • 1
    You could also state that the Visibility logic is the responsibility of the ViewModel. The latest UWP can bind Visibility to a bool directly. Imagine you later have additional business rules, like a max price or so. – H H Mar 04 '17 at 10:34
  • @HenkHolterman Nice, thanks for mentioning that - I've somehow [missed this](https://social.technet.microsoft.com/wiki/contents/articles/34846.uwp-compiled-binding-windows-10-anniversary-update.aspx). – Romasz Mar 04 '17 at 10:45
  • In _14393_, `Visibility="{x:Bind ViewModel.Discount.HasValue, Mode=OneWay}"`. – Justin XL Mar 04 '17 at 22:23
  • Thanks for the suggestion @Justin XL but I couldn't get this to work in 14393 – Vague Mar 04 '17 at 23:15
  • @Vague ahh I think I was wrong as HasValue might only work for the first time. So might still need to create a new bool property? – Justin XL Mar 04 '17 at 23:28
  • @Justin XL the answer accepted is probably the best as it does not require creation and setting of another property. – Vague Mar 04 '17 at 23:32
  • 1
    Actually if you are using 14393 you should avoid converters as they are slow. Instead you can bind a function directly. – Justin XL Mar 04 '17 at 23:40
  • 1
    Yes @Justin XL that's what we have found ... converters are terribly slow. Thanks for the tip! – Vague Mar 04 '17 at 23:55
  • "creation and setting of another property" is not a problem when it's in the right place (and the VM usually is the right place). – H H Mar 05 '17 at 22:44
  • @JustinXL - `Discount.HasValue` will not have INPC implemented. – H H Mar 06 '17 at 09:58
  • @HenkHolterman your right. I realized that after I posted the comment. :P – Justin XL Mar 06 '17 at 10:01

1 Answers1

1

As I've tried with Template10's source it should work. I suspect that you are just missing redefinition of Mode with x:Bind, which as default is OneTime. Try like this:

Visibility="{x:Bind ViewModel.Discount, Mode=OneWay, Converter={StaticResource HideWhenNullConverter}}"
Romasz
  • 29,662
  • 13
  • 79
  • 154