2012-10-12 13 views
0

我是WPF C#的新手。我在我的構造函數中添加了一個方法,將子元素添加到堆棧面板。我有2個xaml文件。 mainone有一個堆疊面板,另一個有一個標籤。無法從按鈕上的ViewModel添加子項點擊

主窗口視圖:

<Grid Grid.Row="1" Name="VoltageChannels" > 
     <StackPanel Height="Auto" Name="stackPanel" Width="Auto" MinHeight="300"> </StackPanel> 
    </Grid> 

<Button Content="Refresh All" Command="{Binding AddChildCommand}" Name="RefreshAllBtn" /> 


public void OnChildAdd() 
    { 
     foreach (VoltageBoardChannel mVoltageChannelViewModel in mVoltageViewModel.VoltageChannelList) 
     { 
      VoltageChannelView mVoltageChannelView = new VoltageChannelView(); 
      mVoltageChannelView.Margin = new Thickness(2); 
      mVoltageChannelView.ChannelInfo = mVoltageChannelViewModel; 
      stackPanel.Children.Add(mVoltageChannelView); 
     } 
    } 

我想從我的ViewModel類訪問該方法通過點擊一個按鈕來添加的孩子。基本上我有一個列表,其中有一組項目。這些項目應顯示按鈕點擊:)這裏是觀與ViewModel類:其中有我的標籤

public viewModel() 
{ 

} 
public List<VoltageBoardChannel> VoltageChannelList 
    { 
     get 
     { 
      return channelList; 
     } 

     set 
     { 
      channelList = value; 
      OnPropertyChanged("ChannelList"); 
     } 
    } 

List<VoltageBoardChannel> channelList = new List<VoltageBoardChannel>(0); 

    // VoltageBoardChannel has Channel name and avalable as property. 
    List<VoltageBoardChannel> redhookChannels = new List<VoltageBoardChannel> 
    { 
     new VoltageBoardChannel { ChannelName = "", IsAvailable = false}, 
     new VoltageBoardChannel { ChannelName = "VDD_IO_AUD", IsAvailable = true}, 
     new VoltageBoardChannel { ChannelName = "VDD_CODEC_AUD", IsAvailable = true}, 
     new VoltageBoardChannel { ChannelName = "VDD_DAL_AUD", IsAvailable = true}, 
     new VoltageBoardChannel { ChannelName = "VDD_DPD_AUD", IsAvailable = true},    
    }; 

    private ICommand mRefreshAllCommand; 
    public ICommand AddChildCommand 
    { 
     get 
     { 
      if (mRefreshAllCommand == null) 
       mRefreshAllCommand = new DelegateCommand(new Action(mRefreshAllCommandExecuted), new Func<bool>(mRefreshAllCommandCanExecute)); 

      return mRefreshAllCommand; 
     } 
     set 
     { 
      mRefreshAllCommand = value; 
     } 
    } 

    public bool mRefreshAllCommandCanExecute() 
    { 
     return true; 
    } 

    public void mRefreshAllCommandExecuted() 
    {        
     VoltageChannelList = bavaria1Channels;   
     // Call OnChildAdd Method here      
    } 

XAML瀏覽:

<Label Grid.Column="0" Content="{Binding ChannelName}" Height="25" Width="120" Name="VoltageLabel" /> 

視圖模型VoltageBoard通道類別:

public class VoltageBoardChannel 
{ 
    public string ChannelName { get; set; } 
    public bool IsAvailable { get; set; } 
}  

按鈕點擊調用的方法。點擊後,我想調用OnChildAdd()方法,以便將這些LIST中的項目列表添加到我的堆棧面板。可能嗎???

回答

1

在XAML:

<Button Command={Binding AddChildCommand} /> 

在您的視圖模型:

public ICommand AddChildCommand { get; private set; } 

然後在視圖模型的構造,設置AddChildCommand到實現ICommand的對象。然後該命令可以調用您的OnChildAdd()方法。 (here的規格)

一個更好的方法來做到這一點,雖然只是將stackpanel的孩子綁定到你的視圖模型中的一個屬性,然後你添加你的VoltageChannelView到那個。

+0

當然,您可以根據需要調用任意多種方法。 – Pyritie

+0

@StonedJesus試試這個綁定你的StackPanel的子項:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f52cbe65-0cc0-4b39-b346-7f70e6830aae/ – Pyritie

+0

我試着做第一種方法你提到但看起來像我失去了一些東西。我已經更新了代碼。你能檢查並告訴我什麼是錯的嗎? :) – StonedJesus