2012-08-13 69 views
0

我有一個ObservableCollection中的字符串列表,我想將它綁定到一個datagrid,listbox或其他東西,這將允許我在列表中的一個項目上添加一個雙擊事件。如果你不能說,我是新來的WPF!ObservableCollection WPF MVVM中的字符串列表

代碼:

private ObservableCollection<string> _searchResults; 
    public ObservableCollection<string> SearchResults 
    { 
     get { return _searchResults; } 
     set 
     { 
      _searchResults = value; 
      OnPropertyChanged("SearchResults"); 
     } 
    } 

<Grid> 
    <DataGrid AutoGenerateColumns="False" 
       Name="MyGrid" 
       Height="400" 
       Width="400" 
       ItemsSource="{Binding SearchResults}" 
       SelectedItem="{Binding MySelectedItemProperty, UpdateSourceTrigger=PropertyChanged}"/> 

</Grid> 

作爲一個方面說明,我也使用Caliburn.Micro

回答

1

在你看來,你會爲你的Datagrid做這樣的事情:

<DataGrid AutoGenerateColumns="False" Name="MyGrid" 
    ItemsSource="{Binding MyListofStrings}" 
    SelectedItem="{Binding MySelectedItemProperty, UpdateSourceTrigger=PropertyChanged}" 
       CommandHelper:MouseDoubleClick.Command="{Binding MyEditCommand}"> 

然後在您的視圖模型中:

  private ObservableCollection<MyStrings> _MyListofStrings; 
     public ObservableCollection<MyStrings> MyListofStrings 
     { 
      get { return _MyListofStrings; } 
      set 
      { 
        _MyListofStrings = value; 
       OnPropertyChanged("MyListofStrings");  //Used for 2 way binding. 
      } 
     } 

然後用您的數據類型填充「MyListofStrings」。

+0

您應該提及'CommandHelper:MouseDoubleClick.Command'的來源。它不是標準WPF的一部分。 – 2012-08-13 19:16:46

+0

那麼你在哪裏得到?我正在創建私人ObservableCollection s =新的ObservableCollection 。 – Josh 2012-08-13 19:31:55

+0

另外網格沒有被填充。 – Josh 2012-08-13 19:32:40

相關問題