0

我试图从绑定到按钮标签的按钮中获取 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 使用标签绑定到这个,一旦他们选择是,就会调用以下函数从数据库中删除项目:

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

    }
4

1 回答 1

0

在事件处理程序内部:

private async void NewSubmitBtn_Click(object sender, RoutedEventArgs e)

您将id变量的值设置为按钮的Content而不是Tag

改变 :

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

对此:

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

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

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

祝你好运!

于 2017-11-03T21:50:05.387 回答