|
|
I' m building a sample in which I want to move an object. I'm not fully familiar with Blend and Xaml and the C# code behind, but I really don't understand why my cursor is far away from the object (is it canvas?... I don't know, in fact I'm shooting in the dark).
Here' s my xaml:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="CanvasMove.MainPage"
Width="640" Height="480">
<Grid x:Name="LayoutRoot" Background="White">
<Canvas Height="80" HorizontalAlignment="Left" Margin="100,100,0,0" VerticalAlignment="Top" Width="80">
<Rectangle Fill="#FF80C1E4" Height="80" Width="80" RenderTransformOrigin="0.5,0.5" MouseLeftButtonUp="CanvasUp" MouseLeftButtonDown="CanvasDown"/>
<Rectangle Fill="#FF7AB7D8" Height="32" Width="32" Canvas.Left="24" Canvas.Top="24"/>
<Canvas.RenderTransform>
<TranslateTransform X="0" Y="0" x:Name="CanvasTransform"/>
</Canvas.RenderTransform>
</Canvas>
</Grid>
</UserControl>
Here's my C#:
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
CanvasTransform.X = e.GetPosition(null).X;
CanvasTransform.Y = e.GetPosition(null).Y;
}
|
|
|
|
|
TCPMem wrote: I really don't understand why my cursor is far away from the object
You are responding to all the MouseMove events for the UserControl.
When you call e.GetPosition(null) you are getting the cursor position
relative to the entire Silverlight plugin.
Your Canvas is 100 pixels down and to the right of the plugin. The
Rectangle you are moving is another 24 pixels down and to the right
of the Canvas.
So the cursor is always 124x124 away from the object.
What are you trying to do? Do you want to be able to drag the
rectangle with the mouse by clicking on it?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
"What are you trying to do? Do you want to be able to drag the
rectangle with the mouse by clicking on it?"
Yes. That's exactly what I want. How can I make it?
|
|
|
|
|
TCPMem wrote: That's exactly what I want. How can I make it?
First, I misread your code so I was wrong about how far off the cursor
was from the Canvas. I now see you're moving the entire canvas, not just
the rectangle, which is the way I read it
Anyway, here's a simple example that will hopefully get you going:
<Grid x:Name="LayoutRoot" Background="White">
<Canvas Height="80" HorizontalAlignment="Left" Margin="100,100,0,0" VerticalAlignment="Top" Width="80"
MouseLeftButtonUp="Canvas_MouseLeftButtonUp"
MouseMove="Canvas_MouseMove"
MouseLeftButtonDown="Canvas_MouseLeftButtonDown" >
<Rectangle Fill="#FF80C1E4" Height="80" Width="80" RenderTransformOrigin="0.5,0.5" />
<Rectangle Fill="#FF7AB7D8" Height="32" Width="32" Canvas.Left="24" Canvas.Top="24"/>
<Canvas.RenderTransform>
<TranslateTransform X="0" Y="0" x:Name="CanvasTransform"/>
</Canvas.RenderTransform>
</Canvas>
</Grid>
Point anchorPoint;
bool isInDrag = false;
private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
anchorPoint = e.GetPosition(null);
element.CaptureMouse();
isInDrag = true;
}
private void Canvas_MouseMove(object sender, MouseEventArgs e)
{
if (isInDrag)
{
Point currentPoint = e.GetPosition(null);
CanvasTransform.X = CanvasTransform.X + currentPoint.X - anchorPoint.X;
CanvasTransform.Y = CanvasTransform.Y + currentPoint.Y - anchorPoint.Y;
anchorPoint = currentPoint;
}
}
private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (isInDrag)
{
FrameworkElement element = sender as FrameworkElement;
element.ReleaseMouseCapture();
isInDrag = false;
}
}
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I used http://msdn.microsoft.com/en-us/library/cc189066(VS.95).aspx this code to move an object only and wondered how can I do this with the entire canvas. I took a look at your code and replaced
Rectangle item = sender as Rectangle;
with yours:
FrameworkElement element = sender as FrameworkElement;
and it happened. Thanks!
|
|
|
|
|
I need to determine whether the FlowDocument inside a RichTexBox fits without exceeding the height of the RichTextBox. If I could get an event that announces that the vertical scroll bar is visible when VerticalScrollBarVisibility="Auto" , that would do the trick, but I haven't been able to discover how to do that. Likewise a property giving me the computed height of the content would also do it, but I don't seem to be able to get that, either. Any other way? What I want to do is to be able to bring up a resizable window containing the content if the content exceeds the actual height and width of the RichTextBox.
|
|
|
|
|
I believe you can see if the control "requires scrolling" after setting the content. Sorry I can't be more detailed in my answer, but it was a few months ago that I was dealing with scrolling.
EDIT - You may have to add a handler for the ContentRendered event for the control because the necessary properties may not be set until the content is rendered. I'm not sure about this though.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
modified on Sunday, November 1, 2009 7:18 AM
|
|
|
|
|
John Simmons / outlaw programmer wrote: I believe you can see if the control "requires scrolling" after setting the content.
The "content" of a RichTextBox is a FlowDocument which is a ContentElement, but I don't see any properties that specify whether a ContentElement "requires scrolling." Likewise for the derived class, RichTextBox. What event could I look for that would tell me that?
John Simmons / outlaw programmer wrote: You may have to add a handler for the ContentRendered event for the control because the necessary properties may not be set until the content is rendered. I'm not sure about this though.
In MSDN, I could only find two classes that receive a ContentRendered event: Window and Frame, and RichTextBox isn't derived from either, and so the ContentRendered event doesn't show up as an event that RichTextBox can receive.
If there were a way to get the ScrollViewer from the RichTextBox, that would give me something else to investigate, but I don't see how you can do that, either.
|
|
|
|
|
If you knew the name of the scroller element, you could get it that way. I believe it's "PART_ContentHost".
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
John Simmons / outlaw programmer wrote: If you knew the name of the scroller element, you could get it that way. I believe it's "PART_ContentHost".
You're getting into an area that is obscure to me. If the name of my RichTextBox is richTB , my first guess for getting at the scroller element was this:
ScrollViewer sv = richTB.PART_ContentHost;
But it didn't compile. How would I code the line that retrieves the ScrollViewer control? And in general, how do you find out the names of all the components of a control? That would enable me to verify whether PART_ContentHost is indeed the name of the RichTextBox ScrollViewer.
|
|
|
|
|
Try this:
ScrollViewer sv = (ScrollViewer)richBox.Template.FindName("PART_ContentHost", richBox);
You can't do this in the form's constructor. Either do it in the form's Loaded or ContentRendered handler.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Thank you very much! That worked! I learned something new today. Now I have to do two things:
1. See if I can find out from the ScrollViewer whether its content fits within the its vertical height. I'll report my success or failure at this attempt.
2. Determine whether I want to pursue this approach at all! After struggling with this for several hours before I got any responses to my original post (only from you so far), I worked through another path that didn't require this information, so I have to revisit my original idea to see whether it is better than the new path I've taken, which is pretty slick and clean. But I'll certainly pursue #1 at least to the point of whether I can find out if the content fits within the RichTextBox. In general, that would be good information to know.
|
|
|
|
|
fjparisIII wrote: Now I have to do two things:
1. See if I can find out from the ScrollViewer whether its content fits within the its vertical height. I'll report my success or failure at this attempt.
and I have been successful! I've found two ways of determining whether the content can fit into the RichTextBox, using the following code:
private void richTB_MouseEnter(object sender, MouseEventArgs e)
{
ScrollViewer sv = (ScrollViewer)richTB.Template.FindName("PART_ContentHost", richTB);
double height = sv.ActualHeight;
double extentHeight = sv.ExtentHeight;
double scrollableHeight = sv.ScrollableHeight;
}
If extentHeight > height , the content doesn't fit. Or, if scrollableHeight == 0 , the content does fit!
Now I have to figure out if I actually want to use this information! Fortunately, I think it will be possible for me to use the information on top of the solution to the original problem I had, to make things even better!
Now if only we can find a dictionary up on MSDN for all these template parts (like ("PART_ContentHost"), the world would be ours! Like you, I failed to find it Googling. I searched for "PART_ContentHost" hoping to find the dictionary, and only came up with examples of using this particular part. There must be such a dictionary on MSDN. Otherwise how would anyone find out what these names are in the first place, inside information? Maybe there's a technique for getting them for any particular element through the System.Reflection namespace, something which, sad to say, I've never studied.
|
|
|
|
|
There's a download available that provides all of the standard element templates, but I can't find the link. If you post a message here that asks for the link, I'm sure someone will have it. I have the download, and that's how I verified the name of the element you were looking for.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
fjparisIII wrote: Fortunately, I think it will be possible for me to use the information on top of the solution to the original problem I had, to make things even better!
This is one of those jump-up-and-tell-someone-about-your-mastery-of-the-technology moments, and then you realize that nobody either cares or would understand when you explained the subtleties. Truly a bittersweet feeling.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
BTW, you can determine the names of the standard WPF components by finding the microsoft-supplied standard object templates download. They're provided by theme, but essentially, they're all the same so you only need to download 1 to get a birds-eye view of the original templates.
I tried for about a half hour to find them on the web, but my google search foo is severely diminished today.
Maybe someone else can provide the link.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Can I use the MS ribbonbar in .Net 3.5?
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
There's a ribbon bar control ? Where does it come from ?
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
Yeah, it's a separate CTP. You have to "license" the MS Fluent User UI to get to the download (a single DLL).
Start here:
http://www.codeplex.com/wikipage?ProjectName=wpf&title=WPF%20Ribbon%20Preview[^]
...and follow the links.
BTW, once you download it, all you gotta do is include it in your tools panel, and you get it all.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Last I heard, it will be included in 4.0, and a preview is available for 3.5 SP1.
WPF Ribbon Preview[^]
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
How depressing. So now everyone can have sucky UI ?
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
Christian Graus wrote: So now everyone can have sucky UI ?
Hehe yeah.
I hate that thing.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I don't like it either, but sooner or latter (and probably more towards sooner), someone where you work is gonna put a bug in your boss' ear about it. I became more interested in it when I noticed that Texas Med Clinic had a custom app written for them, and it had a ribbonbar. What I realized is that if it's a new application that you've never seen before, it actually does help in the navigation of said program. The only thing wrong with the current implementations I've seen is that there doesn't seem to be an ability to get rid of it and go back to a UI you're more comfortable with.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Hello,
I have made a property in a Silverlight project on the Page.
How can I call this propery in the code behind page of ASP.net(c#)?
Thanks,
|
|
|
|