2015-02-11 52 views
0

我所有的數據綁定都可以在整個應用程序中工作。我已經嘗試了數據綁定ItemsSource的每種模式。該列表在開始時加載,但在使用我製作的上下文菜單時不會更新。我已使用WriteLine進行測試,以確保其他所有功能都能正常工作,而且確實如此。我的列表框在刪除或添加項目時不會更新,儘管數據綁定

XAML

<ListBox Grid.Row="1" SelectionMode="Single" BorderThickness="0" 
    SelectionChanged="ListBox_SelectionChanged" 
    SelectedIndex="{Binding Path=Data.SelectedFeedIndex, Mode=TwoWay}" 
    SelectedItem="{Binding Path=Data.SelectedFeed, Mode=TwoWay}" 
    ItemsSource="{Binding Path=Data.Feeds}" 
    ScrollViewer.HorizontalScrollBarVisibility="Disabled"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Grid> 
       <TextBlock Text="{Binding Path=Title}" MouseDown="TextBlock_MouseDown" /> 
       <TextBox Text="{Binding Path=Title}" 
        Visibility="Collapsed" KeyDown="TextBox_KeyDown" Padding="1" /> 
       </Grid> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
     <ListBox.ContextMenu> 
      <ContextMenu> 
       <MenuItem Header="New" Command="{Binding Path=Data.NewFeed}" /> 
       <MenuItem Header="Delete" Command="{Binding Path=Data.DeleteFeed}" /> 
      </ContextMenu> 
     </ListBox.ContextMenu> 
     <ListBox.InputBindings> 
      <KeyBinding Key="Delete" Command="{Binding Path=Data.DeleteFeed}" /> 
     </ListBox.InputBindings> 
    </ListBox> 

屬性

private List<Feed> _Feeds = new List<Feed>(); 
public List<Feed> Feeds 
{ 
    get 
    { 
     return _Feeds; 
    } 
    set 
    { 
     _Feeds = value; 
     onPropertyChanged("Feeds"); 
    } 
} 

命令

public ICommand DeleteFeed 
{ 
    get 
    { 
     return new RelayCommand(ExecuteDeleteFeed, CanDeleteFeed); 
    } 
} 

public void ExecuteDeleteFeed(object parameter) 
{ 
    Feeds.RemoveAt(SelectedFeedIndex); 
    SelectedFeedIndex = nextIndex; 
    onPropertyChanged("Feeds"); 
} 

public bool CanDeleteFeed(object parameter) 
{ 
    return Feeds.Count > 1; 
} 

我收到中通過與上述列表框結合DEX:

private int _SelectedFeedIndex; 
public int SelectedFeedIndex 
{ 
    get 
    { 
     return _SelectedFeedIndex; 
    } 
    set 
    { 
     _SelectedFeedIndex = value; 
     onPropertyChanged("SelectedFeedIndex"); 
    } 
} 

編輯

我試圖改變到ObservableCollection,但我得到了下面的錯誤。

System.Windows.Data Error: 40 : BindingExpression path error: 'Feeds' property not found on 'object' ''Library' (HashCode=60811181)'. BindingExpression:Path=Data.Feeds; DataItem='MainViewModel' (HashCode=34760343); target element is 'ListBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable') 
System.Windows.Data Error: 40 : BindingExpression path error: 'Feeds' property not found on 'object' ''Library' (HashCode=60811181)'. BindingExpression:Path=Data.Feeds; DataItem='MainViewModel' (HashCode=34760343); target element is 'ComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable') 

新屬性

ObservableCollection<Feed> Feeds = new ObservableCollection<Feed>(); 

哦,沒關係。我把它變成了一個財產。它現在有效。非常感謝。我以爲你只需要ObservableCollection,如果你沒有手動使用INotifyProperty的所有屬性。現在有道理。

回答

1

您需要將List更改爲ObservableCollection,因爲List類不會生成通知其內容已更改的事件。結果ListBox將顯示列表的初始內容,但之後從未更新。 ObservableCollection確實會生成更改事件並將執行您所需的操作。

+0

即使對類中的所有屬性使用INotifyProperty,它也不起作用?我現在正在嘗試,但我得到一個錯誤。我會將其添加到帖子中。 – Expotr 2015-02-11 23:09:11

+0

啊,你需要在集合類本身上添加INotifyCollectionChanged,這樣ListBox可以看到何時添加和刪除了集合中的項目。 INotifyPropertyChanged將用於Feed類以通知屬性值的更改。 – 2015-02-11 23:23:09

相關問題