scubee

Hi All,

I am facing a Problem accessing Xaml TextBlock in C# as its giving an exception

of object out of context in code behind file.

Folowing is the code...

<ListView Name="lstArticlesRed" ItemsSource="{Binding}" Width="320" Height="675" >

<ListView.View>

<GridView>

<GridViewColumn Header="Title">

<GridViewColumn.CellTemplate>

<HierarchicalDataTemplate ItemsSource="{Binding}">

<Label Name="lblNew">

<Hyperlink Foreground="SlateGray" Click="DebriefContent" >

<TextBlock Name="txtBlock" Height="70" Foreground="Red" Text="{Binding SectionName}" MouseEnter="ChangeBackgroundColor"/>

</Hyperlink>

</Label>

</HierarchicalDataTemplate>

</GridViewColumn.CellTemplate>

</GridViewColumn>

</GridView>

</ListView.View>

</ListView>

I want to access Textblock txtBlock ic C# and want to change the color of listitem on mouseover.

As soon as i am writing txtBlock. in code behind the intellisense is not working for me.

It shows object out of context....

Plz let me know if there is any other approach for doing this.

Thanx and Regards,

Scubee



Re: Windows Presentation Foundation (WPF) Xaml Object in C#

Nishant Sivakumar

Perhaps it's just an intellisense issue. Does it compile




Re: Windows Presentation Foundation (WPF) Xaml Object in C#

lee d

you cannot access the elements in datatemplate directly in code. you can try using triggers




Re: Windows Presentation Foundation (WPF) Xaml Object in C#

Tina Tam - MSFT

To access the named element that you want, you need to walk through the VisualTree to get a hold of the ContentPresenter that the DataTemplate is set on, and then call Template.FindName.

I'm in the process of creating a blog post that goes over the code that does that. I'll post it here when it's ready.

Hope that helps,

Tina






Re: Windows Presentation Foundation (WPF) Xaml Object in C#