访问DataTemplate中的TextBox

时间:2010-09-05 07:56:14

标签: c# xaml

我有这样的XAML。 (此代码只是我源代码的一部分)

<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

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

1 个答案:

答案 0 :(得分:0)

如何将Binding设置为TwoWay模式并访问Attribution属性呢?这似乎比走视觉树简单得多。