如何检索在datagrid中的文本框中输入的文本

时间:2013-10-25 05:07:45

标签: c# wpf

我在cellediting模板的数据网格中有一个文本框。我想绑定文本 在文本框中输入以对每个单元格进行文本阻止。我尝试了这段代码,但它不起作用。

这是我的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");
            }
        }

如果有人知道怎么做,请帮助我。

1 个答案:

答案 0 :(得分: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