简体   繁体   中英

Bind Visibility of Textblock with Boolean not working

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"); }
    }

Once the propertyChanged event handler is registered, you can use many properties in that class. No need of separate "OnPropertyChanged" functions.

public bool ShowHide
{
    get { return showHide; }
    set { showHide = value;
    OnPropertyChanged("ShowHide"); }
}

public int newProperty
{
    get { return _newProperty; }
    set { _newProperty = value;
    OnPropertyChanged("newProperty"); }
}

public string NewStringProp
{
    get { return _NewStringProp; }
    set { _NewStringProp = value;
    OnPropertyChanged("NewStringProp"); }
}

and so on...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM