Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

ListView with context menu item update trouble

$
0
0

Hello!
My first post!
I´ve been working on a cross-plattform Xamarin Forms application for a couple of months.
I´ve got a problem with the listview. Not sure if I´m doing anything wrong or if it is a bug in Xamarin. But I´ll try and describe the setup.

First I´ve got my own ListView as follows.

public class XListView : ListView {

            public static BindableProperty ItemTappedCommandProperty = BindableProperty.Create("ItemTappedCommand", typeof(ICommand), typeof(XListView), null);
            public static BindableProperty ItemSelectableProperty  = BindableProperty.Create("ItemSelectable", typeof(bool), typeof(XListView), true);

            public ICommand ItemTappedCommand {
                get { return (ICommand)this.GetValue(ItemTappedCommandProperty); }
                set { this.SetValue(ItemTappedCommandProperty, value); }
            }

            public bool ItemSelectable {
                get { return (bool)this.GetValue(ItemSelectableProperty); }
                set { this.SetValue(ItemSelectableProperty, value); }
            }

            public XListView() {
                this.ItemTapped += this.OnItemTapped;
                this.ItemSelected += this.OnItemSelected;
            }



            private void OnItemTapped(object sender, ItemTappedEventArgs e) {
                if (e.Item != null && this.ItemTappedCommand != null && this.ItemTappedCommand.CanExecute(e)) {
                    this.ItemTappedCommand.Execute(e.Item);
                }
            }

            private void OnItemSelected(object sender, SelectedItemChangedEventArgs e) {
                if (e.SelectedItem != null && !this.ItemSelectable) {
                    this.SelectedItem = null;
                }
            }

Then in XAML I´ve got the following (it´s a snippet of the entire xaml but I don´t think that matters in this case):

<views:XListView IsVisible="{Binding ShowTimeConsumers}" ItemsSource="{Binding TimeConsumerViewModel.TimeConsumerItems}" ItemSelectable="false" RowHeight="32" SeparatorVisibility="None" HasUnevenRows="true">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell >
                                <ViewCell.ContextActions>
                                    <MenuItem Grid.Column="1" Command="{Binding TimeConsumerStateChangeCommand}" Text="{Binding StateButtonText}" IsDestructive="false" CommandParameter="{Binding .}" />
                                    <MenuItem Grid.Column="2" Clicked="OnDeleteTimeConsumerClicked" Text="{localization:Translate Delete}" IsDestructive="true" CommandParameter="{Binding .}" />
                                </ViewCell.ContextActions>
                                <Grid>
                                    <Label Text="{Binding Name}" Style="{Binding LabelStyle}"  HorizontalOptions="StartAndExpand" VerticalOptions="Center" />
                                </Grid>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </views:XListView>

Now the problem:
When the TimeConsumerStateChangeCommand runs state is changed and time is calculated and then the following code will run to update the listitems representation in the list.

PropertyChanged.Dispatch(this, "StateButtonText");
PropertyChanged.Dispatch(this, "LabelStyle");

StateChanged.Dispatch(this, EventArgs.Empty);

StateButtonText to change the text on the context menu button and LabelStyle to change the style of the text.
The problem is that from time to time the text will dissapear. The item is still in the list but I cannot see the text. The item still has it´s context menu and it is working. Changing back the state (ie running the code for the same list item) again most often wont help, but sometimes changing state on another listitem will suddenly bring back the dissapeared one...although it could happen that another list item suddenly dissapears in this operation.

Now...if I remove the PopertyChanged.Dispatch(this, "StateButtonText"); and do not update the context menu button text it will all function very well.

    //PropertyChanged.Dispatch(this, "StateButtonText");
    PropertyChanged.Dispatch(this, "LabelStyle");

    StateChanged.Dispatch(this, EventArgs.Empty);

Is this a know issue or is there something I can do to fix it? (Have tried to change the order of the events with no success).
It is running on Ipad/iOs


Viewing all articles
Browse latest Browse all 91519

Trending Articles