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();
}