WPF treeview 如何获得模板生成的控件 搜遍Google 无人能解
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ObservableCollection<A> a = new ObservableCollection<A>();
a.Add(new A());
a[0].name = "thy";
ObservableCollection<AB> ab = new ObservableCollection<AB>();
ab.Add(new AB());
ab[0].abc = "38";
a[0].ab = ab;
Tree.DataContext = a;
}
}
class A
{
public string name { get; set; }
public ObservableCollection<AB> ab { get; set; }
}
class AB
{
public string abc { get; set; }
}
}
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:WpfApplication3"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TreeView x:Name="Tree" ItemsSource="{Binding}">
<TreeView.Resources>
<DataTemplate x:Key="aTemplate" DataType="{x:Type src:AB}">
<TextBox Text="{Binding Path=abc}" />
</DataTemplate>
</TreeView.Resources>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type src:A}"
ItemsSource="{Binding Path=ab}"
ItemTemplate="{StaticResource aTemplate}">
<TextBox x:Name="NameBox" Width="160" Text="{Binding Path=name}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
</Window>
这里有个TextBox控件,名称为NameBox,但由于是Template生成的,因此在过程代码中无法直接引用,必须要用FindName。可是刚刚学WPF,始终不得要领。请高手指点。
拜托别拿Google出来的那个ListBox的例子来说事,那个其实是MSDN上的,http://msdn.microsoft.com/zh-cn/library/system.windows.frameworktemplate.findname.aspx,ListBox和TreeView用法不是一回事。Google了一圈,国外大的论坛也去看了,没结果。考虑到英文比较烂,先在CSDN问问。
希望回答前先写代码测试一下,没那么容易的,谢谢!
[最优解释]
MVP 和 MVVM 并不主张通过访问前台UI而得到数据的。
要访问数据,直接访问绑定了的 Model 的 dataItem 即可,
而通过数据绑定和IValueConverter,dataItem 中的变化可以在前台UI中直接呈现出来。
只有在“万不得以”的时候才会通过一些方法去访问前台UI,而事实上,这些“万不得以”的时候
往往都是由于开发人员思想没有转变过来而造成的。
[其他解释]
textbox的背景色同样可以绑定,只要不是只读属性,都可以通过绑定的方式,用后台model中对应属性来操作
[其他解释]
TextBox 没有 IsFocused 属性哦( Silverlight )。
如何让 TextBox 获得焦点看来本身就是一个问题哦。
另外,leon0905 ,上次有位朋友也是在分层模板中使用 IsSelected 绑定,并且使用 TwoWay 绑定,
可是我在 Silverlight 中试出来的结论是在 Style 中使用不了 数据绑定,只能绑定 StaticResource,
不知在 WPF 中是否没有这个限制?
[其他解释]
还好是WPF,可以试下在style里面用setter绑定IsFocus(差不多这个名字),就是加个<setter>,value用绑定
[其他解释]
“WPF可以的,上次回答的时候没注意是SL,因为在WPF的项目中用过的”
谢谢!
难怪上次那位朋友后来说按照你的例子解决了问题了。
[其他解释]
看上去最像的一个是这样的:
var container = Tree.ItemContainerGenerator.ContainerFromItem(Tree.Items[0]) as FrameworkElement;可惜还是不行。哎,等神人出现了
var textBox = Tree.ItemTemplate.FindName("NameBox", container) as TextBox;
System.Diagnostics.Debug.WriteLine(textBox);
[其他解释]
恩,学习。。。
[其他解释]
我也不主张跟UI打交道啊,可我想设置那个TextBox的背景色
[其他解释]
大家的道理我都明白。谢谢
不过我刚刚学习C#+WPF,model什么的也不懂。
现在就题论题,谁能解吗?
[其他解释]
Backgroud="{Binding Path=solidcolor}"
[其他解释]
都晕了,我打错了,是让NameBox获得焦点
[其他解释]
没试过,以前最多就绑下IsSelected,不知道焦点有没有用
[其他解释]
WPF可以的,上次回答的时候没注意是SL,因为在WPF的项目中用过的
[其他解释]
该回复于2010-09-21 10:20:21被版主删除
[其他解释]
终于解决了,谢谢各位的参与!
[其他解释]
这答案还没出来呢。。LZ把代码贴出来一下,让我们也看一下呀
[其他解释]
怎么获取的
[其他解释]
我也遇到过这个问题。。麻烦告诉我一下。。谢谢。。