I have a curious problem. When I copy a cell (or a row) that have spaces in groups of 2 or more, when I paste this spaces are replaced with non-breaking character (Alt+160). What is more curious is that if I paste on Word then copy from Word and paste again on my program the spaces are returned, but if I paste on Excel then copy from Excel and paste on my program the non-breaking characters are still there. Obviously, the easy way is not our way, we need to copy/paste to/from Excel.
I can control on my application the introduction of this character an replace it with spaces, suposing (what is never a good idea) that nobody would introduce non-breaking characters intentionally. But we work with another application in VB 6.0 with more than 200 windows, and I don’t have my boss’s permition for change anything on that app.
I’ve debugged my code and the datagrid doesn’t have the non-breaking character. The clipboard doesn’t have any non-breaking character after the copy. And if I copy the same text from a textbox no spaces are replaced with non-breaking characters at pasting.
this is the axml of one column of any of my datagrids:
<DataGridTextColumn Header="Machine" CanUserSort="True" IsReadOnly="True"
MinWidth="10" Width="*" MaxWidth="Infinity"
Binding="{Binding Path=NOM_MAQ}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="ToolTip" Value="{Binding Path=NOM_MAQ}"/>
<Setter Property="TextTrimming" Value="CharacterEllipsis"/>
<Setter Property="TextWrapping" Value="NoWrap"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
I tried adding to the DataGridTextColumn a converter:
ClipboardContentBinding="{Binding Path=NOM_MAQ,
Converter={StaticResource QuitNonBreakingConverter}}"
class QuitNonBreakingConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
CultureInfo culture)
{
string texto = value.ToString();
StringBuilder sb = new StringBuilder(texto.Length);
foreach (char c in texto.ToCharArray())
{
if (char.Equals(c, System.Convert.ToChar(160)))
{
sb.Append(System.Convert.ToChar(32));
}
else sb.Append(c);
}
return sb.ToString();
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
return value;
}
}
But as I said before, the datagrid nor the clipboard have the non-breaking character, so this converter is futile.
Any idea on how to prevent this replace of characters??
have you looked at all of the data flavors on the clipboard?
the plain string might not have the nonbreaking character, but one of the other more specific kinds might? in word/excel do “paste special” to see what other formats are available, or enumerate them in code.
I’m betting there are multiple kinds of data on the clipboard, and word prefers one and excel favors the other, and you’re getting just the plain text string in your code?