C# 第一次构建ViewModel

C# 第一次构建ViewModel,c#,xaml,mvvm,C#,Xaml,Mvvm,我正试图为我正在开发的程序找出一个视图模型。我以前没有这样做过。下面是开关对象的部分类定义,该对象需要视图模型和我到目前为止使用的XAML。我知道XAML需要更多 现在,我正在进行代码隐藏,在打开SwitchBrowser窗口之前,用数据填充该窗口。我知道将可观测集合绑定到XAML会更容易 我是否可以将CiscoSwitch对象存储在observateCollection中,并直接绑定到开关对象上的属性 如果是这样,我是否可以将XAML中ModulesTreeview上的ItemsSource设

我正试图为我正在开发的程序找出一个视图模型。我以前没有这样做过。下面是
开关
对象的部分类定义,该对象需要
视图模型
和我到目前为止使用的XAML。我知道XAML需要更多

现在,我正在进行代码隐藏,在打开
SwitchBrowser
窗口之前,用数据填充该窗口。我知道将
可观测集合
绑定到XAML会更容易

我是否可以将
CiscoSwitch
对象存储在
observateCollection
中,并直接绑定到
开关
对象上的属性

如果是这样,我是否可以将XAML中
ModulesTreeview
上的
ItemsSource
设置为
CiscoSwitch
上的
ModulesList
?现在我正在使用代码隐藏中的foreach来填充
ModulesTreeView

或者,我会创建一个
SwitchModules
ObservableCollection
,然后为
vsan
创建另一个,依此类推

 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
}
公共部分类CiscoSwitch
{
#区域baseswitchclassproperties
私有字符串_SwitchName=string.Empty;
公共字符串switchName{get{return{u switchName;}set{{u switchName=value;}}私有字符串{u SWVersion=string.Empty;
公共字符串swVersion{get{return}u swVersion;}set{{u swVersion=value;}}
私有字符串_SwitchModel=string.Empty;
公共字符串switchModel{get{return}switchModel;}set{{switchModel=value;}}
私有字符串_SerialNumber=string.Empty;
公共字符串SerialNumber{get{return}U SerialNumber;}set{{U SerialNumber=value;}}
私有字符串\u SwitchWWPN=string.Empty;
公共字符串SwitchWWPN{get{return}switchwpn;}set{{switchwpn=value;}}
公共字典ModuleList=新字典();
公共字典VSANList=新字典();
受保护的以太网端口_ManagementPort=新以太网端口();
公共字符串IPAddress{set{U ManagementPort.IPAddress=value;}get{return{U ManagementPort.IPAddress;}}
公共字符串LastDataCaptureDate=null;
公共字典PortChanelList=新字典();
公共字典FCIPPortList=新字典();
public InterVSANTopology IVRTopology=null;//新InterVSANTopology();
公共字典邻居列表=新字典();
公共字典DeviceAliases=新字典();
公共字典FlogiDatabase=新字典();
#端区
}
XAML:



关于创建单个
可观察集合的第一个想法是正确的。
下面是一个更简单的示例,您可以扩展到您的特定需求

public class Person
{
    public string Name { get; set; }
    public string Address { get; set; }
}
假设在我们的ViewModel中有一个名为
人的
可观察集合

<ListBox ItemsSource="{Binding Path=People}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=Name}" />
                <TextBlock Text="{Binding Path=Address}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

这是一个过于简单的例子,但我认为您应该能够将其扩展到您的需要。希望这有帮助

<ListBox ItemsSource="{Binding Path=People}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=Name}" />
                <TextBlock Text="{Binding Path=Address}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>