silverlight 获取 ControlTemplate 中的控件

在路上20130607 2012-06-14 06:18:40

StringBuilder strStyle = new StringBuilder();
strStyle.Append("<Style xmlns='http://schemas.microsoft.com/client/2007' xmlns:sdk='http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Key=\"STYLE_DATAGRIDCELL\" TargetType=\"sdk:DataGridCell\"> ");
strStyle.Append(" <Setter Property=\"Foreground\" Value=\"Blue\" /> ");
strStyle.Append(" <Setter Property=\"Cursor\" Value=\"Hand\"></Setter> ");
strStyle.Append(" <Setter Property=\"Template\" > ");
strStyle.Append(" <Setter.Value> ");
strStyle.Append(" <ControlTemplate> ");
strStyle.Append(" <TextBlock x:Name=\"UrlTextName\" VerticalAlignment=\"Center\" Tag=\"{Binding}\" Text=\"{Binding " + name + "}\" /> ");
strStyle.Append(" </ControlTemplate> ");
strStyle.Append(" </Setter.Value> ");
strStyle.Append(" </Setter> ");
strStyle.Append("</Style> ");
Style style = (Style)XamlReader.Load(strStyle.ToString());


如何获取style中的TextBlock 控件。
麻烦各位帮忙看下。
...全文
201 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
在路上20130607 2012-07-04
  • 打赏
  • 举报
回复
感谢各位的回复
wwwljh 2012-06-15
  • 打赏
  • 举报
回复
况1:在设定DataTemplate的Name,并且他是在前台表示时,获取DataTemplate里的指定控件。
方法:

http://blog.csdn.net/wackelbh/article/details/6003947(参考这篇文章)
情况2:当没有设定DataTemplate的Name或是以Resource方式调用时,获取DataTemplate里的指定控件。

方法:
1、这里需要有一个从DataTemplate里获取控件的函数
[csharp] view plaincopy
01.public T FindFirstVisualChild<T>(DependencyObject obj, string childName) where T : DependencyObject
02. {
03. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
04. {
05. DependencyObject child = VisualTreeHelper.GetChild(obj, i);
06. if (child != null && child is T && child.GetValue(NameProperty).ToString() == childName)
07. {
08. return (T)child;
09. }
10. else
11. {
12. T childOfChild = FindFirstVisualChild<T>(child, childName);
13. if (childOfChild != null)
14. {
15. return childOfChild;
16. }
17. }
18. }
19. return null;
20. }

2、稍微改动一下前篇里的代码:


[html] view plaincopy
01.<ItemsControl x:Name="itemsControl" Background="#B28BB2F1">
02. <ItemsControl.ItemsPanel>
03. <ItemsPanelTemplate>
04. <WrapPanel Orientation="Horizontal"/>
05. </ItemsPanelTemplate>
06. </ItemsControl.ItemsPanel>
07. <ItemsControl.ItemTemplate>
08. <DataTemplate>
09. <Border Padding="3">
10. <WrapPanel>
11. <TextBox x:Name="txtID"/>
12. <TextBlock x:Name="txtName" Text="Good"/>
13. </WrapPanel>
14. </Border>
15. </DataTemplate>
16. </ItemsControl.ItemTemplate>
17.</ItemsControl>

或者




[html] view plaincopy
01.<Page.Resource>
02. <DataTemplate x:Key="data">
03. <Border Padding="3">
04. <WrapPanel>
05. <TextBox x:Name="txtID"/>
06. <TextBlock x:Name="txtName" Text="Good"/>
07. </WrapPanel>
08. </Border>
09. </DataTemplate>
10.</Page.Resources>
11.
12.<ItemsControl x:Name="itemsControl" Background="#B28BB2F1" ItemTemplate="{StaticResource data}">
13. <ItemsControl.ItemsPanel>
14. <ItemsPanelTemplate>
15. <WrapPanel Orientation="Horizontal"/>
16. </ItemsPanelTemplate>
17. </ItemsControl.ItemsPanel>
18.</ItemsControl>

3、解下来就写按钮的处理函数:

我需要获取DataTemplate里名为"txtName"的TextBlock控件并显示他的Text内容。



[csharp] view plaincopy
01.private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
02.{
03. TextBlock txt = FindFirstVisualChild<TextBox>(itemsControl, "txtName");
04. if (txt != null)//判断是否找到
05. MessageBox.Show(txt.Text.ToString());
06.}



情况3:当没有设定DataTemplate的里的控件Name或者你压根不知道里面有哪些控件,但是你又想获取他们的值时。例如上一篇,当我动态生成CheckBox后,我想知道哪些CheckBox被选中了。




方法:

1、也需要一个获取DataTemplate控件的函数,但是返回的是一个集合。


[csharp] view plaincopy
01.public List<T> GetChildObjects<T>(DependencyObject obj, string name) where T : FrameworkElement
02.{
03. DependencyObject child = null;
04. List<T> childList = new List<T>();
05. for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
06. {
07. child = VisualTreeHelper.GetChild(obj, i);
08. if (child is T && (((T)child).Name == name || string.IsNullOrEmpty(name)))
09. {
10. childList.Add((T)child);
11. }
12. childList.AddRange(GetChildObjects<T>(child, ""));//指定集合的元素添加到List队尾
13. }
14. return childList;
15. }

2、xaml中代码(详细请看前一篇)


[html] view plaincopy
01.<ItemsControl x:Name="itemsControl" Background="#B28BB2F1">
02. <ItemsControl.ItemsPanel>
03. <ItemsPanelTemplate>
04. <WrapPanel Orientation="Horizontal"/>
05. </ItemsPanelTemplate>
06. </ItemsControl.ItemsPanel>
07. <ItemsControl.ItemTemplate>
08. <DataTemplate>
09. <Border Padding="3">
10. <WrapPanel>
11. <CheckBox Content="{Binding txt}"/>
12. </WrapPanel>
13. </Border>
14. </DataTemplate>
15. </ItemsControl.ItemTemplate>
16.</ItemsControl>

3、解下来就写按钮的处理函数:


[csharp] view plaincopy
01.private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
02.{
03. DataVisualTreeHelper VTHelper = new DataVisualTreeHelper();
04. List<CheckBox> collection = VTHelper.GetChildObjects<CheckBox>(itemsControl, "")//第2个参数为空,表示查找所有指定类型的控件(返回
05.
06.一个CheckBox集合)
07. foreach (CheckBox item in collection //遍历这个集合
08. {
09. if (item.IsChecked == true)
10. MessageBox.Show(item.Content.ToString() + "被选中了!");
11. }
12.}
在路上20130607 2012-06-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

你可以参考,里面有两种获取模板内控件的方法,但是都需要你点击模板上的按钮。
http://www.cnblogs.com/chengxingliang/archive/2012/05/08/2487115.html
[/Quote]

谢谢 我试试
Architecture Net 2012-06-14
  • 打赏
  • 举报
回复
你可以参考,里面有两种获取模板内控件的方法,但是都需要你点击模板上的按钮。
http://www.cnblogs.com/chengxingliang/archive/2012/05/08/2487115.html
在路上20130607 2012-06-14
  • 打赏
  • 举报
回复
这个style 是给 datagrid 的 Column 的CellStyle 使用
column 没有 template 属性 所以msdn上提供的
Grid gridInTemplate = (Grid)myButton1.Template.FindName("grid", myButton1);
方法 用不了

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧