[英]How to retrieve the text entered in texbox inside datagrid
我在cellediting模板的datagrid內有一個文本框。 我想將在文本框中輸入的文本綁定到每個單元格的文本塊。 我嘗試了此代碼,但無法正常工作。
這是我的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");
}
}
如果有人知道該怎么做,請幫助我。
我認為您在后台代碼中錯過了此代碼
public void monothers_TextChanged(object sender, TextChangedEventArgs e)
{
var binding = ((TextBox)sender).).GetBindingExpression(TextBox.TextProperty);
binding.UpdateSource();
}
更多細節
看到這個討論
如何連接TextBox的TextChanged事件和命令以在Silverlight中使用MVVM模式
而且您還需要參考此博客
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.