我正在尝试为我正在处理的程序找出ViewModel。我以前没有这样做过。以下是Switch
对象的部分类定义,它需要ViewModel
和我到目前为止的XAML。我知道XAML需要更多。
现在我正在进行代码隐藏,以便在我打开它之前用SwitchBrowser
窗口填充数据。我知道将ObservableCollection
绑定到XAML会更容易。
我可以在CiscoSwitch
中存储ObservableCollection
对象并直接绑定到Switch
对象上的属性吗?
如果是这样,我是否可以将{XAML中的ItemsSource
上的ModulesTreeview
设置为ModulesList
上的CiscoSwitch
?现在我在代码隐藏中使用foreach来填充ModulesTreeView
。
或者,我会为ObservableCollection
创建SwitchModules
,然后为VSANs
创建另一个,依此类推吗?
public partial class CiscoSwitch
{
#region baseswitchclassproperties
private string _SwitchName = String.Empty;
public string switchName{ get{return _SwitchName;} set{_SwitchName=value;} } private string _SWVersion = String.Empty;
public string swVersion{ get{return _SWVersion;} set{_SWVersion=value;} }
private string _SwitchModel = String.Empty;
public string switchModel { get{return _SwitchModel;} set{_SwitchModel=value;} }
private string _SerialNumber = String.Empty;
public string SerialNumber { get { return _SerialNumber; } set { _SerialNumber = value; } }
private string _SwitchWWPN = string.Empty;
public string SwitchWWPN { get { return _SwitchWWPN; } set { _SwitchWWPN = value; } }
public Dictionary<int, SwitchModule> ModuleList = new Dictionary<int, SwitchModule>();
public Dictionary <int, CiscoVSAN> VSANList = new Dictionary<int, CiscoVSAN>();
protected EthernetPort _ManagementPort = new EthernetPort();
public string IPAddress{ set{_ManagementPort.IPAddress=value;} get{return _ManagementPort.IPAddress;} }
public string LastDataCaptureDate = null;
public Dictionary<int, PortChannel> PortChanelList = new Dictionary<int, PortChannel>();
public Dictionary<String, FCIPPort> FCIPPortList = new Dictionary<string, FCIPPort>();
public InterVSANTopology IVRTopology = null; //new InterVSANTopology();
public Dictionary<int, List<CiscoSwitch>> NeighborsList = new Dictionary<int, List<CiscoSwitch>>();
public Dictionary<string, string> DeviceAliases = new Dictionary<string, string>();
public Dictionary<string, FCPort> FlogiDatabase = new Dictionary<string, FCPort>();
#endregion
}
XAML:
<Window.Resources>
<Style TargetType="{x:Type TreeViewItem}" x:Key="ModuleStyle">
<Setter Property="Foreground" Value="Blue"/>
<Setter Property="FontSize" Value="12"/>
</Style>
<Style TargetType="{x:Type TreeViewItem}" x:Key="RedModuleStyle" BasedOn="{StaticResource ModuleStyle}">
<Setter Property="Foreground" Value="Red"/>
</Style>
</Window.Resources>
<Grid Margin="0,0,-211.4,-168">
<StackPanel HorizontalAlignment="Stretch" Name="StackPanel1" VerticalAlignment="Stretch" Width="Auto" Margin="0,0,188.6,114">
<StackPanel.Resources>
<Style TargetType="{x:Type Label}" x:Key="LabelStyle">
<Setter Property="Foreground" Value="Blue"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="FontWeight" Value="Bold"/>
</Style>
</StackPanel.Resources>
<Label Content="Switch Name:" Name="Label1" Height="25" HorizontalAlignment="Left"/>
<Label Content="Software Version:" Name="Label2" HorizontalAlignment="Left" />
<Label Content="Model Number:" Name="Label3" HorizontalAlignment="left"/>
<Label Content="IP Address:" Name="Label4" HorizontalAlignment="left"></Label>
<Label Content="Serial Number:" Name="Label5" HorizontalAlignment="Left"></Label>
<Label Content="Show Tech Taken:" Name="Label6" HorizontalAlignment="left"/>
</StackPanel>
<StackPanel Margin="105,0,15.6,114">
<StackPanel.Resources>
<Style TargetType="{x:Type Label}" x:Key="LabelStyle">
<Setter Property="FontSize" Value="12"/>
<Setter Property="FontWeight" Value="Bold"/>
</Style>
</StackPanel.Resources>
<Label Content="Name" Name="SwitchNameLabel" HorizontalAlignment="left"/>
<Label Content="Version" Name="VersionLabel" HorizontalAlignment="left"/>
<Label Content="Model" Name="ModelNumberLabel" HorizontalAlignment="Left"/>
<Label Content="IP" Name="IPAddressLabel" HorizontalAlignment="Left"/>
<Label Content="Serial" Name="SerialLabel" HorizontalAlignment="Left"/>
<Label Content="ST" Name="ShowTechLabel" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel HorizontalAlignment="Left" Height="Auto" Margin="0,156,0,0" VerticalAlignment="Top" Width="256" >
<TreeView Name="ModulesTreeView" Height="auto" Background="GhostWhite" BorderThickness="0"/>
</StackPanel>
</Grid>
答案 0 :(得分:2)
关于创建单个ObservableCollection<CiscoSwitch>
的第一个想法是正确的方法。
这是一个更简化的示例,您可以根据自己的特定需求进行扩展。
public class Person
{
public string Name { get; set; }
public string Address { get; set; }
}
假设我们的ViewModel中有一个名为ObservableCollection<Person>
的{{1}}。
People
这是一个过于简单的示例,但我认为您应该能够将其扩展到您的需求。希望这有帮助!