views:

294

answers:

2

I have a WPF textBox that is declared as ReadOnly

<TextBox IsReadOnly="True" IsTabStop="False" Width="200" />

So, User can not write anything into the TextBox or delete the content but it still allows user to drag the text from this textbox and drop it in any other textbox that caused the text removed from the first textbox(the read-only one) and thats unexpected. Is it a bug? how can I get rid of this?

A: 

I tried the following code :

<StackPanel>
    <TextBox IsReadOnly="True">Hello</TextBox>
    <TextBox></TextBox>
</StackPanel>

When I drag-and-drop the text (after selection) from the first TexbtBox to the second one, the text is copied, but not removed from the first TextBox. I tried it under .NET 3.5 and .NET 4.0 targets.

If you want to get rid of your bug without trying to understand it (since it shouldn't happen), you can just put an empty control (Canvas will be ok) on top of your TextBox, with its Background property set to transparent.

<Grid>
    <TextBox IsReadOnly="True" IsTabStop="False" Width="200" />
    <Canvas Background="Transparent"/>
</Grid>

But the text won't be selectable anymore...

Aurélien Ribon
in my Case it get removed from the first textbox and dropped to the second, When I drag-and-drop the text (after selection) from the first TextBox to the second one, Your trick is nice and may work, But I was expecting something with textbox itself to disable this behavior!!
viky
I tried to set IsReadOnly to False, and now the text is removed when I drag drop it. So you may be changing this attribute in your code without knowing maybe...Try to create a new project, and do the small test I described on top of my post. If the text is removed, you have a serious .NET problem, if not, the solution may reside in your code...
Aurélien Ribon
finally I got the reason. I am binding Text Property of TextBox with Some dependency property say MyText. I am using CoerceValueCallback mechanism and in this handler I am putting validation on MyText property and updating it to blank in case validation fails or update it with some other value in case of validation succeded. The problem was occurring since My handler get called whenever I was doing a Drag and drop of text from textbox. And in the handler MyText property goes through all validation logic and fails since its not that expected and set to blank.
viky
But I couldn't find why coercevaluecallback get called while drag and drop? can you tell me the reason?
viky
A: 

Add html/javascript calls in the textbox

IE onDrop="blur();return false;" onDrag event exists too

Disclaimer: Not tested on all platforms ...

http://www.webreference.com/programming/javascript/dragdropie/

Jean-Francois Lanouette