2012-10-30 69 views
0

我正在處理在我的應用程序中動態生成的一組單選按鈕。談到訪問班級成員時,我似乎遇到了一個問題。這是我做了什麼至今:未能在WPF中訪問其他ViewModel類的成員

XAML:

<GroupBox Header="Daughter Cards" Height="Auto" HorizontalAlignment="Stretch" Margin="20,5,20,20" Name="groupBox2" VerticalAlignment="Stretch" Width="Auto"> 
      <Grid>     
       <Grid Grid.Column="0"> 
        <ItemsControl ItemsSource="{Binding SlotChildren}"> 
         <ItemsControl.ItemsPanel> 
          <ItemsPanelTemplate> 
           <UniformGrid Columns="3" Rows="8" /> 
          </ItemsPanelTemplate> 
         </ItemsControl.ItemsPanel> 

         <ItemsControl.ItemTemplate> 
          <DataTemplate> 
           <RadioButton Content="{Binding SlotButtons}" Margin="0,10,0,0" IsChecked="{Binding IsChecked}" GroupName="SlotGroup" Height="15" Width="80" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
          </DataTemplate> 
         </ItemsControl.ItemTemplate> 
        </ItemsControl> 
       </Grid>     

       <Grid Grid.Column="1">             
         <ComboBox Grid.Row="0" ItemsSource="{Binding DaughterBoardBoxList}" SelectedItem="{Binding SelectedDaughterBoardBoxList, Mode=TwoWay}" SelectedIndex="0" Height="23" HorizontalAlignment="Center" Margin="0" Name="comboBox5" VerticalAlignment="Center" Width="158" /> 
         <ComboBox Grid.Row="1" ItemsSource="{Binding DaughterVersionBoxList}" SelectedItem="{Binding SelectedDaughterVersionBoxList, Mode=TwoWay}" SelectedIndex="0" Height="23" HorizontalAlignment="Center" Margin="0" Name="comboBox6" VerticalAlignment="Center" Width="158" /> 
         <ComboBox Grid.Row="2" ItemsSource="{Binding DaughterSerialBoxList}" SelectedItem="{Binding SelectedDaughterSerialBoxList, Mode=TwoWay}" SelectedIndex="0" Height="23" HorizontalAlignment="Center" Margin="0" Name="comboBox7" VerticalAlignment="Center" Width="158" /> 

         <Button Grid.Row="1" Command="{Binding GetStringCommand}" Content="Get String" Height="23" HorizontalAlignment="Center" Margin="0" Name="RefreshDaughterCards" VerticalAlignment="Center" Width="90" /> 
         <Button Grid.Row="2" Command="{Binding SetStringCommand}" Content="Set String" Height="23" HorizontalAlignment="Center" Margin="0" Name="WriteEEPROMDCBtn" VerticalAlignment="Center" Width="90" /> 
         <Label Content="{Binding DaughterStatus}" Height="25" HorizontalAlignment="Center" Margin="0" Name="DaughterCardLabel" VerticalAlignment="Center" Width="170" /> 
        </Grid>          
       </Grid> 
     </GroupBox> 

EEPROMViewModel類:

public ObservableCollection<EEPROMSlotViewModel> SlotChildren { get; set; } 

public EEPROMViewModel() 
{ 
     SlotChildren = new ObservableCollection<EEPROMSlotViewModel>(); 
     SlotChildren.Add(new EEPROMSlotViewModel() { ParentVM = this, SlotButtons = "0 : None", ID = 0 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { ParentVM = this, SlotButtons = "1 : None", ID = 1 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { ParentVM = this, SlotButtons = "2 : None", ID = 2 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { ParentVM = this, SlotButtons = "3 : None", ID = 3 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { ParentVM = this, SlotButtons = "4 : None", ID = 4 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { ParentVM = this, SlotButtons = "5 : None", ID = 5 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { ParentVM = this, SlotButtons = "6 : None", ID = 6 }); 
} 

產生7個單選按鈕與ID與每個。

EEPROMSlotViewModel類別:

private string _SlotButtons; 
    public string SlotButtons 
    { 
     get; set; 
    } 

    private EEPROMViewModel _parentVm; 
    public EEPROMViewModel ParentVM 
    { 
     get; set; 
    } 

    private int _ID; 
    public int ID 
    { 
     get; set; 
    } 

    private bool _isChecked; 
    public bool IsChecked 
    { 
     get; set; 
    } 

從而每當我選擇單選按鈕並單擊SETSTRING按鈕,下面的代碼被執行:

EEPROMSlotViewModel mSlotVM = new EEPROMSlotViewModel(); 

     string label; 
     if (mSlotVM.ID == 0) //Accessing the 1st radiobutton clicked 
     { 
      label = string.Empty; 
      mSlotVM.getShortName(0, label); 
      if (label == string.Empty) 
      { 
       label = "None"; 
      } 

      mSlotVM.SlotButtons = Convert.ToString(0 + ":" + label); // Setting CONTENT of radiobutton selected 
     } 

比方說我點擊第一單選按鈕,ID應該它叫getShortName()方法,它執行以下操作:

ParentVM.SelectedDaughterBoardBoxList = ParentVM.DaughterBoardBoxList[0]; 
ParentVM.SelectedDaughterVersionBoxList = ParentVM.DaughterVersionBoxList[0]; 
ParentVM.SelectedDaughterSerialBoxList = ParentVM.DaughterSerialBoxList[0]; 
shortlabel = "Hello"; 

我在這裏面臨幾個問題:

  • mSlotVM訪問其他類成員/功能正確的方式?
  • 一旦控制進入getShortname(),它拋出以下內容:Object reference not set to an instance of an object.ParentVM.DaughterBoardBoxList[0];
  • 即使我評論getShortName()中的前3個語句,當getShortName被調用並且控件返回時,值 label是「」,我應該是「hello」。

我覺得mSlotVm這是異常背後的原因。請幫助:)

回答

1
  1. 不,你只是創建一個EEPROMSlotViewModel類的新實例,你不能訪問任何RadioButtons ViewModels。
  2. 如果您能告訴我們您的EEPROMViewModel-class,這將有所幫助。我認爲問題是,你的ParentVM.列表爲空。
  3. 達到你想要什麼,你getShortname() - 方法有看起來像:

    public void getShortname(int i, ref string shortlabel) 
    { 
        ParentVM.SelectedDaughterBoardBoxList = ParentVM.DaughterBoardBoxList[0]; 
        ParentVM.SelectedDaughterVersionBoxList = ParentVM.DaughterVersionBoxList[0]; 
        ParentVM.SelectedDaughterSerialBoxList = ParentVM.DaughterSerialBoxList[0]; 
        shortlabel = "Hello"; 
    } 
    

編輯: 通過EEPROMSlotViewModel mSlotVM = new EEPROMSlotViewModel();您創建的EEPROMSlotViewModel一個新的實例,但你不會得到檢查單選按鈕視圖模型。所以在那個時候你打電話給mSlotVM.getShortName(0, label); mSlotVM沒有ParentVM,這就是引發異常的原因。你可以做的是通過你的SlotChildren-List,並採取其IsChecked屬性爲true的EEPROMSlotViewModel。

實施例:

EEPROMSlotViewModel checkedVM;  
string label = string.Empty; 
foreach (EEPROMSlotViewModel vm in SlotChildren) 
{ 
    if (vm.IsChecked) 
    { 
     checkedVM = vm; 
    } 
    else 
    { 
     vm.SlotButtons = vm.ID + " : NONE" 
    } 

} 
checkedVM.getShortName(0, ref label); 
if (label == string.Empty) 
{ 
    label = "None"; 
} 
checkedVM.SlotButtons = Convert.ToString(0 + ":" + label); // Setting CONTENT of radiobutton selected 
+0

感謝。那麼我有幾件事要問:1。我已經創建了實例'mSlotVm',它似乎不起作用。那麼你在談論哪個新實例? 2.我爲您更新了'EEPROMViewModel'類。我確定這裏有錯誤 –

+0

1.如果你點擊「Set String」按鈕,你想改變選中的單選按鈕的字符串,對吧?看我的編輯。 –

+0

非常感謝。我能夠理解你想說的話。如果你能用代碼證明它,我將不勝感激。 :) –