I want to modify the content in a TabItem of a TabView. And that TabItem uses DataTemplate.
When I am trying to access the children of that item like the following:
var container = tabview.ContainerFromIndex(tabview.SelectedIndex);
int count = VisualTreeHelper.GetChildrenCount(container);
I got the ArgumentException: Wrong Parameter Reference on the second line. How should I use VisualTreeHelper to modify it?
Here is a easy method:
From your description, you can already get the container of the target element. Let's assume that the element you need is named
TargetEleand the type isTextBlock. You can write it like this:Update
I tested your code and found that you didn't capture the events loaded by the page.
In fact, the
SelectionChangedevent is fired when the TabView is just created, but the visual tree is not loaded yet, and you can't get the content from it through the code. You can create anIsLoadedproperty in the page, set it to True when Page Loaded, and determine this property in theSelectionChangedtime.Only when it is True, proceed to the next step.