从的DataTemplate访问DataGrid中(Silverlight的)

编程入门 行业动态 更新时间:2024-10-21 06:04:38
本文介绍了从的DataTemplate访问DataGrid中(Silverlight的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

WPF如何从的DataTemplate

在上面的例子中,答案都与.NET解决方案。我在找Silverlight的类似解决方案,它不具有FindName的框架模板。我的DataTemplate有,我需要用它来打开则selectedItem对电网的网格。我现在有4格其使用的内容presenter放置,并因为Silverlight不支持TemplateSelector我有能见度设置为隐藏和显示正确的内容presenter。 有没有一种方法,我可以在我的数据模板访问网格?

In above example, the answers are all related to .Net solution. I am looking for similar solution for silverlight, it does not have FindName on Framework Template. My DataTemplate has a grid which I need to use to open the selecteditem on the grid. I currently have 4 grids which are placed using contentpresenter and since Silverlight does not support TemplateSelector I have visibility set to hide and show correct content presenter. Is there a way I can access Grid within my data template?

<DataTemplate x:Key="DefaultTemplate"> <toolkit:DataGrid x:Name="dgGrid" ItemsSource="{Binding List}" HorizontalAlignment="Stretch" HeadersVisibility="Column" ColumnHeaderHeight="30" VerticalGridLinesBrush="Transparent" AutoGenerateColumns="False" SelectionMode="Single" RowStyle="{StaticResource rowStyle}" FontSize="16" IsReadOnly="True"> <toolkit:DataGrid.Columns> <toolkit:DataGridTemplateColumn Width="Auto" SortMemberPath="IsInvalid" > <toolkit:DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="!" Visibility="{Binding IsInvalid, Converter={StaticResource conv}}"/> </DataTemplate> </toolkit:DataGridTemplateColumn.CellTemplate> </toolkit:DataGridTemplateColumn> <!--Open Button--> <toolkit:DataGridTemplateColumn Width="70"> <toolkit:DataGridTemplateColumn.CellTemplate> <DataTemplate> <Button Content="Open" Width="60" FontSize="14" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0" Click="Open_Click" /> </DataTemplate> </toolkit:DataGridTemplateColumn.CellTemplate> </toolkit:DataGridTemplateColumn> <toolkit:DataGridTextColumn Header="Name" Width="150" FontSize="18" Binding="{Binding CustomerName}" /> </toolkit:DataGrid.Columns> </toolkit:DataGrid> </DataTemplate>

我想访问dgGrid上面的DataTemplate

I am trying to access dgGrid on above DataTemplate

更新:

在我控制我有以下

<StackPanel> <ContentPresenter Content="{Binding}" DataContext="{Binding}" ContentTemplate="{StaticResource Template1}" Visibility="{Binding PanelID,Converter={StaticResource ParamVisibilityConv}, ConverterParameter=5}"/> <ContentPresenter Content="{Binding}" DataContext="{Binding}" ContentTemplate="{StaticResource Template2}" Visibility="{Binding PanelID,Converter={StaticResource ParamVisibilityConv}, ConverterParameter=7}"/> <ContentPresenter Content="{Binding}" DataContext="{Binding}" ContentTemplate="{StaticResource Template3}" Visibility="{Binding PanelID,Converter={StaticResource ParamVisibilityConv}, ConverterParameter=13}"/> <ContentPresenter Content="{Binding}" DataContext="{Binding}" ContentTemplate="{StaticResource DefaultTemplate}" Visibility="{Binding PanelID,Converter={StaticResource ParamVisibilityConv}, ConverterParameter=4}"/> </StackPanel>

我想访问的背后

I am trying to access the grid in code behind

StackPanel sp = (StackPanel) this.Content; foreach (var child in sp.Children) { if (child is ContentPresenter && ((ContentPresenter) child).Visibility == Visibility.Visible) { DataTemplate dt = ((ContentPresenter) child).ContentTemplate; } }

它返回一个DataTemplate但FrameworkTemplates不具有FindName功能找到一个DataTemplate中的元素。

it returns a DataTemplate but FrameworkTemplates dont have FindName function to find elements within a DataTemplate.

有没有解决办法?

推荐答案

找到您的DataTemplate你要加载什么是里面之后。 例如:

After finding your DataTemplate you have to load whatever is inside it. For Example:

var grid = dt.LoadContent() as DataGrid/StackPanel/AnyElement;

在你加载的元素,您可以使用System.Linq的组件,在其中进行高层次的查询。

Once you loaded the element you can use System.Linq assembly to perform high level queries within it.

例如:

var item = grid.Children.First(c => c.Name == "element1");

希望它可以帮助

Hope it helps

更多推荐

从的DataTemplate访问DataGrid中(Silverlight的)

本文发布于:2023-11-07 05:34:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565678.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:DataTemplate   DataGrid   Silverlight

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!