我在列表框中有一个带有一些联系人号码的列表框现在我想在列表框中的每个联系人项目中添加显示和隐藏按钮当用户按下隐藏按钮联系人将隐藏,当用户按下显示按钮联系人将显示和反之亦然我如何在WP8中做到这一点.. 我正在使用以下代码请帮助我..
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" Margin="0, 10, 0, 10">
<Border BorderThickness="0" HorizontalAlignment="Left" VerticalAlignment="Center" BorderBrush="{StaticResource PhoneAccentBrush}" Margin="5, 0, 0, 0">
<Image Source="{Binding ImageUrl, Converter={StaticResource kconverter}}" Width="48" Height="48" Stretch="Fill"/>
</Border>
<TextBlock x:Name="item_name" Text="{Binding Name, Mode=OneWay}" FontSize="{StaticResource PhoneFontSizeLarge}" Margin="10, 0, 0, 0" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0, 10, 0, 10">
<Border BorderThickness="0" HorizontalAlignment="Left" VerticalAlignment="Center" BorderBrush="{StaticResource PhoneAccentBrush}" Margin="355, 0, 0, 0">
<Image Name="onimg" Visibility="Visible" Source="/Assets/Images/blue.button.png" Width="85" Height="20" Tap="Image_TapOn" Stretch="Fill"/>
</Border>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0, 10, 0, 10">
<Border BorderThickness="0" HorizontalAlignment="Left" VerticalAlignment="Center" BorderBrush="{StaticResource PhoneAccentBrush}" Margin="355, 0, 0, 0">
<Image Name="offimg" Visibility="Collapsed" Source="/Assets/Images/setting-h.png" Width="85" Height="48" Tap="Image_TapOff" Stretch="Fill"/>
</Border>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
答案 0 :(得分:0)
您好使用此代码希望它适合您。 注意:仅将ObservableCollection绑定到ListBox以使用INotifyPropertyChanged
XAML:
<Grid Grid.Row="1">
<ListBox x:Name="lstContact">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Border Background="#404040"
Height="45"
Width="60"
Tag="{Binding ContactId}"
Tap="_Collapsed">
<TextBlock Text="-"
FontSize="24"
VerticalAlignment="Center"
TextAlignment="Center"
Foreground="White" />
</Border>
<Border Name="BrCount"
Width="160"
Height="45"
BorderBrush="#404040"
BorderThickness="0"
Margin="10,0">
<TextBlock Name="LblCont"
Text="{Binding ContactName}"
FontSize="24"
VerticalAlignment="Center"
TextAlignment="Center"
Foreground="#404040"
Visibility="{Binding _Visibility}" />
</Border>
<Border Background="#404040"
Height="45"
Width="60"
Tag="{Binding ContactId}"
Tap="_Visible">
<TextBlock Text="+"
FontSize="24"
VerticalAlignment="Center"
TextAlignment="Center"
Foreground="White" />
</Border>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
C#:
private ObservableCollection<Contact> _contact = new ObservableCollection<Contact>();
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
_contact = new ObservableCollection<Contact>();
for (int i = 0; i < 10; i++)
{
_contact.Add(new Contact() { ContactId = i, ContactName = "Name " + i, _Visibility = Visibility.Visible });
}
lstContact.ItemsSource = _contact;
}
private void _Collapsed(object sender, System.Windows.Input.GestureEventArgs e)
{
Border bdr = (Border)sender;
_contact.Where(X => X.ContactId == Convert.ToInt32(bdr.Tag.ToString())).First()._Visibility = Visibility.Collapsed;
}
private void _Visible(object sender, System.Windows.Input.GestureEventArgs e)
{
Border bdr = (Border)sender;
_contact.Where(X => X.ContactId == Convert.ToInt32(bdr.Tag.ToString())).First()._Visibility = Visibility.Visible;
}
public class Contact : INotifyPropertyChanged
{
public int ContactId { get; set; }
public string ContactName { get; set; }
public Visibility _visibility;
public Visibility _Visibility
{
get { return _visibility; }
set { SetProperty(ref _visibility, value); }
}
protected void SetProperty<T>(ref T storage, T value, [System.Runtime.CompilerServices.CallerMemberName] String propertyName = null)
{
if (!object.Equals(storage, value))
{
storage = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
protected void RaisePropertyChanged([System.Runtime.CompilerServices.CallerMemberName] String propertyName = null)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}