0

I am having a textblock element in WPF application that is bound to a combobox and I want the textbox text value to be copied to the clipboard when a button is pressed but i am unable to get the text value in the code behind as i am unable to use the textbox name itself to refer to its properties. This is a follow up to my previous question over here Getting XML element from a Combobox item the code is over there so donot want to extend the question these are the simple two lines of code

<TextBox Grid.Column="1" 
    Text="{Binding SelectedItem.Value, ElementName=QueryChooser}" 
    Grid.ColumnSpan="2" Grid.Row="1" Height="200" HorizontalAlignment="Left" 
    Name="textBlock1"  VerticalAlignment="Top" Width="481" />
Community
  • 1
  • 1
JackyBoi
  • 2,695
  • 12
  • 42
  • 74

2 Answers2

1

Firstly, I would recommend looking into the MVVM design pattern if you are serious about doing any WPF development. If you are using MVVM, then use an MVVM framework. It will make your life considerably easier.

Secondly, you need some kind of mechanism for invoking verbs on your data context (a view model in MVVM). WPF provides commanding, and MVVM frameworks provide other techniques and variations.

Once you have this mechanism, then you have the query text in your SelectedQuery property, so you can copy SelectedQuery.Value (a string) to the clipboard.

Community
  • 1
  • 1
devdigital
  • 34,151
  • 9
  • 98
  • 120
  • wow i am reading this link and this will take a really long time for me to understand everything could you pls explain what i have to do for my code at present to get it working? http://msdn.microsoft.com/en-us/magazine/dd419663.aspx – JackyBoi Mar 12 '12 at 15:09
  • If you're doing everything in code behind, then have a look at http://www.c-sharpcorner.com/Resources/705/how-to-add-a-button-click-event-handler-in-wpf.aspx – devdigital Mar 12 '12 at 15:10
  • oh so u want me to initialize the query and everything in the button click handler all over again? cause at first when the applicaiton starts it will start displaying the first item from the xml datasource and there onwards it will show what ever the user chooses from the combobox..chat perhaps? – JackyBoi Mar 12 '12 at 15:13
  • You don't need to initialise your combobox when the button is clicked. You said you need to copy the currently selected query to the clipboard? You have the currently selected query already. The selected item of your combo box is bound to a SelectedQuery property. So access the information you need there in your button click event handler. – devdigital Mar 12 '12 at 15:18
  • private void button3_Click(object sender, RoutedEventArgs e) { string tbvalue; tbvalue = SelectedQuery.ToString(); clipboard mycp = new clipboard(); mycp = tbvalue.ToString(); } "I AM STUCK HERE" getting error at the tbvalue.ToString(); cannot implicitly convert string type to mainwindow.clipboard – JackyBoi Mar 12 '12 at 15:24
  • You need to go back to basics. See http://stackoverflow.com/questions/899350/how-to-copy-the-contents-of-a-string-to-the-clipboard-in-c for copying data to the clipboard. Also, the data you want is SelectedQuery.Value, not SelectedQuery (that is of type Query, not a string). – devdigital Mar 12 '12 at 15:33
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8792/discussion-between-jackyboi-and-devdigital) – JackyBoi Mar 12 '12 at 15:42
  • Clipboard.Clear(); Clipboard.SetText(SelectedQuery.Value); – JackyBoi Mar 12 '12 at 15:48
0

write CopyingCellClipboardContent event in your datagridTemplateColumn in xaml. In code behind in this event write this,

if (dataGrid1.CurrentCell != null && dataGrid1.CurrentCell.Column == e.Column) { dataGrid1.SelectionUnit=Microsoft.Windows.Controls.DataGridSelectionUnit.Cell; e.Content = ((System.Data.DataRowView)(dataGrid1.CurrentCell.Item)).Row.ItemArray[4].ToString(); } else e.Content = true;

Thanks..