2010-06-01 93 views
1

美好的一天!樹狀圖信息與HierarchicalDataTemplate

我有這樣一個模板:

<common:HierarchicalDataTemplate x:Key="my2ndPlusHierarchicalTemplate" ItemsSource="{Binding Children}"> 
        <StackPanel Margin="0,2,5,2" Orientation="Vertical" Grid.Column="2"> 
         <CheckBox 
         IsTabStop="False" 
         IsChecked="False" 
         Click="ItemCheckbox_Click" 
         Grid.Column="1" 
         /> 
         <TextBlock Text="{Binding Name}" FontSize="16" Foreground="#FF100101" HorizontalAlignment="Left" FontFamily="Verdana" FontWeight="Bold" /> 
         <TextBlock Text="{Binding Description}" FontFamily="Verdana" FontSize="10" HorizontalAlignment="Left" Foreground="#FFA09A9A" FontStyle="Italic" />  

<TextBox Width="100" Grid.Column="4" Height="24" LostFocus="TextBox_LostFocus" Name="tbNumber"></TextBox> 

</StackPanel> 
     </common:HierarchicalDataTemplate> 

爲一個TreeView

<controls:TreeView x:Name="tvServices" ItemTemplate="{StaticResource myHierarchicalTemplate}" ItemContainerStyle="{StaticResource expandedTreeViewItemStyle}" Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="3" BorderBrush="#FFC1BCBC" FontFamily="Verdana" FontSize="14">      
     </controls:TreeView> 

我想知道在樹視圖每個TextBox的名稱屬性,以使每個文本框的驗證,如:

private void TextBox_LostFocus(object sender, RoutedEventArgs e) 
     { 
      tbNumber.ClearValidationError(); 
      if ((!tbNumber.Text.IsZakazNumberValid()) && (tbNumber.Text != "")) 
      { 
       tbNumber.SetValidation(MyStrings.NumberError); 
       tbNumber.RaiseValidationError(); 
       isValid = false; 
      } 
      else 
      { 
       isValid = true; 
      } 
     } 

我想看看檢查框是什麼檢查

我該怎麼辦?

回答

0

我不確定我清楚你爲什麼需要知道這個名字。

在這種情況下LostFocus事件的參數senderTextBox。因此,你可以使用: -

TextBox tb = (TextBox)sender; 

現在使用這個tb變量,而不是使用tbNumber(這實際上並不存在,因爲它在模板中定義)。

+0

哦!非常感謝您的回答!這正是我需要的! – lina 2010-06-02 05:07:41