【问题标题】:Is there a way to get the index of a item in the UWP toolkits PullToRefreshList?有没有办法获取 UWP 工具包 Pull To Refresh List 中项目的索引?
【发布时间】:2017-11-03 17:35:32
【问题描述】:

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

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

    }

【问题讨论】:

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


    【解决方案1】:

    在 eventHandler 内部:

    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>
    

    祝你好运!

    【讨论】:

    • @UWP122 提供更多信息和代码,目前尚不清楚实际问题是什么。如果 UpdateBtn_Click 启动一个 MessageDialog,其中包含一个与 NewSubmit_Btn_Click 处理程序挂钩的按钮,那么发送者就是 MessageDialog 内的按钮。那个按钮上也设置了标签吗?
    • 我按照你的要求更新了代码,基本上我想要做的是从列表中显示的数据模板中获取 id,该列表由项目列表提供,具体取决于什么用户单击它会从 Azure 门户简单表中删除它。
    • @UWP122 你的问题是否被 Iza Eddi-son Atsou 的建议解决了?
    猜你喜欢
    • 2020-11-18
    • 2020-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2012-05-31
    • 2013-01-14
    • 2015-08-07
    相关资源
    最近更新 更多