1

我正在尝试为我正在处理的程序找出 ViewModel。我以前没有这样做过。Switch这是需要的对象的部分类定义ViewModel和我目前拥有的 XAML。我知道 XAML 需要更多。

现在我正在做代码隐藏,在SwitchBrowser打开窗口之前用数据填充窗口。我知道将 an 绑定ObservableCollection到 XAML 会更容易。

我可以将对象存储在一个CiscoSwitch对象中ObservableCollection并直接绑定到Switch对象的属性吗?

如果是这样,我可以执行类似将XAML 中的on 设置为 on 的操作ItemsSource吗?现在我在代码隐藏中使用 foreach 来填充. ModulesTreeviewModulesListCiscoSwitchModulesTreeView

或者,我会创建一个ObservableCollectionof SwitchModules,然后创建另一个 for 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>

4

1 回答 1

2

您对创建单曲的第一个想法ObservableCollection<CiscoSwitch>是正确的方法。这是一个更简化的示例,您可以将其扩展到您的特定需求。

public class Person
{
    public string Name { get; set; }
    public string Address { get; set; }
}

假设在我们的 ViewModel 中有一个ObservableCollection<Person>被调用的People.

<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>

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

于 2012-10-08T07:30:22.780 回答