[英]Failing To Access Members of Other ViewModel Class in WPF
我正在研究在我的應用程序中動態生成的單選按鈕集。 訪問班級成員時,我似乎面臨一個問題。 這是我到目前為止所做的:
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
按鈕並單擊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應該為0。它調用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];
。 label
值為“”,我也應該為“ hello”。 我覺得這是mSlotVm
異常的原因。 請幫忙 :)
EEPROMSlotViewModel
的新實例,就不會訪問任何RadioButtons ViewModels。 EEPROMViewModel
,將很有幫助。 我認為問題是您的ParentVM.
-列表為空。 要實現所需的功能,您的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
的新實例,但是您沒有得到選中的RadioButtons ViewModel。 因此,此時您調用了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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.