我正在尝试为我的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; }
}
标签应包含有关(在本例中)游戏标题的数据。
因此,当用户点击该项时,点击功能应该处理该事件,我需要能够从标签中获取数据。
答案 0 :(得分:3)
您不需要标签。在XAML中,像ListView
这样的集合控件选择你给它们的实际类实例。他们尽量避免让您尽可能地在代码中处理ListViewItems
。因此,您的事件处理程序中的e.ClickedItem
将不是ListViewItem
;它将是您的项目,是您在Games
ListView.ItemsSource
中提供给List<Games>
的{{1}}个实例之一。在爷爷的日子里不需要像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
中,因此列表应该真正属于OnLoaded
方法的本地,以防止发生。或者更好的是,将其类型更改为ObservableCollection<Games>
。这将成为List<T>
的直接替代品。
正如我所说,我认为你偶然发现了一个非常糟糕的教程。
执行此操作的“正确”方法(实际上确实如此)是拥有一个视图模型,其中Games
集合作为公共属性,并且public Games SelectedGame
属性也是如此。然后,您将分别绑定到ItemsSource
上的SelectedItem
和ListView
。没有ItemClicked
处理程序。但是,我犹豫地强烈要求你放弃你正在做的事情并重新开始。