1
votes

I have set the binding source of "IsVisible" property to a public variable in the code behind without following MVVM. In this case, even though the state of the variable is changed, the view (in this case ListView) is still visible.

private void btnNotifications_Clicked(object sender, EventArgs e)
{
    if (!ActivateControlButton(sender as Button))
    {
        Remove_ChildElements_InUserCOntrol();
        return;
    }

    Is_MainTaskList_Visible = false;
}

My xml code is:

<ListView x:Name="lstUserTasks"
          Grid.Row="2" 
          ItemsSource="{Binding User_Task_Groups}"
          IsGroupingEnabled="True"
          FlowDirection="LeftToRight"
          HasUnevenRows="True"
          ItemSelected="lstUserTasks_ItemSelected"
          ItemTapped="lstUserTasks_ItemTapped"
          IsVisible="{Binding Is_MainTaskList_Visible}">

Can someone help?

How to notify the UI thread that this propery is changed?

1
is Is_MainTaskList_Visible a public property? Does the class implement INotifyPropertyChanged?Jason

1 Answers

0
votes

You could use the code below:

 public class UserViewModel
{

    public UserViewModel()
    {
        CreateCollection();
        Is_MainTaskList_Visible = true;
    }
    public ObservableCollection<User> User_Task_Groups { get; set; }
    public bool Is_MainTaskList_Visible { get; set; }
    public void CreateCollection()
    {
        User_Task_Groups = new ObservableCollection<User>()
        {
            new User(){ Name="A"},
            new User(){ Name="B"},
            new User(){ Name="C"},
        };

    }
}
public class User
{
    public string Name { get; set; }
}

Xaml:

<ContentPage.BindingContext>
    <local:UserViewModel></local:UserViewModel>
</ContentPage.BindingContext>
<ContentPage.Content>
    <StackLayout>
        <Button Clicked="Button_Clicked"></Button>
        <ListView x:Name="lstUserTasks" Grid.Row="2" 
          ItemsSource="{Binding User_Task_Groups}"   FlowDirection="LeftToRight" HasUnevenRows="True"
          ItemSelected="lstUserTasks_ItemSelected"
          ItemTapped="lstUserTasks_ItemTapped" IsVisible="{Binding Is_MainTaskList_Visible}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Label Text="{Binding Name}"></Label>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage.Content>