2010-09-05 92 views
1

我有这样的XAML。 (此代码是我的源代码只是一些部分)访问DataTemplate中的TextBox

<UserControl x:Class="EmployeeClass" 
...............> 
............. 
............. 
<ListView x:Name="EmployeeList" Width="700" Height="500" Margin="10" 
           ItemsSource ="{Binding Source={x:Static local:Company.employeeList}}" 
           Background="Black" 
           ItemTemplateSelector="{DynamicResource myDataTemplateSelector}" Focusable="False" 
           IsSynchronizedWithCurrentItem="True">        
         </ListView> 
</UserControl> 

这里是我的数据模板的一个

<DataTemplate x:Key="SeniorEmployee" DataType="{x:Type local:ListEmployee}"> 
        <Grid> 
         <Grid.RowDefinitions> 
          <RowDefinition/> 
          <RowDefinition/> 
          <RowDefinition/> 
         </Grid.RowDefinitions> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition/> 
          <ColumnDefinition/> 
         </Grid.ColumnDefinitions> 

         <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="10"> 
          <Image x:Name="ProfilePicture" Source="{Binding Path=ProfilePict}" Stretch="Fill" Width="80" Height="80" VerticalAlignment="Top"></Image> 
          <StackPanel Orientation="Vertical" Margin="5">         
           <TextBlock Text="{Binding Path=Attribution}" Foreground="White" TextWrapping="Wrap" Width="400" VerticalAlignment="Top" HorizontalAlignment="Left"/> 

         <StackPanel Margin="7,5,0,7"> 
             <UISkripsi3:CommandTextBox Name="AttributionTextBox" HorizontalAlignment="Left" Margin="10,5" Panel.ZIndex="1" 
                   BannerText="Add to the attribution" FontSize="{DynamicResource LargeFontSize1}" 
                   SpellCheck.IsEnabled="True" Style="{DynamicResource AttributionTextBoxStyle}" 
                   TextWrapping="Wrap"> 
             </UISkripsi3:CommandTextBox> 
             <Button Style="{DynamicResource StandardButtonStyle}" IsEnabled="{Binding ElementName=AttributionTextBox, Path=Text, Converter={StaticResource IsStringNullOrWhitespaceConverter}, ConverterParameter=Inverse}" Click="AttributionButtonClick" Height="22" HorizontalAlignment="Right" Margin="10,4,10,0" Panel.ZIndex="0" CommandTarget="{Binding ElementName=AttributionTextBox}" Content="Show" FontSize="{DynamicResource LargeFontSize1}"> 
             </Button> 
             </StackPanel> 
        </Grid> 
       </DataTemplate> 

如果用户类型的东西在AttributionTextBox并单击按钮,会出现一个消息显示用户输入的文字。我曾尝试 http://blogs.msdn.com/b/wpfsdk/archive/2007/04/16/how-do-i-programmatically-interact-with-template-generated-elements-part-ii.aspx 和H * TP视觉树帮手://msdn.microsoft.com/en-us/library/system.windows.media.visualtreehelper.aspx

我的问题是,我可以” t在代码后面找到AttributionTextBox。我也无法访问EmployeeList Listview,因为它不是在后面的代码中生成的。任何人都可以帮我解决这个问题吗?

回答

0

将Binding设置为TwoWay模式并访问Attribution属性怎么样?这似乎比行走视觉树更简单。

+0

我想访问AttributionTextBox而不是Attribution属性,当用户输入一些内容并点击按钮时,文本将被显示。但是,thx为您的建议 – 2010-09-06 13:52:29

相关问题