我正在研究在我的应用程序中动态生成的一组单选按钮。在访问班级成员时,我似乎面临一个问题。这是我到目前为止所做的:
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 应该为 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.
atParentVM.DaughterBoardBoxList[0];
。 - 即使我在 getShortName() 中注释前 3 个语句,当调用 getShortName 并且一旦控件返回时,值为
label
“”,我应该是“你好”。
我觉得mSlotVm
这是异常背后的原因。请帮忙 :)