有没有办法在UWP工具包PullToRefreshList中获取项目的索引?

时间:2017-11-03 17:35:33

标签: c# xaml uwp windows-community-toolkit

我试图从我绑定到按钮标签的按钮中获取Id,但是当我尝试在方法中获取它时,即使我可以设置上下文和文本并且Id将出现,也会抛出空值错误屏幕很好。

所以现在我试图用索引标记列表中的项目,这样当数据模板生成它们时,我可以轻松地调用它们并将它们与我的c#代码中的id进行比较。

XAML列表:

<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

上面的堆栈面板中的这两个按钮是我试图绑定Id以便稍后检索的地方。

<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>

下面是从按钮中获取Id的方法:

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

    }

继承数据项代码:

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

}
}

我添加的删除弹出窗口:

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

id绑定到此using标签,一旦选择yes,将调用以下函数从数据库中删除该项:

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

    }

1 个答案:

答案 0 :(得分:0)

在eventHandler中:

private async void NewSubmitBtn_Click(object sender, RoutedEventArgs e)

您要将 id 变量的值设置为按钮的内容,而不是标记

更改:

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

到此:

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

确保调用正确的事件处理程序,因为您正在调用更新按钮的click事件 处理程序:

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

而不是

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

祝你好运!