I am writing a Windows Phone 8.1 (WINRT) App. I used a ListView to populate items:
XAML:
xmlns:ConvertersFile="using:DrFit.Converters"
<Page.Resources>
<ConvertersFile:BoolToVisConverter x:Key="BooleanToVisibilityConverter"/>
</Page.Resources>
<ListView x:Name="EquipmentListView" DataContext="{Binding}" SelectionChanged="EquipmentListView_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding EquipmentDetails}"
x:Name="EquipmentDetails"
Visibility="{Binding Path=ShowHide, Converter={StaticResource BooleanToVisibilityConverter}}"/>>
</TextBlock>
</Grid>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
C# Class For ListView:
class EquipmentClassTemporary
{
public string EquipmentTitle { get; set; }
public string EquipmentDetails { get; set; }
public bool ShowHide { get; set; }
}
C#:
ObservableCollection<EquipmentClassTemporary> EquipmentListViewList = new ObservableCollection<EquipmentClassTemporary>();
private void GenerateEquipmentListView()
{
for(int i=0; i< SingletonInstance.strHeading.Length;i++)
{
EquipmentListViewList.Add(new EquipmentClassTemporary { EquipmentTitle = SingletonInstance.strHeading[i], EquipmentDetails = SingletonInstance.strEquipment[i], ShowHide=false });
}
EquipmentListView.ItemsSource = EquipmentListViewList;
}
private void EquipmentListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
{
var ClickedItem = e.AddedItems[0];
((EquipmentClassTemporary)ClickedItem).ShowHide = true;
}
if (e.RemovedItems.Count > 0)
{
var UnClickedItem = e.RemovedItems[0];
((EquipmentClassTemporary)UnClickedItem).ShowHide = false;
}
}
BoolToVisConverter:
public class BoolToVisConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
try
{
if ((bool)value)
{
return Visibility.Visible;
}
else
{
return Visibility.Collapsed;
}
}
catch
{
return Visibility.Collapsed;
}
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
try
{
if ((bool)value)
{
return Visibility.Visible;
}
}
catch { }
return Visibility.Collapsed;
}
}
The Visibility of EquipmentDetails Textblock is not getting changed.?
you need to add the following code to implement INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
then call OnPropertyChanged from the property setter like
private bool showHide;
public bool ShowHide
{
get { return showHide; }
set { showHide = value;
OnPropertyChanged("ShowHide"); }
}