Este es un problema de enhebrado. Tenemos que obtener el hilo correcto y ejecutarlo a través de los delegados.
Estoy actualizando mis propiedades a través de un temporizador que transcurre cada 500 ms.Aquí está el código:
public delegate void ClipboarDelegate();
ClipboarDelegate clipboardDelegate = null;
void clipboardTimer_Elapsed(object sender, ElapsedEventArgs e)
{
if (clipboardDelegate == null)
clipboardDelegate = ClipboarDelegateMethod;
//Here we get the right thread, most probably the application thread
Application.Current.Dispatcher.BeginInvoke(clipboardDelegate);
}
public void ClipboarDelegateMethod()
{
try
{
if (Clipboard.ContainsData(DataFormats.Text))
{
//It's important to lock this section
lock (ClipboardString)
{
ClipboardString = Clipboard.GetData(DataFormats.Text) as string;
}
}
}
catch
{ }
}
Por otra parte he hecho un DependencyProperty adecuada con ClipboardString:
public static readonly DependencyProperty ClipboardStringDP =
DependencyProperty.Register("ClipboardString",
typeof(string),
typeof(MainWindow),
new UIPropertyMetadata(string.Empty));
public string ClipboardString
{
get { return (string)this.GetValue(ClipboardStringDP); }
set { this.SetValue(ClipboardStringDP, value); }
}
De esta manera se puede enlazar a mi cuadro de texto en XAML asumiendo mi control o ventana x:Name="_this"
:
<TextBox Name="ClipBoardTextBox"
DataContext="{Binding ElementName=_this}"
Text="{Binding Path=ClipboardString, Mode=OneWay}"/>
¿Cómo está poniendo datos en el portapapeles? –
¿Los datos que se encuentran en el portapapeles son "texto" y no gráficos, audio u otros? – JMD