0

Im trying to take the Id from a button that I binded to the button tag but when I try I get it in a method a null value error is thrown even though I can set the context and text and the Id will appear on screen fine.

So now im trying to mark the items in the list with a index so when the data template generates them I can easily call them and compare them to the ids in my c# code.

XAML for list:

<my:PullToRefreshListView
    x:Name="RefreshListView"
    MinWidth="200"
    Margin="24"
    HorizontalAlignment="Center"
    VerticalAlignment="Bottom"
    Background="White"
    OverscrollLimit="0.4"
    PullThreshold="100"
    IsPullToRefreshWithMouseEnabled="True">
        <my:PullToRefreshListView.ItemTemplate >
            <DataTemplate >
                <StackPanel Name="ListPanel">
                        <TextBlock AutomationProperties.Name="IdTextBlock"
             Style="{StaticResource CaptionTextBlockStyle}"
             Text="{Binding Id}"
             TextWrapping="WrapWholeWords"  />
                        <TextBlock AutomationProperties.Name="{Binding Name}"
             Style="{StaticResource CaptionTextBlockStyle}"
             Text="{Binding Name}"
             TextWrapping="WrapWholeWords"  />
                    <TextBlock AutomationProperties.Name="{Binding Sets}"
             Style="{StaticResource CaptionTextBlockStyle}"
             Text="{Binding Sets}"
             TextWrapping="WrapWholeWords"  />
                    <TextBlock AutomationProperties.Name="{Binding SetTime}"
             Style="{StaticResource CaptionTextBlockStyle}"
             Text="{Binding  SetTime}"
             TextWrapping="WrapWholeWords"  />
                        <StackPanel Orientation="Horizontal">
                            <Button Tag="{Binding Id}" Content="Delete" Click="DelButton_Click" Style="{StaticResource DrillButtons}" ></Button>
                            <Button Tag="{Binding Id}" Content="Update" Click="UpdateBtn_Click" Style="{StaticResource DrillButtons}" ></Button>
                        </StackPanel>
                    </StackPanel>
            </DataTemplate>
        </my:PullToRefreshListView.ItemTemplate>
        <my:PullToRefreshListView.PullToRefreshContent>
            <TextBlock FontSize="16"
           Opacity="0.5"
           Text="Pull down to refresh data" />
        </my:PullToRefreshListView.PullToRefreshContent>
    </my:PullToRefreshListView

These two buttons from the stack panel above is where im trying to bind the Id to retrieve later.

<StackPanel Orientation="Horizontal">
                            <Button Tag="{Binding Id}" Content="Delete" Click="DelButton_Click" Style="{StaticResource DrillButtons}" ></Button>
                            <Button Tag="{Binding Id}" Content="Update" Click="UpdateBtn_Click" Style="{StaticResource DrillButtons}" ></Button>
                        </StackPanel>

Heres the methods that take in the Id from the buttons:

//Update button
    private async void NewSubmitBtn_Click(object sender, RoutedEventArgs e)
    {
        String Name = NewNameBox.Text;
        String id = (String)((Button)sender).Content;
        int Sets;
        int Time;
        bool successfullyParsedTime = int.TryParse(NewSetsBox.Text, out Time);
        bool successfullyParsedSets = int.TryParse(NewTimeBox.Text, out Sets);

        if (successfullyParsedSets)
        {
            Sets = Int32.Parse(NewSetsBox.Text);

        }
        if (successfullyParsedTime)
        {
            Time = Int32.Parse(NewTimeBox.Text);
        }

        await ctv.combatDrillsTable.UpdateDrill(id, Name, Sets, Time, catagory);
        ppup.IsOpen = false;
        var addSuccess = new MessageDialog("Drill Updated");
        await addSuccess.ShowAsync();

    }

Heres the data item code:

namespace UWPCombatApp
{
class DrillItem
{
    public string Id { get; set; }

    [JsonProperty(PropertyName = "name")]
    public string Name { get; set; }

    [JsonProperty(PropertyName = "sets")]
    public int Sets { get; set; }

    [JsonProperty(PropertyName = "settime")]
    public int SetTime { get; set; }

    [JsonProperty(PropertyName = "style")]
    public string Style { get; set; }

}
}

The delete popup I added:

// Delete button
    private void DelButton_Click(object sender, RoutedEventArgs e)
    {
        delpup.Height = Window.Current.Bounds.Height;
        delpup.IsOpen = true;
        id = (((Button)sender).Tag).ToString();
    }

The id is binded to this using tag and once they select yes the following function is called to delete the item from the database:

private async void YesBtn_Click(object sender, RoutedEventArgs e)
    {
        await ctv.combatDrillsTable.DeleteDrillAsync(id);
        var addSuccess = new MessageDialog("Drill Deleted");
        await addSuccess.ShowAsync();

    }
UWP122
  • 39
  • 7

1 Answers1

0

Inside the eventHandler:

private async void NewSubmitBtn_Click(object sender, RoutedEventArgs e)

you are setting the value of the id variable to the Content of the button instead of the Tag.

Change :

String id = (String)((Button)sender).Content;

to this:

String id = (String)((Button)sender).Tag;

Make sure to call the right event handler because on the click event of the update button your are calling the handler :

<Button Tag="{Binding Id}" Content="Update" Click="UpdateBtn_Click" Style="{StaticResource DrillButtons}" ></Button>

instead of

<Button Tag="{Binding Id}" Content="Update" Click="NewSubmitBtn_Click" Style="{StaticResource DrillButtons}" ></Button>

Best of luck!

Iza Atsou
  • 181
  • 2
  • 6
  • I actully had it set to tag before and updatebtn calls a popup that calls NewSubmitBtn I tried to implement yours still got the same results a null. – UWP122 Nov 04 '17 at 13:41
  • @UWP122 Provide more info and code, it's not clear what the actual problem is. If the **UpdateBtn_Click** launches a MessageDialog containing a button that is hooked to the **NewSubmit_Btn_Click** handler, then the sender is the button inside the MessageDialog. Is the tag set on that button too? – Iza Atsou Nov 04 '17 at 18:08
  • I updated the code like you asked, to clear things up basically what I want to do is take the id from a data template displayed in a list which is provided by a list of items and depending on what one the user clicks on it will delete that from an Azure portal easy table. – UWP122 Nov 06 '17 at 11:59
  • @UWP122 Have you solved your issue by Iza Eddi-son Atsou's suggestion? – Xie Steven Nov 13 '17 at 06:10