标签:val lis 之间 log TreeView handle tty get items
记录一下工作中遇到的问题,以便以后忘记了可以来看.
在工作中遇到一个问题,就是要实现类型如下的界面,没有使用Telerik和Dev库。本来最开始是想使用Datagrid,但不知道怎么实现treeview,后来遍使用treeview。
前端xaml代码:
<TreeView x:Name="mytreeview">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Nodes}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding level,Converter={StaticResource LevelToWidthConverter}}"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Content}"/>
<CheckBox IsChecked="{Binding IsChecked}"
Grid.Column="1"/>
</Grid>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
其中下面的用于控制文字和Checkbox之间的距离。
<ColumnDefinition Width="{Binding level,Converter={StaticResource LevelToWidthConverter}}"/>
后台代码:
private void InitDatabase()
{
List<MyTreeview> lists = new List<MyTreeview>();
MyTreeview node = new MyTreeview("分类", false, 0);
MyTreeview node1 = new MyTreeview("水果", false,1);
MyTreeview node2 = new MyTreeview("颜色", false, 1);
MyTreeview node3 = new MyTreeview("蔬菜", false, 1);
MyTreeview node4 = new MyTreeview("苹果", false, 2);
MyTreeview node5 = new MyTreeview("红色", false, 2);
MyTreeview node6 = new MyTreeview("绿色", false, 2);
MyTreeview node7 = new MyTreeview("西红柿", false, 2);
node1.Nodes.Add(node4);
node2.Nodes.Add(node5);
node2.Nodes.Add(node6);
node3.Nodes.Add(node7);
node.Nodes.Add(node1);
node.Nodes.Add(node2);
node.Nodes.Add(node3);
lists.Add(node);
mytreeview.ItemsSource = lists;
}
}
public class MyTreeview:INotifyPropertyChanged
{
public MyTreeview(string Content,bool IsChecked,int level)
{
this.Content = Content;
this.IsChecked = IsChecked;
this.level = level;
Nodes = new List<MyTreeview>();
}
public string Content { get; set; }
public bool IsChecked { get; set; }
public int level { get; set; }
public List<MyTreeview> Nodes { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
宽度转换:
public class LevelToWidthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || (int)value == 0)
return 200;
return 200 - (int)value * 20;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
标签:val lis 之间 log TreeView handle tty get items
原文地址:http://www.cnblogs.com/liyijin/p/7147889.html