Ciao a tutti!
era da un pò che non passavo di qui, poi ho visto questo post e mi pareva giusto rispondere a Pietro che mi è simpatico(ciao Pietro).
questo codice crea un controllo (datagrid) che inserisce le righe copiate da un excel (testato con excel 2007). Quando si passa sopra il controllo con il mouse nell'angolo dx basso appare un immagine (da importare nel progetto) che consente, facendoci click sopra, di importare il contenuto testuale degli appunti. l'immagine rimane per 4 secondi poi scompare con animazione (anche quando si esce dal controllo scompare, e ricompare quando si rientra sempre per 4 secondi con animazione), a meno che non si posizioni il mouse sopra di esso, ricompare anche quando si porta il mouse nell'angolo dx basso del controllo e riscompare quando si esce da esso. Credo sia meglio provare per capire, ma vedrai che quando serve c'è e quando non serve no!. è possibile copiare anche le colonne oltre i dati, oppure se non vengono copiati anche i nomi delle colonne si può optare per non visualizzarle (IncludeHeader = false).
Per questo controllo è necessari che l'utente accetti (tramite messageBox di silverlight) che il controllo possa accedere alla clipboard (impostazioni di sicurezza di SL), ma può mettere il check per non chiedere più questa impostazione.
ecco finalmente il codice, divertiti
code behind (MainPage.xaml.cs)
xaml (MainPage.xaml)codice:using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Text.RegularExpressions; using System.Windows.Data; using System.Windows.Threading; using System.Windows.Input; namespace ClipboardGrab { public partial class MainPage : UserControl { bool IncludeHeader; DispatcherTimer myDispatcherTimer = new DispatcherTimer(); public MainPage() { InitializeComponent(); //Imposta il tempo in cui rimane visibile il bottone per incollare myDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 4); myDispatcherTimer.Tick += new EventHandler(myDispatcherTimer_Tick); //true se quando si copia sono incluse i nomi delle colonne IncludeHeader = true; ; } void myDispatcherTimer_Tick(object sender, EventArgs e) { ButtonOpacityDown.Begin(); myDispatcherTimer.Stop(); } private void dataGrid2_MouseEnter(object sender, MouseEventArgs e) { ButtonOpacityUp.Begin(); myDispatcherTimer.Start(); } private void dataGrid2_MouseLeave(object sender, MouseEventArgs e) { this.button1.Opacity = 0; myDispatcherTimer.Stop(); } private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { dataGrid2.Columns.Clear(); var txt = Clipboard.GetText(); var rows = Regex.Split(txt, @"\r\n"); var columns = Regex.Split(rows[0], @"\t"); List<Row> table = new List<Row>(); for (int i = 0; i < rows.Length - 1; i++) { Row row = new Row(); string[] items = Regex.Split(rows[i], @"\t"); for (int y = 0; y < columns.Length; y++) { if (i < 1) { this.dataGrid2.Columns.Add(new DataGridTextColumn() { Binding = new Binding(string.Format("[{0}]", columns[y])), Header = IncludeHeader ? columns[y] : string.Empty, Width = DataGridLength.Auto }); if (!IncludeHeader) row[columns[y]] = items[y]; } else { row[columns[y]] = items[y]; } } if (i > 0||!IncludeHeader) table.Add(row); } this.dataGrid2.ItemsSource = table; } private void button1_MouseEnter(object sender, MouseEventArgs e) { ButtonOpacityDown.Stop(); this.myDispatcherTimer.Stop(); this.button1.Opacity = 1; } private void button1_MouseLeave(object sender, MouseEventArgs e) { ButtonOpacityDown.Begin(); } } public class Row { private Dictionary<string, object> columns = new Dictionary<string, object>(); public object this[string ColumnName] { get { if (columns.ContainsKey(ColumnName)) return columns[ColumnName]; else return null; } set { if (columns.ContainsKey(ColumnName)) columns[ColumnName] = value; else columns.Add(ColumnName, value); } } public IEnumerable<string> Keys { get { return columns.Keys; } } } }
codice:<UserControl x:Class="ClipboardGrab.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" MouseEnter="dataGrid2_MouseEnter" MouseLeave="dataGrid2_MouseLeave"> <UserControl.Resources> <Storyboard x:Name="ButtonOpacityUp"> <DoubleAnimation Storyboard.TargetName="button1" Storyboard.TargetProperty="Opacity" From="0.0" To="1.0" Duration="0:0:1" AutoReverse="False"/> </Storyboard> <Storyboard x:Name="ButtonOpacityDown"> <DoubleAnimation Storyboard.TargetName="button1" Storyboard.TargetProperty="Opacity" From="1.0" To="0.0" Duration="0:0:1" AutoReverse="False"/> </Storyboard> </UserControl.Resources> <Grid x:Name="LayoutRoot" Background="White"> <sdk:DataGrid AutoGenerateColumns="False" Name="dataGrid2" > </sdk:DataGrid> <Image Source="/ClipboardGrab;component/Images/1350629552_edit-paste.png" MouseEnter="button1_MouseEnter" MouseLeave="button1_MouseLeave" MouseLeftButtonDown="Image_MouseLeftButtonDown" Height="40" Name="button1" HorizontalAlignment="Right" VerticalAlignment="Bottom"/> </Grid> </UserControl>![]()

(ciao Pietro).
. è possibile copiare anche le colonne oltre i dati, oppure se non vengono copiati anche i nomi delle colonne si può optare per non visualizzarle (IncludeHeader = false).
Rispondi quotando
Non so neanche se ne vada la pena a studiarlo (e qui mi aspetto un altro tuo consiglio)