Treeview绑定到查看模型不起作用

时间:2015-01-01 08:18:50

标签: wpf xaml mvvm treeview

我查看了Treeview绑定的所有示例,   但我仍然无法从视图模型将其绑定到Treeview。 请告诉我这里缺少的东西。

//这是我的类,需要在UI中生成树视图。

public class Station
{        
   public string Name { get; set; } // Header of Treeview

    public ObservableCollection<AnalogToDigital> Adc { get; set; }
    public ObservableCollection<DigitalToAnalog> Dac { get; set; }
    public ObservableCollection<AuxilaryInputs> AuxInput { get; set; }
}

//ADC channel Details
public class AnalogToDigital
{
    public string InternalName { get; set; }               
}

//DAC channel details
public class DigitalToAnalog
{
   public string InternalName { get; set; }

}
//Input output pin details
public class AuxilaryInputs
{       
    public string InternalName { get; set; }            
}

// View.xaml:treeview xaml代码:

<TreeView Name="StationTree" HorizontalAlignment="Left" Height="326.04" Margin="10,10,0,0"    
 VerticalAlignment="Top" Width="188" ItemsSource="{Binding StationTree}">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding StationTree}">
                    <TreeViewItem Header="{Binding Name}"/>                        
                    <HierarchicalDataTemplate.ItemTemplate>
                        <HierarchicalDataTemplate ItemsSource="{Binding StationTree.Adc}">
                            <TreeViewItem Header="{Binding StationTree.Adc.InternalName}"/>
                        </HierarchicalDataTemplate>
                    </HierarchicalDataTemplate.ItemTemplate>

                </HierarchicalDataTemplate>                            
            </TreeView.ItemTemplate>
        </TreeView>

// ViewModel:我在这里使用mvvmlight

public class MainViewModel : ViewModelBase
{
    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel()
    {
        // Move it to business layer
        StationXmlOperation stRead = new StationXmlOperation();

        StationTree = (Station)stRead.Read(@"C:\Station.xml");           

    }

    private string _name;
    private Station _stationTree;

 public Station StationTree
 {
     get { return _stationTree; }

     set
     {
         _stationTree = value;
         RaisePropertyChanged(() => StationTree);
     }
 }
}

//代码隐藏文件:View.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var viewModel = new MainViewModel();
        this.DataContext = viewModel;

        //this.StationTree.DataContext = viewModel.StationTree;
        this.StationTree.Items.Add(viewModel.StationTree);
    }
}

// XML文件:Station.xml

<StationDetails>
<Station id="1" Name="FirstStation">
<ADCs>
<ADC id="1" InternalName="ADC1" ></ADC>
<ADC id="1" InternalName="ADC2" ></ADC>
</ADCs>
<DACs>
<DAC id="1" InternalName="DAC1"  ></DAC>
<DAC id="1" InternalName="DAC2"  ></DAC>
</DACs>
<IOs>
<IO id="1" InternalName="IO1" ></IO>
</IOs>
</Station>
</StationDetails>

//预期来自上述对象的树视图层次结构是

Name
->ADC
-->InternalName 
->DAC
-->InternalName
->AuxInput
-->InternalName

目前代码仅显示&#34; FirstStation&#34;。

请帮助我,我第一次使用treeview。

2 个答案:

答案 0 :(得分:0)

我认为你的方法中缺少xpath。用xpath创建HDT

<HierarchicalDataTemplate x:Key="NodeTemplate">
        <HierarchicalDataTemplate.ItemsSource>
            <Binding XPath="child::*" />
        </HierarchicalDataTemplate.ItemsSource>           
            <TextBlock Text="{Binding Path=Name}" />
    </HierarchicalDataTemplate>

请关注thisthisthisthis,了解使用XML数据的Treeview

希望这有帮助!!!

答案 1 :(得分:0)

您将不得不更改对象模型以使其与TreeView一起使用。因此,不可能按照您想要的方式使用TreeView,它只是不兼容(您只能将HierarchicalDataTemplate绑定到子项的一个集合,您有三个集合,并且您希望每个集合都是它们的节点)自己的)。

public class Station
{        
    public string Name { get; set; } // Header of Treeview
    public ObservableCollection<DetailGroup> DetailGroups { get; set; }
}

public class DetailGroup
{
    public string GroupName { get; set;}
    public ObservableCollection<Detail> Details { get; set; }
}

public class Detail
{
    public string InternalName { get; set; }               
}

public class ADC : Detail
{
    //...
}

public class DAC : Detail
{
    //...
}

public class AuxInput : Detail
{
    //...
}

对象模型在XML中基本上看起来像这样(这只是一个示例,您可以看到对象模型,您可以保持原始XML相同,您只需要更改StationXmlOperation.Read方法) :

<StationDetails>
  <Station Name="FirstStation">
    <DetailGroup GroupName="ADCs">
      <ADC InternalName="ADC1"/>
      <ADC InternalName="ADC2"/>
    </DetailGroup>
    <DetailGroup GroupName="DACs">
      <DAC InternalName="DAC1"/>
      <DAC InternalName="DAC2"/>
    </DetailGroup>
    <DetailGroup GroupName="AuxInputs">
      <AuxInput InternalName="AuxInput1"/>
      <AuxInput InternalName="AuxInput2"/>
    </DetailGroup>
  </Station>
</StationDetails>

就像我在评论中提到的那样,你需要将TreeView的ItemsSource绑定到一个集合而不是一个对象(如果你只有一个对象,只需将它包装在一个集合中):

public class MainViewModel : ViewModelBase
{
    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel()
    {
        // Move it to business layer
        StationXmlOperation stRead = new StationXmlOperation();
        _stationTree = (Station)stRead.Read(@"C:\Station.xml");           
    }

    private string _name;
    private Station _stationTree;

    public IEnumerable<Station> StationTree
    {
        get { yield return _stationTree; }
    }
}

XAML:

<TreeView ItemsSource="{Binding StationTree}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding DetailGroups}">
            <TextBlock Text="{Binding Name}"/>                        
            <HierarchicalDataTemplate.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Details}">
                    <TextBlock Text="{Binding GroupName}"/>                  
                    <HierarchicalDataTemplate.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding InternalName}"/>
                        </DataTemplate>
                    </HierarchicalDataTemplate.ItemTemplate>
                </HierarchicalDataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>                            
    </TreeView.ItemTemplate>
</TreeView>