简体   繁体   English

为什么Contenttemplate为null

[英]Why is Contenttemplate null

I am using WPF with MVVM principles. 我正在将WPF与MVVM原理结合使用。 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. 我正在尝试访问myApp:TextBox来设置焦点。 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). 为此,我在后面的代码中使用了此代码段(未完成)(我知道MVVM的原理,我认为我没有违反它们)。

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? contenttemplate是否为null? Why? 为什么? What am making wrong? 怎么了?

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() . 为了解决此问题,您将必须调用cp.ApplyTemplate或使用Dispatcher.BeginInvoke()延迟UI线程在后台时要执行的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM