0
votes

I have a StackPanel that I would like to hide when there isn't a selected item from a ListView in the same window. Currently, when I open the window, there is no selected item and the StackPanel is hidden, but when I do select something from the ListView, no change occurs.

I am binding the SelectedItem in the ListView like:

<ListView
     MinHeight="0"
     MaxHeight="500"
     Margin="10,10,10,0"
     Background="#e7f5f4"
     BorderThickness="0"
     ItemsSource="{Binding Issues}"
     ScrollViewer.HorizontalScrollBarVisibility="Hidden"
     SelectedItem="{Binding SelectedIssue}"
     SelectionMode="Single">

Where "SelectedIssue" is a custom class property in my ViewModel (my entire window has the same DataContext). I am currently binding the Visibility property of my StackPanel as:

<StackPanel
     Grid.Column="1"
     Margin="13,0,0,5"
     VerticalAlignment="Bottom"
     Background="#ebf7f6"
     Orientation="Horizontal"
     Visibility="{Binding SelectedIssue, 
     Converter={StaticResource NullToVisibilityConverter},
     UpdateSourceTrigger=PropertyChanged}">

And my converter is:

public class NullToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value == null ? Visibility.Collapsed : Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

What am I missing?

EDIT: Here is my getter/setter

    private Issue _selectedIssue;
    public Issue SelectedIssue
    {
        get { return _selectedIssue; }
        set { Set(ref _selectedIssue, value); }
    }

    public void RaisePropertyChanged([CallerMemberName]string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public bool Set<T>(ref T storage, T value, [CallerMemberName]string propertyName = null)
    {
        if (Equals(storage, value))
            return false;
        storage = value;
        RaisePropertyChanged(propertyName);
        return true;
    }
1
Can you show the getter/setter for SelectedIssue? Probably just missing a property changed event for when the SelectedIssue changes. The UI won't know the value has changed unless you tell it...CodexNZ
Just added. I am raising the event, right?Michael Holvey
Are you seeing any binding errors in Visual Studio's Output Window when the bindings go in to effect? Sometimes they are easy to miss.Bradley Uffner
@Michael Holvey you are missing the name of the property when calling Set(ref _selectedIssue, value); should be Set(ref _selectedIssue, value, "SelectedIssue");CodexNZ

1 Answers

0
votes

apparently the problem is that you have not implemented INotifyPropertyChanged or you not raise (in the SelectedIssue property setter) the PropertyChanged Event.

but you can do simpler, binding the StackPanel directly to ListView.SelectedItem:

<ListView x:Name="listView"
     MinHeight="0"
     MaxHeight="500"
     Margin="10,10,10,0"
     Background="#e7f5f4"
     BorderThickness="0"
     ItemsSource="{Binding Issues}"
     ScrollViewer.HorizontalScrollBarVisibility="Hidden"
     SelectedItem="{Binding SelectedIssue}"
     SelectionMode="Single">

<StackPanel
     Grid.Column="1"
     Margin="13,0,0,5"
     VerticalAlignment="Bottom"
     Background="#ebf7f6"
     Orientation="Horizontal"
     Visibility="{Binding SelectedItem, ElementName=listView
     Converter={StaticResource NullToVisibilityConverter}" >