0

Using Windows Phone 8, C#.

What I've done is basically done is edited the pivot item. I've named it MainPivot and inside that I've edited the Pivot Item Title and added a TextBlock inside it called PivotTitletxt. XAML for that is:

<DataTemplate x:Key="DataTemplate3">
  <TextBlock x:Name="PivotTitletxt" Height="34" TextWrapping="Wrap" Text="{Binding}" Width="447"/>
</DataTemplate>

How can I access this e.g. when setting opacity or changing foreground? so that I can use it on my MainPage like e.g. PivotTitletxt.Opacity = 30; ...

Thanks!

Ahmed.C
  • 487
  • 1
  • 6
  • 17

1 Answers1

1

The link @Sankarann gave you is a pretty good example.

I'll try to put it on your scenario:

Your MainPivot has PivotItems right? So What you have to do on the Loaded event is:

var _mainPivot = MainPivot as Pivot
foreach (var _pivotItem in _mainPivot.Items)
{
var _container = _mainPivot.ItemContainerGenerator.ContainerFromItem(_pivotItem);
var _children = AllChildren(_container)
var _name = "PivotTitletxt";
var _control = (TextBlock)_Children.first(x=>x.Name == _name);
_control.Opacity  = 30;

} 

Then copy the AllChildren method exactly as the it is in the site.

The code above, might have a few adjustments because I've done it without VS...

Hope it helps.

Regards,

============ new answer ==============

Find all controls in WPF Window by type

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
    if (depObj != null)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
            if (child != null && child is T)
            {
                yield return (T)child;
            }

            foreach (T childOfChild in FindVisualChildren<T>(child))
            {
                yield return childOfChild;
            }
        }
    }
}

Then try :

TextBlock xx = FindVisualChildren<TextBlock>(mainPivot).FirsOrDefault(x=>x.name=="PivotTitletxt");

if(xx!=null)
xx.opacity = 30

Once again, this come might need some correction...i'm doing it by heart, without VS.

Try it out

Community
  • 1
  • 1
sexta13
  • 1,558
  • 1
  • 11
  • 19
  • I managed to get the thing sorted but now It keeps saying that `Cannot convert type 'System.Windows.Controls.Control' to 'System.Windows.Controls.TextBlock'` What do you think I should do? – Ahmed.C Feb 26 '14 at 21:58
  • can you show me the line where it is giving that error? – sexta13 Feb 26 '14 at 22:09
  • It's this line: `var _Control = (TextBlock)_Children.First(x => x.Name == _Name);` – Ahmed.C Feb 26 '14 at 22:14
  • Try replacing Control by FrameworkElement in the AllChildren method. The reason is that Textblock inherits from FrameworkElement and TextBox from System.Windows.Control – sexta13 Feb 26 '14 at 22:14
  • I'll give you another solution in one minute – sexta13 Feb 26 '14 at 22:22
  • Ok. So I think there is a problem in the item being found. Remember that the textblock "PivotTitleTxt" is inside the MainPivot's Title template not the Pivot Items template. Could there be a problem in those areas. Searching for the wrong template? It keeps saying that the item has no matching name etc.. – Ahmed.C Feb 26 '14 at 22:23
  • Seems like even that didn't work. The other alternative way might be the way @Shawn Kendrot mentioned. DataBinding. Oh well thanks for your help. – Ahmed.C Feb 26 '14 at 22:37
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48529/discussion-between-sexta13-and-ahmed-c) – sexta13 Feb 26 '14 at 22:39