簡體   English   中英

將標簽添加到ListView Item XAML [UWP]

[英]Adding a tag to ListView Item XAML [UWP]

我正在嘗試向我的ListView項目添加標簽(例如向按鈕添加標簽)。

我的XAML:

        <ListView x:Name="gamesList" IsItemClickEnabled="True" ItemClick="gamesList_ItemClick">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <ContentPresenter/>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <Grid Height="200" Width="896">
                            <Rectangle Fill="#FFCCCCCC" HorizontalAlignment="Left" Height="64" Margin="10,71,0,0" Stroke="#FFCCCCCC" VerticalAlignment="Top" Width="100"/>
                            <TextBlock x:Name="voteCount" HorizontalAlignment="Left" Margin="50,79,0,0" TextWrapping="Wrap" Text="{Binding Votes, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Foreground="#FF107C10" FontWeight="Bold"/>
                            <TextBlock x:Name="voteCountText" HorizontalAlignment="Left" Margin="37,104,0,0" TextWrapping="Wrap" Text="votes" VerticalAlignment="Top" Foreground="#FF292C33"/>
                            <Button x:Name="voteButton" Tag="{Binding Slug, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Content="{Binding BtnVoted, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="10,143,0,0" VerticalAlignment="Top" Width="100" Background="#FF888888" Foreground="#FF292C33" Click="voteButton_Click" IsEnabled="{Binding BtnEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                            <TextBlock x:Name="voteTitle" HorizontalAlignment="Left" Margin="144,80,0,0" TextWrapping="Wrap" Text="{Binding Title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" FontSize="22" Foreground="#FF292C33"/>
                            <TextBlock x:Name="voteError" HorizontalAlignment="Left" Margin="144,115,0,0" TextWrapping="Wrap" Text="You already voted for this game" VerticalAlignment="Top" Visibility="{Binding Voted, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                            <Rectangle Fill="Black" HorizontalAlignment="Left" Height="4" Margin="10,180,0,0" Stroke="#FFCCCCCC" VerticalAlignment="Top" Width="801"/>
                        </Grid>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

C#(部分):

        public async void OnLoaded(object sender, RoutedEventArgs e)
        {
            var response = await start();

            dynamic dynJson = JsonConvert.DeserializeObject(response);

            foreach (var item in dynJson)
            {
                Object votedGame = localSettings.Values[item.slug.ToString()];
                string voted = "Collapsed";
                string btnVoted = "VOTE";
                bool btnEnabled = true;

                if(votedGame != null)
                {
                    voted = "Visible";
                    btnVoted = "VOTED!";
                    btnEnabled = false;
                }

                listofGames.Add(new Games { Title = item.name, Votes = item.votes, Slug = item.slug, Voted = voted, BtnVoted = btnVoted, BtnEnabled = btnEnabled });
            }

            gamesList.ItemsSource = listofGames;
        }

        public class Games
        {
            public string Title { get; set; }
            public string Slug { get; set; }
            public string Voted { get; set; }
            public string BtnVoted { get; set; }
            public bool BtnEnabled { get; set; }
            public int Votes { get; set; }
        }

標簽應包含有關(在這種情況下)游戲標題的數據。

因此,當用戶單擊項目時,單擊功能應處理該事件,並且我需要能夠從標簽中獲取數據。

您不需要標簽。 在XAML中,諸如ListView之類的集合控件會選擇您為其提供的實際類實例。 他們試圖避免讓您在代碼中盡可能多地處理ListViewItems 因此,事件處理程序中的e.ClickedItem不會是ListViewItem 這將是您的商品,您在List<Games>提供給ListView.ItemsSourceGames實例之一。 無需像爺爺時代那樣與Tag混為一談。

private void listView_ItemClick(object sender, ItemClickEventArgs e)
{
    //var lv = sender as ListView;
    Games clickedGames = e.ClickedItem as Games;

    //  Do stuff with the clicked item. 
}

順便說一下,它從您的代碼中看起來好像listofGames是一個私有字段。 那不是一個好主意。 由於它不是ObservableCollection ,因此對該列表的任何添加或刪除都不會反映在ListView ,因此List實際上應該在OnLoaded方法的本地,以防止這種情況發生。 或者更好的是,將其類型更改為ObservableCollection<Games> 這將替代List<T>

就像我說過的,很不幸,我認為您偶然發現了一個非常糟糕的教程。

執行此操作的“正確”方法(實際上實際上是這樣)是擁有一個視圖模型,該視圖模型將您的Games集合作為一個公共屬性,以及一個public Games SelectedGame屬性。 然后將它們分別綁定到ListView上的ItemsSourceSelectedItem 沒有ItemClicked處理程序。 但是,我會毫不猶豫地敦促您放棄正在做的事情並重新開始。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM