How can I manage sv-mi elements on View from other classes (not from the code behnid)? Specifically, you need to hide or show elements ( button , Image , etc.

Update: My application has 1 page, so you need to click on the A button to show ListView1 , and ListView2 hide ListView2 . And vice versa, click the button Б ListView1 remove ListView1 , and show ListView2 . Similarly with other elements. Everything happens by pressing buttons

Initially only buttons are shown. The application has 3 functions: The first button displays a ListView with a list of filters, after applying the filter, hide one (original) Image and leave only the filtered one, remove the ListView with filters from the second button and show the one in which the effects are stored. By the third button, all elements on the View are hidden and the Image elements become visible and FilePicker immediately launched to add pictures there.

  • Usually you do wrong. You have a VM class, you set properties that say what you want, and View binds to them via Binding and enables / disables the necessary visual elements. - VladD
  • Describe better your real task. Why do you need to remove the button? - VladD
  • Updated, look, please - SmiLe
  • This is better, but let's go even higher. What is shown in the application in the first and in the second case? I do not mean the presence / absence of buttons, but the meaning of the shown UI. - VladD
  • I correctly understood what to do? - SmiLe

3 answers 3

The simplest thing is to use Xaml Behaviors and create triggers. For example, on a click event on a button, hide what you don't need and show what you need. It looks like this:

Add the XAML Behaviors (managed) Nuget Package. Register yousings.

 xmlns:i="using:Microsoft.Xaml.Interactivity" xmlns:core="using:Microsoft.Xaml.Interactions.Core" 

and describe the triggers themselves and the expected behavior

 <core:EventTriggerBehavior EventName="Tapped" SourceObject="{Binding ElementName=Button1}"> <core:ChangePropertyAction TargetObject="{Binding ElementName=ListView1}" PropertyName="Visibility" Value="Visible" /> <core:ChangePropertyAction TargetObject="{Binding ElementName=ListView2}" PropertyName="Visibility" Value="Collapsed" /> </core:EventTriggerBehavior> 

    The easiest way to use scheduling is the VM type that ContentPresenter gives you.

    You get something like this:

     <Page ...> <StackPanel> <!-- это ваша переменная часть --> <ContentPresenter Content="{Binding InnerPart}"/> <!-- а это постоянная часть --> <Button Command="{Binding AdvanceCommand}"/> <StackPanel> </Page> 

    Now, your VM should have dependency property InnerPart of object type (or BaseVM , or whatever you have), and AdvanceCommand .

    In the VM constructor, you write InnerPart = new FiltersVM() , and on the Execute at AdvanceCommand substitute InnerPart : InnerPart = new EffectsVM() .

    Now, to organize the display of FiltersVM and EffectsVM , we need DataTemplate 's.

    Go back to XAML. Define a template for displaying FiltersVM as a ListView with a list of filters:

     <DataTemplate DataType="{x:Type local:FiltersVM}"> <ListView ItemsSource="{Binding FilterList}"> ... </ListView> </DataTemplate> 

    ( FilterList must be a collection in FiltersVM ). Next, a template for displaying EffectsVM as a ListView with a list of effects:

     <DataTemplate DataType="{x:Type local:EffectsVM}"> <ListView ItemsSource="{Binding EffectList}"> ... </ListView> </DataTemplate> 

    ( EffectList must be a collection in EffectsVM ). Thus, you can organize the display of any number of “sub-pages” on the page.

      Just need to do a Binding on the Visibility property.

      I made a property of type bool and Binding on it for an element that needs to be hidden or shown, and a command was hung on the button that will control this

      Here is my ListView

       <ListView Visibility="{Binding IsListOfFiltersVisible, Converter={StaticResource BooleanToVisibilityConverter}}" </ListView> 

      In the Visibility property you need to bind a Boolean property, and on the button you hang a command and in the method change the value of the property to the opposite. In order for the View understand that Visibility has changed, you also need to add a class that inherits from IValueConverter and, depending on true or fasle returns Visible or Collapsed