我用 DataGrid 和 TextBox 创建窗口:
<DataGrid CanUserAddRows="False" ItemsSource="{Binding Path=CRow}" HorizontalAlignment="Left" Height="505" Margin="0,46,0,0" Grid.Row="0" VerticalAlignment="Top" Width="1152" Name="MainDataGrid" CellEditEnding="MainDataGridCellEditEnding" SelectedCellsChanged="MainDataGridSelectedCellsChanged" AutoGenerateColumns="False" PreviewMouseLeftButtonUp="MainDataGrid_PreviewMouseLeftButtonUp">
<DataGrid.Resources>
<ContextMenu x:Key="CellContextMenu">
<MenuItem Name="DataGridUndoMenuItem" Command="Undo" Header="Undo">
<MenuItem.Icon>
<Image Source="Images/Undo.png" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Command="Redo" Header="Redo">
<MenuItem.Icon>
<Image Source="Images/Redo.png" />
</MenuItem.Icon>
</MenuItem>
<Separator/>
<MenuItem Command="Cut" Header="Cut">
<MenuItem.Icon>
<Image Source="Images/Cut.png" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Command="Copy" Header="Copy">
<MenuItem.Icon>
<Image Source="Images/Copy.png" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Command="Paste" Header="Paste">
<MenuItem.Icon>
<Image Source="Images/Paste.png" />
</MenuItem.Icon>
</MenuItem>
<Separator></Separator>
<MenuItem Header="Add in Translation Database" Click="AddInTranslationDtbMenuClick"></MenuItem>
<MenuItem Header="Search in Translation Database" Click="SearchInTranslationDatabase"></MenuItem>
</ContextMenu>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Width="30" Binding="{Binding Path=Number, Mode=TwoWay, NotifyOnTargetUpdated=True}" Header="№" IsReadOnly="True"></DataGridTextColumn>
<DataGridTextColumn Width="40*" Binding="{Binding Path=Tags, Mode=TwoWay, NotifyOnTargetUpdated=True}" Header="Tags" IsReadOnly="True">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Background" Value="{Binding Path=Tags, Converter={StaticResource TagsToBrushConvecter}}"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Width="250*" Binding="{Binding Path=ID, Mode=TwoWay}" Header="ID">
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox">
<Setter Property="ContextMenu" Value="{StaticResource CellContextMenu}"/>
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Width="390*" Binding="{Binding Path=OriginalText, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True}" Header="Original">
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox">
<Setter Property="ContextMenu" Value="{StaticResource CellContextMenu}"/>
</Style>
</DataGridTextColumn.EditingElementStyle></DataGridTextColumn>
<DataGridTextColumn Width="390*" Binding="{Binding Path=Translation, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True}" Header="Translation">
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox">
<Setter Property="ContextMenu" Value="{StaticResource CellContextMenu}"/>
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<TextBox SpellCheck.IsEnabled="True" HorizontalAlignment="Left" Height="75" Margin="1,26,0,0" Grid.Row="1" TextWrapping="Wrap" Text="{Binding ElementName=MainDataGrid, Path=SelectedItem.Translation, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" VerticalAlignment="Top" Width="1152" Name="TranslationTextBox" TextChanged="TranslationTextBoxTextChanged" ContextMenuOpening="TranslationTextBoxContextMenuOpening">
<TextBox.ContextMenu>
<ContextMenu>
<MenuItem Command="Undo" Header="Undo">
<MenuItem.Icon>
<Image Source="Images/Undo.png" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Command="Redo" Header="Redo">
<MenuItem.Icon>
<Image Source="Images/Redo.png" />
</MenuItem.Icon>
</MenuItem>
<Separator/>
<MenuItem Command="Cut" Header="Cut">
<MenuItem.Icon>
<Image Source="Images/Cut.png" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Command="Copy" Header="Copy">
<MenuItem.Icon>
<Image Source="Images/Copy.png" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Command="Paste" Header="Paste">
<MenuItem.Icon>
<Image Source="Images/Paste.png" />
</MenuItem.Icon>
</MenuItem>
<Separator></Separator>
<MenuItem Command="SelectAll" Header="Select All">
<MenuItem.Icon>
<Image Source="Images/SelestAll.png" />
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</TextBox.ContextMenu>
<!--<SpellCheck.CustomDictionaries>
<System:Uri>pack://application:,,,/Dictionaries/Russian.lex</System:Uri>
</SpellCheck.CustomDictionaries>-->
</TextBox>
代码通过 ObservableCollection 填充一个表:
MainDataGrid.ItemsSource = Rows; //ObservableCollection<CRow> Rows
但是当我更改 TranslationTextBox 中的文本时,只有在我开始编辑单元格时才会出现 DataGrid 更改。如何解决?
Crow代码:
public enum RowSearchMode { IgnoreCase, MatchCase, Regex }
public class CRow : INotifyPropertyChanged
{
public int Number { get; set; }
public string Tags { get; set; }
public string ID { get; set; }
public string OriginalText { get; set; }
public string Translation { get; set; }
public CRow(int iNumber, string strID, string strOriginalText, string strTranslation)
{
Number = iNumber;
ID = strID;
OriginalText = strOriginalText;
Translation = strTranslation;
Tags = "";
}
public CRow():this(0,"","",""){}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string strName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(strName));
}
}
public bool Contrains(string strText, RowSearchMode hMode = RowSearchMode.IgnoreCase)
{
switch (hMode)
{
case RowSearchMode.MatchCase:
return ID.Contains(strText) || OriginalText.Contains(strText) || Translation.Contains(strText);
case RowSearchMode.IgnoreCase:
return ID.ToLower().Contains(strText.ToLower()) || OriginalText.ToLower().Contains(strText.ToLower()) || Translation.ToLower().Contains(strText.ToLower());
case RowSearchMode.Regex:
return Regex.IsMatch(ID, strText) || Regex.IsMatch(OriginalText, strText) || Regex.IsMatch(Translation, strText);
}
return false;
}
public void Replace(string strOldText, string strNewText, RowSearchMode hMode = RowSearchMode.IgnoreCase)
{
switch (hMode)
{
case RowSearchMode.MatchCase:
ID = ID.Replace(strOldText, strNewText);
Translation = Translation.Replace(strOldText, strNewText);
OriginalText = OriginalText.Replace(strOldText, strNewText);
break;
case RowSearchMode.IgnoreCase:
ID = ID.Replace(strOldText, strNewText);
Translation = Translation.Replace(strOldText, strNewText);
OriginalText = OriginalText.Replace(strOldText, strNewText);
break;
case RowSearchMode.Regex:
var hRegex = new Regex(strOldText);
ID = hRegex.Replace(ID, strNewText);
Translation = hRegex.Replace(Translation, strNewText);
OriginalText = hRegex.Replace(OriginalText, strNewText);
break;
}
}
}