DISABLE ADBLOCK

ADBlock is blocking some content on the site

ADBlock errore

Question

I am using WPF with MVVM principles. The view looks like this:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="MyResources.xaml" />
        </ResourceDictionary.MergedDictionaries>

        <DataTemplate x:Key="ValveOptionTemplate" >                
            <Grid Margin="{StaticResource MyApp.DefaultMarginTopBottomThin}" 
                  VerticalAlignment="Center">                        
               <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="Auto"/>
                  <ColumnDefinition Width="Auto"/>
               </Grid.ColumnDefinitions>                        
               <Grid.RowDefinitions>
                  <RowDefinition Height="*"/>
               </Grid.RowDefinitions>

               <myApp:CheckBox Grid.Column="0"  Grid.Row="0" 
                               Margin="{StaticResource 
                                          MyApp.DefaultMarginTopBottomThin}"
                               IsChecked="{Binding IsSelected}" 
                               VerticalAlignment="Center" 
                               Checked="ToggleButton_OnChecked"/>
               <myApp:TextBox Grid.Column="1"  Grid.Row="0"
                              Width="300"
                              Margin="{StaticResource MyApp.DefaultMarginLeftThin}" 
                              VerticalAlignment="Center"
                              Text="{Binding Description, 
                                       UpdateSourceTrigger=PropertyChanged}" 
                              IsEnabled="{Binding IsEditable}"                                     
                              x:Name="textBox"/>    
            </Grid>                    
         </DataTemplate>    
        </ResourceDictionary>
    </UserControl.Resources>

    <Grid x:Name="OptionGrid">
        <ItemsControl x:Name="Options" ItemsSource="{Binding Options}"
                      ItemTemplate="{StaticResource ValveOptionTemplate}" 
                      FocusVisualStyle="{x:Null}" 
                      Margin="{StaticResource MyApp.DefaultMarginTopBottomThin}"/>
    </Grid>

Nothing strange i would say. I am trying to access the myApp:TextBox to set the focus on it. For this i use this (not completed) snippet in code behind (i know what MVVM principles are and i don t think i am violating them).

private void ToggleButton_OnChecked( object sender, RoutedEventArgs e )
{
   var cp = Options.ItemContainerGenerator.ContainerFromIndex(0) as ContentPresenter;        
   var dt = cp.ContentTemplate; //<--this is null! Why?
   var tb = (TextBox)(dt.FindName( "textBox", cp ));           
}

You see my comment in the event handler? The contenttemplate is null? Why? What am making wrong?

No correct solution

OTHER TIPS

At the point the binding is being evaluated the complete visual tree is not build yet.

That is why template is not available.

In order to fix this you will have to call cp.ApplyTemplate or postpone the code to be executed when UI thread is in background by using Dispatcher.BeginInvoke().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow