I'm trying to add a tag to my ListView Item (like adding a tag to a button).
My 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# (Partially):
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; }
}
The tag should contain data about (in this case) a game title.
So, when the user clicks the item a click function should handle the event and I need to be able to get the data from the tag.
Tag
property? Don't do that, use MVVM. Also, a little off topic -- can you explain yourItemContainerStyle
? – 15ee8f99-57ff-4f92-890c-b56153Tag={Binding whatevs, Mode=TwoWay
suggests you're lacking a lot of knowledge here. Might want to invest in a couple books on the subject and do some reading. – user1228