2013-10-25 55 views
0

我有一個文本框內cellediting模板中的數據網格。我想將在文本框中輸入的文本 綁定到每個單元格的文本塊。我試過這個代碼,但它不會工作。如何檢索在數據網格中的texbox中輸入的文本

這是我的XAML:

    <DataTemplate> 
         <StackPanel Orientation="Horizontal"> 
          <!--<ComboBox x:Name="monday" Width="50" IsSynchronizedWithCurrentItem="true" Loaded="monday_Loaded" SelectionChanged="monday_SelectionChanged"></ComboBox>--> 
          <ComboBox x:Name="monday" Width="30" ItemsSource="{Binding Path=Subjects}" DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" SelectedItem="{Binding SelectedCollectionItem,Mode=TwoWay}"  Loaded="monday_Loaded" SelectionChanged="monday_SelectionChanged"></ComboBox> 
          <ComboBox x:Name="staff" Width="30" ItemsSource="{Binding Path=mondstaff}" DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" SelectedItem="{Binding SelectedCollectionItem1,Mode=TwoWay}" Loaded="staff_Loaded" SelectionChanged="staff_SelectionChanged"></ComboBox> 
          <TextBox x:Name="monothers" Visibility="Hidden" Text="{Binding Path=Subjects}" DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" SelectedText ="{Binding SelectedCollectionItem2,Mode=TwoWay}" Width="30" TextChanged="monothers_TextChanged"></TextBox> 
          <!--<ComboBox x:Name="staff" Width="50" Loaded="staff_Loaded"></ComboBox>--> 
         </StackPanel> 
        </DataTemplate> 

這裏是我的代碼:

public string SelectedCollectionItem 
     { 
      get { return _SelectedCollectionItem; } 
      set 
      { 
       _SelectedCollectionItem = value; 
       RaiseProperty2("SelectedCollectionItem2"); 
      } 
     } 

如果有人知道如何做到這一點,請幫助我。

回答

0

我想你錯過了你的代碼隱藏

public void monothers_TextChanged(object sender, TextChangedEventArgs e) 
{ 
    var binding = ((TextBox)sender).).GetBindingExpression(TextBox.TextProperty); 
    binding.UpdateSource(); 
} 

更多細節

看到這個討論這個代碼

How to hookup TextBox's TextChanged event and Command in order to use MVVM pattern in Silverlight

,你也需要參照這個博客

Make a Silverlight TextBox update its binding on every character with a Behavior

+0

好的謝謝你的建議 – prabhakar

相關問題