【问题标题】:How to retrieve the text entered in texbox inside datagrid如何检索在数据网格内的文本框中输入的文本
【发布时间】:2013-11-04 01:56:20
【问题描述】:

我在单元格编辑模板的数据网格中有一个文本框。我要绑定文字 在文本框中输入文本块每个单元格。我试过这段代码,但它不起作用。

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

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

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    我认为您在代码隐藏中错过了这段代码

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-24
      • 1970-01-01
      相关资源
      最近更新 更多