c#wpf菜鸟提问:给一个c# wpf 目录浏览类型的路径字符串,使静态的treeViewItem节点如何绑定该路径映射的c# wpf 目录浏览树?

& & & 我们经常看到一些软件比如酷狗音乐,在对列表右键进行重命名的时候,当前列表会泛白并且进入可编辑状态,当我们更改完成后就会并进入非编辑状态,这些具体是怎么实现的呢?下面的方法也许会提供一些思路,下面的TreeView节点是通过数据双向绑定的方式,绑定到TextBlock控件和TextBox控件的Text属性上,并且让两者绑定相同的属性,同时使TextBox控件刚好完全覆盖TextBlock控件,&由于TextBlock控件和TextBox控件的区别,TextBlock控件无法实现编辑,所以我在TextBlock控件的上面覆盖了一个TextBox控件,初始状态下我们设置TextBox的Visibility属性为Collapsed当我们点击重命名的时候,我们再设置TextBox的Visibility属性为Visible,这样我们就能够进行节点的重命名,当然当我们命名完成后(该TextBox失去焦点之后)我们再设置TextBox的Visibility属性为Collapsed,这样就完成了重命名的过程,当然我们还有很多重要的工作要做,比如如何获取HierarchicalDataTemplate中的TextBox控件这个是关键,其次TextBlock控件和TextBox控件必须同时绑定到同一属性,这样当属性值发生改变时,就能够更改TextBlock的Text属性值。注意:TextBox的默认绑定方式Mode=TwoWay。前端XAML代码(关键部分)&TreeView.ItemTemplate&&&&&&& &HierarchicalDataTemplate DataType="{x:Type localex:TreeMode}" ItemsSource="{Binding Children}"&&&&&&&&&&&& &CheckBox Tag="{Binding Children}" IsChecked="{Binding IsChecked, Mode=TwoWay}" ToolTip="{Binding ToolTip}"&&&& &&&&&&&&&&&&StackPanel Orientation="Horizontal"&&&&&&&& &&&&&&&&&&&&Image VerticalAlignment="Center" Source="{Binding Icon}"/&&&&&&&& &&&&&&&&&&&&&&&StackPanel Orientation="Vertical"&&&&&&&&&&&& &&&&&&&&&&&TextBlock Text="{Binding Name, Mode=TwoWay}" HorizontalAlignment="Center" Width="Auto"/&&&&&&&&&&&& &&&&&&&&&&&&TextBox x:Name="renametextbox" Text="{Binding Name, Mode=TwoWay}" HorizontalAlignment="Center" Margin="0,-20,0,0"& & & & & & & & & & & & & & &Width="Auto" &Visibility="Collapsed" &LostFocus="renametextbox_LostFous"/&&&&&&&&&&&&&&&&&&&&&&& &/StackPanel&&&& &&&&&&&&&&&&&&/StackPanel&&&& &&&&&&&&&&&CheckBox.ContextMenu&&&&&&&& &&&&&&&&&&&ContextMenu&&&&&&&&&&&& &&&&&&&&&&MenuItem &Name="reNameItem" Header="重命名" Click="ReNameTreeViewItem_Click"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&/MenuItem&&&&&&& &&&&&&&&&&&&/ContextMenu&&&& &&&&&&&&&&&&/CheckBox.ContextMenu&&&&&&&&&&&& &/CheckBox&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &/HierarchicalDataTemplate&& &/TreeView.ItemTemplate&&&后端核心代码:& & & & //下面的部分是在鼠标指针位于此元素(TreeViewItem)上并且按下鼠标右键时发生。& & & & private void TreeViewItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)& & & & {& & & & & & &//此处item定义的是一个类的成员变量,是一个TreeViewItem类型& & & & & & &item = GetParentObjectEx&TreeViewItem&(e.OriginalSource as DependencyObject) as TreeViewI& & & & & & &if (item != null)& & & & & & & {& & & & & & & & &//使当前节点获得焦点& & & & & & & & &item.Focus();& & & & & & & & &//系统不再处理该操作& & & & & & & & &e.Handled =& & & & & & & }& & & & }&&&&&&& //对当前TreeViewItem进行重命名&&&&&&& private void ReNameTreeViewItem_Click(object sender, RoutedEventArgs e)&&&&&&& {& & & & & & &//获取在TreeView.ItemTemplate中定义的TextBox控件& & & & & & &tempTextBox = FindVisualChild&TextBox&(item as DependencyObject);& & & & & & //设置该TextBox的Visibility 属性为Visible& & & & & & &tempTextBox.Visibility = Visibility.V&&&&&&& }& & & & & & &下面的这个函数主要是利用VisualTreeHelper.GetParent()方法获取视觉树上面的各种控件,当我们鼠标点击TreeView节点的时候,我们沿着视觉树VisualTree依次向上查找获取& & & & 相应的控件,在本例中依次查找到的控件为:TextBlock-》StackPanel-》StackPanel-》ContentPresenter-》BulletDecorator-》CheckBox-》ContentPresenter-》Boarder-》Grid-》TreeViewItem,通过每一次的向上查找最终找到我们需要的TreeViewItem对象。& & & &//获取当前TreeView的TreeViewItem& & & public TreeViewItem GetParentObjectEx&TreeViewItem&(DependencyObject obj) where TreeViewItem : FrameworkElement& & & {& & & & DependencyObject parent = VisualTreeHelper.GetParent(obj);& & & & while (parent != null)& & & & &{& & & & & & if (parent is TreeViewItem)& & & & & &{& & & & & & & &return (TreeViewItem)& & & & & &}& & & & & & & parent = VisualTreeHelper.GetParent(parent);& & & & }& & & & && & & }&& & & & 下面的这个函数也是非常重要的,由于我们定义的TextBox控件是在TreeView.ItemTemplate中定义的,所以无法通过this来查找当前的控件,如果无法获取当前的该控件,就无法进行下面的操作,所以这个函数也是非常重要的。和鼠标点击是沿着视觉树向上查找不同,此处我们需要沿着视觉树向下查找,直到找到我们TextBox控件为止,最终返回TextBox控件对象,这个刚好和上面的过程相反,但是这个过程也是非常重要的,具体的使用方式可以参考MSDN上面有更加具体的说明。&&&&&&& //获取ItemTemplate内部的各种控件&&&&&&& private childItem FindVisualChild&childItem&(DependencyObject obj) where childItem : DependencyObject&&&&&&& {&&&&&&&&&&& for (int i = 0; i & VisualTreeHelper.GetChildrenCount(obj); i++)&&&&&&&&&&& {&&&&&&&&&&&&&&& DependencyObject child = VisualTreeHelper.GetChild(obj, i);&&&&&&&&&&&&&&& if (child != null && child is childItem)&&&&&&&&&&&&&&&&&&& return (childItem)&&&&&&&&&&&&&&& else&&&&&&&&&&&&&& &{&&&&&&&&&&&&&&&&&&& childItem childOfChild = FindVisualChild&childItem&(child);&&&&&&&&&&&&&&&&&&& if (childOfChild != null)&&&&&&&&&&&&&&&&&&&&&&& return childOfC&&&&&&&&&&&&&&& }&&&&&&&&&&& }&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&& &&&&//当TextBox失去焦点时发生此事件&&&&&&& private void renametextbox_LostFous(object sender, RoutedEventArgs e)&&&&&&& {&&&&&&&&&&& tempTextBox.Visibility = Visibility.C&&&&&&& }&WPF绑定实例详解
投稿:shichen2014
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了WPF绑定的用法,包括了WPF绑定控件及非控件对象的用法,以及各类参数的详细用法,需要的朋友可以参考下
本文详细讲述了WPF绑定的用法,分享给大家供大家参考。具体用法分析如下:
1.WPF绑定使用的源属性必须是依赖项属性,这是因为依赖项属性具有内置的更改通知支持,元素绑定表达式使用了Xaml扩展标记,WPF绑定一个控件是使用Binding.ElementName,绑定非控件对象时使用Source,RelativeSource,DataContext属性(WPF特有,而非XAML),只能绑定对象的共有字段.
下边是部分Binding 属性名,完整列表参考 :/zh-cn/library/vstudio/ms750413.aspx
① Source:数据提供者
② RelativeSource:根据当前对象为基础,自动查找源并绑定
③ DataContext:如果未使用Source和RelativeSource,WPF就从当前控件开始在控件树种向上查找,并使用第一个非空的DataContext属性,可以在更高层次容器对象上设置DataContext,如下代码 Text 绑定到 Source属性,但未设置Text的绑定对象,会向上查找DataContext绑定的对象的Source属性
④ 示例代码:
&StackPanel DataContext="{X:Static SystemFonts.IconFontFamily}"&
&TextBox Margin="5" Text="{Binding Path=Source}"&
&/TextBox&
&/StackPanel&
&TextBlock Margin="3" Name="lblSampleText"
FontSize="{Binding ElementName=sliderFontSize,Path=Value Mode="TwoWay"}"
Text="{Binding ElementName=txtContent,Path=Text}" Foreground="{Binding ElementName=lstColors,Path=SelectedItem.Tag}" &&/TextBlock&
也可是使用代码创建绑定:
Binding binding = new Binding();
binding.Source = sliderFonS
binging.path=new PropertPath("Value")
binding.Mode=BindignMode.TwoW
txt.SetBinding(TextBlock.FontSize,binding)
2.BindingMode的枚举值有:
③ OneTime:根据源端属性值设置目标属性值,之后的改变会被忽略,除非调用BindingExpression.UpdateTarge方法
④ OneWayToSource:与OneWay类似,但方向相反,用于目标属性是非依赖项属性的情况
⑤ Default:默认值,根据目标属性确定绑定类型.依赖项属性都由一个元数据 FrameworkPropertyMetadata.BindsTwoWayByDefault用于标识oneway绑定还是twoway绑定
3.从目标到绑定源端数据更新时(binding mode为twoway或者onewaytosource),更新行为(什么时机更新)由Binding.UpdateSourceTrigger枚举属性控制,UpdateSourceTrigger的值有:
① PropertyChanged:目标属性发生变化时立即更新
② LostFocus:目标属性发生变化并且目标丢失焦点时更新源
③ Explicit:除非调用BindingExpression.UpdateSource()方法,否则无法更新
④ Default:根据目标属性的元数据(FrameworkPropertMetadata.DefaulUpdateSourceTrigger)确定更新行为,大多数属性默认行为是PropertyChanged
4.MultiBinding:将多个对象绑定到一个控件,主要要使用StringFormat
&ListBox ItemsSource="{StaticResource MyData}"&
&ListBox.ItemTemplate&
&DataTemplate&
&TextBlock&
&TextBlock.Text&
&MultiBinding StringFormat="{}{0} -- Now only {1:C}!"&
&Binding Path="Description"/& &Binding Path="Price"/& &/MultiBinding&
&/TextBlock.Text&
&/TextBlock&
&/DataTemplate&
&/ListBox.ItemTemplate&
&/ListBox&
5.ObjectDataProvider:从另一个类中获取信息,只用于数据查询,IsAsynchronous=true,可以使ObjectDataProvider在后台执行,这样即使发生异常不会影响绑定控件的显示:
&ObjectDataProvider x:Key="productsProvider" ObjectType="{x:Type local:StoreDB}"
MethodName="GetProducts"&&/ObjectDataProvider&
6.WPF中派生自ItemsControl的类都能显示列表,能够支持集合数据绑定的元素包括ListBox,ComboBox,ListView和DataGrid,Menu,Treeview,ItemsControl中有三个重要属性:
① ItemsSource: 指向一个集合,结合必须支持IEnumerable接口,该集合包含将在列表中显示的所有元素,但基本的IEnumerable接口只支持只读绑定,要使修改能直接反应到绑定的控件上需要使用ObservablCollection类
② DisplayMemberPath:确定用于显示的 对象的属性,如果未设置 则会显示对象的ToString()方法返回的值
③ ItemTemplates:接受一个数据模板,用于为每个项创建可视化外观
7.继承自IEnumerable接口的类型都支持绑定到列表形元素,大多数集合类没有继承INotifyCollectionChanged接口,WPF提供了一个使用INotifyCollectionChanged接口的集合,ObservableCollction类
8.将Grid绑定到lstProducts对象的SelectItem属性
&Grid DataContext="{Binding ElementName=lstProducts,Path=SelectedItem}"&....&/Grid&
9.绑定时,被绑定的数据对象可能还不存在(绑定控件对象时可以看该对象在xaml中是否已经定义),这时依然可以在Xaml中绑定对象类属性(Binding Path),然后在代码中生成数据对象后在与控件绑定
10.WPF列表控件提供了UI虚拟化(UI Virtualization)功能用于提高大列表的性能,UI虚拟化是列表仅为当前显示项创建容器对象的一种技术
11.数据验证:用于捕获非法数据
① ExceptionValidationRule验证:验证失败时,WPF会在绑定元素上将Validation.HasError设置为True,WPF自动将控件使用的模板切换到又Validation.ErrorTemplate定义的模板,创建包含错误细节的ValidationError对象,并加到Validation.Errors集合中,如果Binding.NotifyOnValidationError属性设置为True,WPF就会在控件上引发Validation.Error事件
② IDataErrorInfo类:在数据对象中引发错误,并且要在binding中设置DataErrorValidationRule验证规则,当修改了一个属性后,IDataErrorInfo中的字符串索引器,以属性名为Key对值进行验证。可以自定义验证类并响应验证错误
12.没有Path的Binding:Binding源本身就是数据且不需要Path来指明,如下绑定表示将Text绑定到字符串类型mystring,mystring本身就是数据.Path后为"."或者空表示绑定source本身:
&TextBlock Text="{Binding source={StaticResource ResourceKye=mystring},Path=.}"&
相信本文所述对大家C#程序设计的学习有一定的借鉴价值。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具wpf程序中,用treeview读取选择目录下文件夹和文件
[问题点数:100分]
wpf程序中,用treeview读取选择目录下文件夹和文件
[问题点数:100分]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
本帖子已过去太久远了,不再提供回复功能。WPF中展开一个TreeView控件的所有树节点
WPF中展开一个TreeView控件的所有树节点
18:28:00来源:
&&&&&& 在 Windows Form 应用中,我们碰到需要展开一个TreeView 控件的所有树节点的时候很简单,微软已经替我们提供了ExpandAll 方法,我们只要简单的一行代码tv_QTree.ExpandAll();就可以了。即 System.Windows.Forms 命名空间的 TreeView.ExpandAll 方法 。 &&&&&& 在WPF中,我们会发现,System.Windows.Controls.TreeView 中没有了 ExpandAll 方法。唯一跟展开有关系的属性和方法就是每一个TreeViewItem 中有一个属性IsExpanded 属性。这个属性定义这个节点是否打开。MSDN帮助如下: &&&&&& Gets or sets whether the nested items in a TreeViewItem are expanded or collapsed. This is a dependency property.
&&&&&& 这时候如何办呢? 很简单,自己写个递归,遍历这个树的每一个子节点,然后把每一个子节点的 IsExpanded 设置为 true. &&&&&& 你可以通过以下几个链接看到这个解决方案: &&&&&&
& &&&&&& 我们可以在上面解决方案基础上进一步发展。 &&&&&& 用扩展方法来给 System.Windows.Controls.TreeView 类扩展个 ExpandAll方法方法。有关扩展方法的一些基础知识可以参看我之前的博客: &&&&& 我的扩展方法代码如下: /// &summary&/// 郭红俊的扩展方法/// &/summary&public static class ExtensionMethods{&&& /// &summary&&&& /// &&& /// &/summary&&&& /// &param name="treeView"&&/param&&&& public static void ExpandAll(this System.Windows.Controls.TreeView treeView)&&& {&&&&&&& ExpandInternal(treeView);&&& }
&&& /// &summary&&&& /// &&& /// &/summary&&&& /// &param name="targetItemContainer"&&/param&&&& private static void ExpandInternal(System.Windows.Controls.ItemsControl targetItemContainer)&&& {&&&&&&& if (targetItemContainer == null)&&&&&&& if (targetItemContainer.Items == null)&&&&&&& for (int i = 0; i & targetItemContainer.Items.C i++)&&&&&&& {&&&&&&&&&&& System.Windows.Controls.TreeViewItem treeItem = targetItemContainer.Items[i] as System.Windows.Controls.TreeViewI&&&&&&&&&&& if (treeItem == null)&&&&&&&&&&& if (!treeItem.HasItems)
&&&&&&&&&&& treeItem.IsExpanded =&&&&&&&&&&& ExpandInternal(treeItem);&&&&&&& }
& 扩展方法的使用方法也请参看&提到的注意事项。

我要回帖

更多关于 c# wpf 选择目录 的文章

 

随机推荐