2015-01-01 122 views
1

我查看了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:樹形視圖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> 

//視圖模型:我使用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 

目前代碼僅顯示 「FirstStation」。

請幫助我,我第一次使用treeview。

+0

第一個問題:財產'StationTree在您的視圖模型是不是集合,它僅僅是一個對象:「站」。你正試圖將它綁定到一個ItemsSource,所以它需要是一個集合。 Next:HierarchicalDataTemplates中的ItemsSource綁定是錯誤的。DataContext是構成TreeView綁定的原始集合的各個對象,而不是ViewModel本身。 –

+0

如何將下面的類綁定到Treeview。 'public class station { public string Name {get;組; } // Treeview的標題 public ObservableCollection Adc {get;組; } public ObservableCollection Dac {get;組; } public ObservableCollection AuxInput {get;組; } }' – Umesh

回答

0

我覺得XPath是在approach.Create一個HDT缺少使用XPath

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

請按照thisthisthisthis瞭解樹狀視圖與XML數據

希望這有助於! !

+0

嗨,我沒有綁定到視圖直接xml。我想將它綁定到Viewmodel的Station類對象來查看。上面的例子解釋了直接綁定到xml。請告訴我。 – Umesh

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>