本文介绍了帮助我有关Access templateChild的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Windows Phone 7中有问题
我有一个功能:
I have a problem in windows phone 7
I have a function:
public class myDataContext : DataContext
{
public Table<task> task;
public myDataContext(string ConnectionString) : base(ConnectionString) { }
}
private void DisplayData()
{
var query =
from dt in database.task
select dt;
List<task> myData = query.ToList();
lstListSite.ItemsSource = myData;
}
</task></task>
<Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
<setter property="Background" value="Transparent" />
<setter property="BorderThickness" value="0" />
<setter property="BorderBrush" value="Transparent" />
<setter property="Padding" value="0" />
<setter property="HorizontalContentAlignment" value="Left" />
<setter property="VerticalContentAlignment" value="Top" />
<setter property="Template">
<setter.value>
<controltemplate targettype="ListBoxItem">
<grid>
<grid.columndefinitions>
<columndefinition width="380" />
<columndefinition width="75" />
</grid.columndefinitions>
<grid.rowdefinitions>
<rowdefinition />
<rowdefinition />
<rowdefinition />
</grid.rowdefinitions>
<textblock name="tbNameAddSite" text="{Binding Name}" grid.row="0" fontsize="{StaticResource PhoneFontSizeLarge}" />
<textblock text="{Binding URL}" grid.row="1" />
<Button Name="btnDelete" Height="75" Grid.Column="1" Click="btnDelete_Click">
<Image Source="Images/appbar.delete.rest.png" Stretch="Fill" />
</Button>
<textblock text="{Binding Category}" grid.row="2" />
</grid>
</controltemplate>
</setter.value>
</setter>
</Style>
在XAML中,我有一个列表框
In the XAML, i have a listbox
<ListBox Name="lstListSite" Grid.Row="4" Grid.ColumnSpan="2" ItemsSource="{Binding myTaskItem}" ItemContainerStyle="{StaticResource ListBoxItemStyle}" SelectionChanged="lstListSite_SelectionChanged"/>
我想从该列表框中访问模板子元素,
我尝试使用:
I want acess template child from that Listbox,
I attempt use :
private void lstListSite_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBoxItem lbi = lstListSite.ItemContainerGenerator.ContainerFromItem(lstListSite.SelectedIndex) as ListBoxItem;
TextBlock txtbx = FindVisualChildByType<textblock>(lbi,"tbNameAddSite");
if (txtbx.Text != null)
txtbx.Text = "hoantrogngag";
}
T FindVisualChildByType<t>(DependencyObject element, String name) where T : class
{
if (element is T && (element as FrameworkElement).Name == name)
return element as T;
int childcount = VisualTreeHelper.GetChildrenCount(element);
for (int i = 0; i < childcount; i++)
{
T childElement = FindVisualChildByType<t>(VisualTreeHelper.GetChild(element, i), name);
if (childElement != null)
return childElement;
}
return null;
} </t></t></textblock>
但是我遇到了一个问题:
But I recived a problem :
Reference is not a valid visual DependencyObject
请帮我解决这个问题!
非常感谢
Please help me fix that!
thanks a lot
推荐答案
这篇关于帮助我有关Access templateChild的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!