如何在Windows Phone中更改同一ListBox中另一个对象的可见性
本文关键字:ListBox 一个对象 可见性 Windows Phone | 更新日期: 2023-09-27 18:23:51
我的XAML摘录:
<ListBox x:Name="list" Margin="0,0,0,0" ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Background="LightGray" Margin="0,5,0,0" >
<Image Name="like" Source="/Images/Like1.png" Tap="like_Tap"/>
<Image Name="unlike" Visibility="Collapsed" Source="/Images/UNLike1.png" Tap="unlike_Tap"/>
<Image Name="comment" Source="/Images/Comment1.png" Margin="30,0,0,0"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这是我的问题
在我的C#代码中:
private void like_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
// Here I'm unable to change the visibility of either image. How do I do this?
}
我希望这种行为也能传播到不同的按钮上。
我该怎么做?谢谢
我建议使用binding
来实现这一点,而不是访问代码中的图像。
在Item
类中创建bool类型的属性,并将该属性与映像的Visibility
属性绑定。您需要"可见性到布尔转换器"。
<Image Visibility="{Binding Path=ShowLikeImage Converter={Static Resource VisibilityToBooleanConverter}}" Source="/Images/UNLike1.png" Tap="unlike_Tap"/>
在点击事件中,您只需更改ShowLikeImage
属性,UI就会自动更新。
我建议用另一种方式,将其用作多用途按钮。
你的XAML应该是这样的:
<StackPanel Orientation="Horizontal" Background="LightGray" Margin="0,5,0,0" >
<Image Name="Likebtn" Source="/Images/Like1.png" Tap="Likebtn_Tap"/>
<Image Name="commentbtn" Source="/Images/Comment1.png" Margin="30,0,0,0"/>
</StackPanel>
你的方法是这样的:
private void Likebtn_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
Image img = sender as Image;
if (img == null) return;
switch (img.Source.ToString())
{
case "The like button's location":
//change do your like logic, and then change it to an unlike button
break;
//the opposite for the unlike button's location
}
}