2014-01-23 65 views
0

我需要在我的WPF項目中創建動態創建節點的TreeView。 TreeView節點有一個條件。所有葉子必須包含Radiobuttons,其工作邏輯與RadioButton組相同。TreeView with RadioButtons

我有這樣的XAML代碼,描述樹形數據模板:

<HierarchicalDataTemplate x:Key="sko_ver_hdt"> 
     <RadioButton Margin="0,0,10,0" Content="{Binding Version}"/> 
    </HierarchicalDataTemplate> 

    <HierarchicalDataTemplate x:Key="sko_hdt" 
           ItemTemplate="{StaticResource sko_ver_hdt}" 
           ItemsSource="{Binding Children}"> 
      <TextBlock Text="{Binding Name}"/> 
    </HierarchicalDataTemplate> 

我試着使用CollectionViewSource到TreeView控件的節點組這樣的:

<CollectionViewSource x:Key="cvs" Source="{Binding SKOs}"> 
     <CollectionViewSource.GroupDescriptions> 
      <PropertyGroupDescription PropertyName="Version"/> 
     </CollectionViewSource.GroupDescriptions> 
    </CollectionViewSource> 

但沒有任何反應。

我有描述樹狀數據模型:

internal class SKOVM 
{ 
    public SKOVM(DBCommunication.SKO sko) 
    { 
     Name = sko.Name; 
     Version = sko.VersionCode; 
     Id = sko.Id; 
     Children = new ObservableCollection<SKOVM>(sko.SKO1.Select(x => new SKOVM(x))); 
     sko.SKOSystemInformationReference.Load(); 
     if (sko.SKOSystemInformation != null) 
      Version = String.Format("{0}/{1}", Version, sko.SKOSystemInformation.Designer); 
    } 

    public long Id { get; set; } 

    public string Name { get; set; } 
    public string Version { get; set; } 

    public ObservableCollection<SKOVM> Children { get; set; } 
} 

任何想法如何,我可以實現分組單選按鈕的行爲邏輯,我樹狀葉節點?

在此先感謝。

+0

如果它幫助您解決問題,請接受答案,以便將此問題標記爲已回答。 – Sheridan

回答

3

您是否嘗試過使用RadioButton.GroupName property

<HierarchicalDataTemplate x:Key="sko_ver_hdt"> 
    <RadioButton Content="{Binding Version}" GroupName="{Binding GroupName}" /> 
</HierarchicalDataTemplate> 

此屬性獲取或設置指定其名稱單選按鈕控件是互相排斥的。因此,您需要在每個組的子項目具有相同值的子項目類中使用屬​​性。通過這種方式,您可以將數據綁定到GroupName屬性的值,並且每個子項目的RadioButton應該在其各個組中工作。

+0

非常感謝。你真的幫助我。一切都按照我的計劃進行。 – ArhiChief

+0

@SergeyKosivchenko,不客氣,歡迎來到StackOverflow。我可以首先從[幫助中心](http://stackoverflow.com)提請您注意[我應該怎麼做當某人回答我的問題?](http://stackoverflow.com/help/someone-answers) /幫幫我)?其次,如果您想充分利用本網站,建議您花一些時間瀏覽幫助中心的其他頁面。他們充滿了有用的信息。 – Sheridan