[英]Failing To Access Members of Other ViewModel Class in WPF
I am working on set of radiobuttons which are dynamicaly generated in my app. 我正在研究在我的应用程序中动态生成的单选按钮集。 I seem to be facing a issue when it comes to access the class members. 访问班级成员时,我似乎面临一个问题。 Here is what I have done till now: 这是我到目前为止所做的:
XAML: 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 Class: 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 });
}
generates 7 radiobuttons with ID related to each. 生成7个与每个ID相关的单选按钮。
EEPROMSlotViewModel Class: 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;
}
thus whenever I select a radiobutton and click SETSTRING
button, the following code gets executed: 因此,每当我选择一个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
}
Lets say I clicked 1st radio button, ID is supposed to be 0. It calls getShortName()
method which does the following: 可以说我单击了第一个单选按钮,ID应该为0。它调用getShortName()
方法,该方法执行以下操作:
ParentVM.SelectedDaughterBoardBoxList = ParentVM.DaughterBoardBoxList[0];
ParentVM.SelectedDaughterVersionBoxList = ParentVM.DaughterVersionBoxList[0];
ParentVM.SelectedDaughterSerialBoxList = ParentVM.DaughterSerialBoxList[0];
shortlabel = "Hello";
I am facing few issues here: 我在这里面临几个问题:
mSlotVM
right way to access other class members/function?? mSlotVM
是访问其他类成员/函数的正确方法吗? getShortname()
, it throws the excetion as follows: Object reference not set to an instance of an object.
一旦控件进入getShortname()
,它将引发如下异常: Object reference not set to an instance of an object.
at ParentVM.DaughterBoardBoxList[0];
在ParentVM.DaughterBoardBoxList[0];
. 。 label
is "", i should be "hello". 即使我注释了getShortName()中的前3条语句,当getShortName被调用并且控件返回时, label
值为“”,我也应该为“ hello”。 I feel mSlotVm
this is the reason behind the exception. 我觉得这是mSlotVm
异常的原因。 Please help :) 请帮忙 :)
EEPROMSlotViewModel
-Class, youre not accessing any of the RadioButtons ViewModels. 不,您只需创建EEPROMSlotViewModel
的新实例,就不会访问任何RadioButtons ViewModels。 EEPROMViewModel
-Class. 如果您可以向我们展示您的EEPROMViewModel
,将很有帮助。 I think the problem is, that your ParentVM.
我认为问题是您的ParentVM.
-Lists are null. -列表为空。 To achieve what you want, your getShortname()
-method has to look like: 要实现所需的功能,您的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"; }
EDIT: Through EEPROMSlotViewModel mSlotVM = new EEPROMSlotViewModel();
编辑:通过EEPROMSlotViewModel mSlotVM = new EEPROMSlotViewModel();
you create a new instance of EEPROMSlotViewModel
, but youre not getting the checked RadioButtons ViewModel. 您创建了一个EEPROMSlotViewModel
的新实例,但是您没有得到选中的RadioButtons ViewModel。 So at the point were you call mSlotVM.getShortName(0, label);
因此,此时您调用了mSlotVM.getShortName(0, label);
mSlotVM has no ParentVM, thats what raises the exception. mSlotVM没有ParentVM,这就是引发异常的原因。 What you could do is go through your SlotChildren-List and take the EEPROMSlotViewModel whose IsChecked-property is true. 您可以做的是浏览SlotChildren-List并获取IsChecked属性为true的EEPROMSlotViewModel。
Example: 例:
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.