2013-10-18 16 views
1

我有一個簡單的MVVM項目,我正在學習。我試圖通過一個ICommand命令添加到ObservableCollection中,但我無法?如何從Icommand命令添加新的ObservableCollection?

MainWindow.cs我還沒有添加任何東西*

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:Local="clr-namespace:WpfApplication1" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <Grid.DataContext> 
     <Local:ViewModel></Local:ViewModel> 
    </Grid.DataContext> 

    <ListView Grid.Row="0" x:Name="lstNames" Margin="5,5,5,5" Grid.Column="1" ItemsSource="{Binding View_}"> 
     <ListView.View> 
      <GridView x:Name="Setting_Items"> 
       <GridViewColumn Header="Setting_A" DisplayMemberBinding="{Binding View_String}"/> 
      </GridView> 
     </ListView.View> 
    </ListView> 

    <TextBox Height="23" 
      HorizontalAlignment="Left" 
      Margin="145,195,0,0" 
      Name="textBox1" 
      VerticalAlignment="Top" 
      Width="120" /> 
    <ComboBox Height="23" 
      HorizontalAlignment="Left" 
      Margin="269,195,0,0" 
      Name="My_ComboBox" 
      VerticalAlignment="Top" 
      Width="222"  
      ItemsSource="{Binding View_}"/> 
    <Button Content="Message Text" 
      Height="23" 
      HorizontalAlignment="Left" 
      Margin="52,166,0,0" 
      Name="button1" 
      VerticalAlignment="Top" 
      Width="75" 
      CommandParameter="{Binding Text, ElementName=textBox1}" 
      Command="{Binding Print_Line}"/> 
    <Button Content="Add To Drop" 
      Height="23" 
      HorizontalAlignment="Left" 
      Margin="52,195,0,0" 
      Name="button2" 
      VerticalAlignment="Top" 
      Width="75" 
      /> 


</Grid> 


public class View 
{ 
    public string View_String {get; set;} 
} 

public class SimpleDelegateCommand : ICommand 
{ 
    Action<object> _executeDelegate; 

    public SimpleDelegateCommand(Action<object> executeDelegate) 
    { 
     _executeDelegate = executeDelegate; 
    } 

    public void Execute(object parameter) 
    { 
     _executeDelegate(parameter); 
    } 

    public bool CanExecute(object parameter) { return true; } 
    public event EventHandler CanExecuteChanged; 
} 

public class ViewModel 
{ 
    private ObservableCollection<View> _View; 

    public string _View_String { get; set; } 

    public ObservableCollection<View> View_ 
    { 
     get { return _View; } 
     set { _View = value; } 
    } 

    ICommand _Print_Line = new SimpleDelegateCommand((x) => MessageBox.Show(x.ToString())); 

    ICommand _Add_Line = new SimpleDelegateCommand((x) => 
     View_ = new ObservableCollection<View>() /////////Error HERE 
     { 
      new View(){View_String = x.ToString()} 
     } 
     ); 


    public ViewModel() 
    { 
     View_ = new ObservableCollection<View>() 
     { 
      new View(){View_String = "Setting 1"}, 
      new View(){View_String = "Setting 2"} 
     }; 
    } 

    public ICommand Print_Line { get { return _Print_Line; } } 
    public ICommand Add_Line { get { return _Add_Line; } } 
} 

如何使用ICommand命令添加到我的ObservableCollection?或者我該怎麼辦?如何使用ICommand命令執行多個任務,例如: ICommand _Print_Line = new SimpleDelegateCommand((x)=> MessageBox.Show(x.ToString()); MessageBox.Show(「Second task 「));

+0

那沒有工作 –

+1

是你的問題解決? – Nitin

+0

不,我只是不太瞭解WPF的核心,但我知道這是一個愚蠢的簡單的解決方案。我只是想添加到我的ObservableCollection從一個按鈕點擊,文本框中的文本!大聲笑 –

回答

1

執行多個任務:

_Print_Line = new SimpleDelegateCommand((x) => { 
    MessageBox.Show(x.ToString()); 
    MessageBox.Show("Second task"); 
}); 

添加私訂到您的命令的領域,進入它只是從你的類

private ICommand print_Line; 
public ICommand Print_Line { 
          get { return print_Line; } 
          private set { print_Line = value; } 
          } 

private ICommand add_Line; 
public ICommand Add_Line { 
          get { return add_Line; } 
          private set { add_Line = value; } 
          } 

或許也可以幫助你這樣說:

private ICommand print_Line; 
public ICommand Print_Line { get { return print_Line; } } 

private ICommand add_Line; 
public ICommand Add_Line{ get { return add_Line; } } 
+0

「」「Facepalm!「」「」 –

+0

對不起,我很累,你是什麼意思?:D – Krekkon

+0

,你能告訴,你的命令有什麼錯誤嗎? – Krekkon

0

將您的Commands初始化代碼移動到您的constructorViewModel類。您正在嘗試訪問尚未構建的View_,因此發生錯誤。構造函數之外的對象初始化器不應該調用實例方法/屬性,因爲直到那時該類纔會被構造。

ICommand _Print_Line; 

ICommand _Add_Line; 


public ViewModel() 
{ 
    _Print_Line = new SimpleDelegateCommand((x) => MessageBox.Show(x.ToString())); 

    _Add_Line = new SimpleDelegateCommand((x) => 
    View_ = new ObservableCollection<View>() /////////Error HERE 
    { 
     new View(){View_String = x.ToString()} 
    } 
    ); 

    View_ = new ObservableCollection<View>() 
    { 
     new View(){View_String = "Setting 1"}, 
     new View(){View_String = "Setting 2"} 
    }; 
} 
0

首先,你的代碼幾乎不可讀。錯誤非常簡單。初始化字段時,不能引用非靜態字段。 ICommand _Add_Line是一個實例字段。就像_View一樣。如果您想引用它,請在類的構造函數中初始化ICommand _Add_Line。每次調用非靜態字段或方法時,都需要該類的實例來獲取其值。 在這個問題上有幾個答案stackoverflow

+0

Bhaha勉強可讀嗎? –

+0

是的,你使用的這種'編碼約定'不是我的一杯茶。不是一個簡單的camelCase?! –

+0

基本上,因爲我是來自幾個月前開始的Windows窗體的WPF品牌, IDK什麼是camelCase。 –