Search code examples
c#listviewuwpscrollviewerconverters

How to use DoubleToVisibilityConverter in uwp


DoubleToVisibilityConverter can be used to easily change a double value to a Visibility based one based on a given threshold value. If both GreaterThan and LessThan are set, the converter will set the visibility if the target value is in-between those two values. Otherwise, it will look for the target being greater than or less than the specified value.

so my question is ----

How to use DoubleToVisibilityConverter for scrollviewer as i want to hide (back to Top)Button and scrollbar is at last down position and when scrollbar is going to up it will show the button.

i am using back to top button for scroll to top.

[ same as working in many website (scrollUp Button) ]

Below one is MainPage.xaml

XAML - Page Resource

<Page.Resources>
   <converters:DoubleToVisibilityConverter x:Key="GreaterThanToleranceVisibilityConverter" GreaterThan="65.0"/>
</Page.Resources>

XAML - Ui Element

<Button x:name="Scroll_To_Up_Button" Visibility="{Binding ScrollableHeight, Converter{StaticResourceGreaterThanToleranceVisibilityConverter}/>

where i have to add

ScrollableHeight

in MainPage.xaml.cs and how to use in MyUWP App.


Solution

  • Did you try adding a parameter eg.

    <Button x:name="Scroll_To_Up_Button" Visibility="{Binding ScrollableHeight, Converter={StaticResource GreaterThanToleranceVisibilityConverter}, ConverterParameter=42/>
    

    Edit:

    Give your page a name:

    <Page Name="thisPage"
          ... >
    

    Then point your binding source to it to access the dependency property ScrollableHeight if that is the type you declared it:

    <Button x:name="Scroll_To_Up_Button" Visibility="{Binding ScrollableHeight, ElementName=thisPage, Converter={StaticResource GreaterThanToleranceVisibilityConverter}, ConverterParameter=42/>
    

    And the dependency property in MainPage.xaml.cs:

    public static readonly DependencyProperty ScrollableHeightProperty = DependencyProperty.Register(nameof(ScrollableHeight), typeof(int), typeof(*NAMEOFYOURPAGE*), new PropertyMetadata(null));
    
    public int ScrollableHeight
    {
        get => (int)GetValue(ScrollableHeightProperty);
        set => SetValue(ScrollableHeightProperty, value);
    }