Home > OS >  xamarin form - how to access string in BindingContext
xamarin form - how to access string in BindingContext

Time:10-13

I have the following form

        <ListView x:Name="DocumentList"
                  HasUnevenRows="true"
                 >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>                        
                     <ViewCell.ContextActions>
                            <MenuItem Text="Check In"
                                      Clicked="CheckInFile"/>
                            <MenuItem Text="Check Out"
                                      Clicked="CheckOutFile"/>
                            <MenuItem Text="Status"
                                      Clicked="FileStatus"/>
                        </ViewCell.ContextActions>
                        
                        <Grid>
                            <Label Text="{Binding Path=WebUrl}"
                                   FontSize="Small" />
                        </Grid>
                    </ViewCell>

                </DataTemplate>
            </ListView.ItemTemplate>

And here's the click event handler for one of the items in the context menu:

    protected void CheckOutFile(object sender, EventArgs e)
    {
        var menuItem = sender as MenuItem;
        if (menuItem != null)
        {
            var webUrl = menuItem.BindingContext;
            DisplayAlert("Alert", "Check Out File: "   webUrl   "requested", "ok");
        }
    }

Problem / Questions

Question 1

When the form loads - the "WebUrl" value appears correctly. But when I click on "CheckOut", i don't know how to extract the value / url from the menuItem.BindingContext. The alert shows null.

Question 2

I only want the user to see the WebUrl, but I want to pass 3 different values let's say when the click event is triggered. How would I do this?

Sorry, I'm just a noob with xaml and xamarin.
Thanks.

CodePudding user response:

If your ListView ItemsSource is a List<SomeType> then

var item = (SomeType)menuItem.BindingContext;
DisplayAlert("Alert", "Check Out File: "   item.webUrl   "requested", "ok");

assuming that webUrl is a property of SomeType

  • Related