這是我繼承的代碼庫,而System.Windows.Controls命名空間中的TreeView顯然不支持CheckBoxes屬性。我試圖切換到System.Windows.Forms,但是必須更改太多的代碼。是否可以添加複選框到System.Windows.Control.TreeView,如果是這樣,怎麼樣?謝謝。在C#,.Net 3.5中,如何將複選框添加到TreeView?
0
A
回答
0
您可以通過影響HierarchicalDataTemplate樹視圖的ItemTemplate屬性。只需定義模板是這樣的:
<HierarchicalDataTemplate x:Key="CheckBoxItemTemplate" ItemsSource="{Binding YourNodeInnerElementsCollectionHere, Mode=OneTime}">
<StackPanel Orientation="Horizontal">
<CheckBox Focusable="False" IsChecked="{Binding YourBooleanPropertyForCheck}" VerticalAlignment="Center"/>
<ContentPresenter Content="{Binding YourStringPropertyForText, Mode=OneTime}"/>
</StackPanel>
</HierarchicalDataTemplate>
然後設置你的樹狀如下:
<TreeView (...) ItemTemplate="{StaticResource CheckBoxItemTemplate}" (...) />
UPDATE
這裏有一個模板樹視圖的一個非常簡單的例子。
項目:WpfApplication4(根名稱空間)
文件1:MyItemCollection.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WpfApplication4
{
public class MyItemCollection : System.Collections.ObjectModel.ObservableCollection<MyItem>
{
public MyItemCollection()
{
Add(new MyItem() { MyText = "test", MyIsChecked = true });
Add(new MyItem() { MyText = "test2", MyIsChecked = false });
Add(new MyItem() { MyText = "test3", MyIsChecked = false });
Add(new MyItem() { MyText = "test4", MyIsChecked = true });
this[0].MyInnerCollection.Add(new MyItem() { MyText = "innertest", MyIsChecked = true });
}
}
public class MyItem
{
public MyItem()
{
_MyInnerCollection = new System.Collections.ObjectModel.ObservableCollection<MyItem>();
}
// Fields...
private System.Collections.ObjectModel.ObservableCollection<MyItem> _MyInnerCollection;
private bool _MyIsChecked;
private string _MyText;
public string MyText
{
get { return _MyText; }
set
{
_MyText = value;
}
}
public bool MyIsChecked
{
get { return _MyIsChecked; }
set
{
_MyIsChecked = value;
}
}
public System.Collections.ObjectModel.ObservableCollection<MyItem> MyInnerCollection
{
get
{
return _MyInnerCollection;
}
}
}
}
文件2:MainWindow.xaml
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication4"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Resources>
<local:MyItemCollection x:Key="ColObj"></local:MyItemCollection>
<HierarchicalDataTemplate x:Key="CheckBoxItemTemplate" ItemsSource="{Binding MyInnerCollection}">
<StackPanel Orientation="Horizontal">
<CheckBox Focusable="False" IsChecked="{Binding MyIsChecked}" VerticalAlignment="Center"/>
<ContentPresenter Content="{Binding MyText}"/>
</StackPanel>
</HierarchicalDataTemplate>
</Grid.Resources>
<TreeView ItemTemplate="{StaticResource CheckBoxItemTemplate}" ItemsSource="{StaticResource ColObj}">
</TreeView>
</Grid>
</Window>
再次,這是爲了示範的目的。 :)它應該給你關於treeview項目模板的主要想法。
相關問題
- 1. 將複選框添加到VB.NET WPF 3.5 TreeView
- 2. 如何將複選框添加到Tcl/Tk中的treeview?
- 3. 在Treeview中添加複選框
- 4. 如何在childnodes treeview中添加複選框?
- 5. 在C#中的TREEVIEW中添加複選框
- 6. 擴展.NET TreeView複選框
- 7. 如何將複選框添加到UITableViewCell?
- 8. 如何將複選框添加到c#中的圖表系列?
- 9. 如何將複選框添加到添加的列表中?
- 10. 如何在c#中使用XmlDocument將屬性添加到xml中.net CF 3.5
- 11. 如何將複選框添加到WPF中的datepicker中
- 12. treeview複選框
- 13. 將複選框列添加到GridControlEx中
- 14. 將複選框列添加到DataGridView中
- 15. 如何將System.Collections.Generic 3.5添加到Asp.net 2.0?
- 16. 如何將複選框添加到表中
- 17. 帶有複選框的.NET TreeView控件
- 18. 如何使用.net將複選框添加到Excel電子表格中?
- 19. 如何在c#中的組合框內添加複選框
- 20. 將圖像添加到winforms複選框
- 21. TreeView複選框在C#.Net中作爲單獨和單獨的複選框工作.Net
- 22. 如何在Ext.tree.Panel中添加複選框?
- 23. 如何在listview中添加複選框?
- 24. 如何在DataTable中添加複選框?
- 25. 如何在mvc3中添加複選框?
- 26. 如何在slickgrid中添加複選框?
- 27. 添加取消選中TreeView的所有複選框
- 28. 如何將複選框添加到swift中的uicollectionview中
- 29. 如何在c#中獲取treeview控件複選框名稱
- 30. 如何將複選框中的值添加到數組中
你在說WPF嗎? – DonBoitnott
是的,我正在談論WPF – scrayne
編輯您的標籤以包含'WPF'。 – DonBoitnott