2017-09-07 146 views
0

我有一個StackPanel,我想隱藏,當沒有從一個ListView中選擇的項目在同一窗口中。目前,當我打開窗口時,沒有選定的項目並且StackPanel被隱藏,但是當我從ListView中選擇某些內容時,不會發生任何更改。NullToVisibilityConverter沒有被選擇更改觸發

我綁定的SelectedItem在ListView像:

<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"> 

其中「SelectedIssue」在我的視圖模型(我的整個窗口具有相同的DataContext)的自定義類的屬性。我目前正在結合我的StackPanel的可見性屬性爲:

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

我的轉換器:

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

我缺少什麼?

編輯:這是我的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

您可以顯示SelectedIssue的的getter/setter?當SelectedIssue更改時,可能只是遺漏了屬性更改事件。 UI不會知道價值已經改變,除非你告訴它... – CodexNZ

+0

剛剛添加。我正在舉辦這個活動,對吧? –

+0

當綁定進入時,您是否在Visual Studio的輸出窗口中看到任何綁定錯誤?有時他們很容易錯過。 –

回答

0

明顯的問題是,你還沒有實現INotifyPropertyChanged或者你不提高(在SelectedIssue屬性setter)的PropertyChanged事件。

但你可以做簡單的,結合了StackPanel直接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}" >