Binding stackLayout's visible property in a list with Toggle switch

I have a toggle button and list which contains stack layout in it. Toggle(Switch) and List View are declared in Xaml file where list's Data-cell is generated programatically in cs file. I need to hide/show this stack layout in the list based on the toggle switch

Xaml code:

`<Switch  IsToggled="{Binding IsToggled, Mode=TwoWay}" Grid.Row = "0" Grid.Column = "1" HorizontalOptions = "Start" Margin = "10,8,8,0"></Switch>

<ListView x:Name="lvItemSigns" HasUnevenRows="True" SeparatorVisibility="Default" SeparatorColor="Gray">`

cs file code:

lvItemSigns.ItemTemplate = new DataTemplate(typeof(DataCell));
lvItemSigns.ItemsSource = VM.ItemSignsList;

    `class DataCell : ViewCell
Label label;
 public DataCell()
    // has grid
    var label = new Label();
        label.TextColor = Color.Black;
        label.Margin = 4;
        label.Text = "test";
        grid.Children.Add(label, 2, 1);
        label.SetBinding(Label.IsVisibleProperty, new Binding("BindingContext.IsToggled", BindingMode.TwoWay, new BooleanConverter(), null, null, "cs-          file_name"));

grid.Children.Add(stackLayout, 0, 3);

        grid.Margin = new Thickness(8,0,0,0);
        View = grid;

In ViewModel:

public bool _IsToggled; public bool IsToggled { get { return _IsToggled; } set { _IsToggled= value; OnPropertyChanged("_IsToggled"); } } public ItemSignsTabViewModel() { ItemSignsList = new ObservableCollection<TicketItem>(daItemSign.GetItemSigns(Ticket.MobileID)); }
I'm binding the visible property to Label but still I can see the label all the time (when toggle switch is on or off).

I know I'm doing something wrong. Any help?


  • ShantimohanElchuriShantimohanElchuri USMember ✭✭✭✭✭

    @saikiransunkari.3595 You can do as follows if you want to enable / disable StackLayout depending on the Switch.IsToggled.

    <Switch x:Name="Switch1" />
    <StackLayout IsEnabled="{Binding Source={x:Reference Switch1}, Path=IsToggled}" />
Sign In or Register to comment.