|
derm2 wrote: Starting your mousemove eventhandler with "if the mouse has moved" feels unelegant for me
I guess I read your original post wrong because I saw
"Save the mouse location on previewmousedwn".
It's at that point I'd do hittesting or whatever necessary to
determine if it's the start of a drag situation, and if so, capture
the mouse.
derm2 wrote: I have never worked with a framework that fired mousemove events without the mouse moving before WPF.
I don't know what you're talking about there....there's no randomly fired
move events. Here's a very simple drag and drop example showing this:
<Grid >
<Canvas >
<TextBlock x:Name="textBlock" Text="0" Foreground="Black" />
<Rectangle Canvas.Left="30" Canvas.Top="30" Width="50" Height="50"
Stroke="Red" StrokeThickness="2" Fill="#00000000"
MouseLeftButtonDown="Rectangle_MouseLeftButtonDown"
MouseLeftButtonUp="Rectangle_MouseLeftButtonUp"
MouseMove="Rectangle_MouseMove" />
</Canvas>
</Grid>
Point anchorPoint;
Point currentPoint;
bool isInDrag = false;
int moveCount = 0;
private void Rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
anchorPoint = e.GetPosition(null);
element.CaptureMouse();
isInDrag = true;
e.Handled = true;
}
private void Rectangle_MouseMove(object sender, MouseEventArgs e)
{
moveCount++;
textBlock.Text = moveCount.ToString();
if (isInDrag)
{
FrameworkElement element = sender as FrameworkElement;
currentPoint = e.GetPosition(null);
double x = System.Convert.ToDouble(element.GetValue(Canvas.LeftProperty));
double y = System.Convert.ToDouble(element.GetValue(Canvas.TopProperty));
element.SetValue(Canvas.LeftProperty, x + currentPoint.X - anchorPoint.X);
element.SetValue(Canvas.TopProperty, y + currentPoint.Y - anchorPoint.Y);
anchorPoint = currentPoint;
}
}
private void Rectangle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (isInDrag)
{
FrameworkElement element = sender as FrameworkElement;
element.ReleaseMouseCapture();
isInDrag = false;
e.Handled = true;
}
}
No "fired mousemove events without the mouse moving" that I can see there.
Don't even have to check if the cursor moved because I know it did when I got the event...
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Indeed. Your example doesn't fire that event, but it wouldn't be random if it always happened
Put this onto your form, and expand the root node. Mousemove will be fired, and you will start a drag operation if you don't check the position.
class Tree2 : TreeView
{
public Tree2()
{
TreeViewItem root = new TreeViewItem();
root.Header = "root";
TreeViewItem child = new TreeViewItem();
root.Items.Add(child);
this.Items.Add(root);
this.PreviewMouseDown += new MouseButtonEventHandler(Tree2_PreviewMouseDown);
this.PreviewMouseMove += new MouseEventHandler(Tree2_PreviewMouseMove);
}
Point lastMouseDown;
bool readyToDrag = false;
void Tree2_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
lastMouseDown = e.GetPosition(this);
readyToDrag = true;
}
void Tree2_PreviewMouseMove(object sender, MouseEventArgs e)
{
Point currentPosition = e.GetPosition(this);
if (readyToDrag)
{
if (currentPosition == lastMouseDown)
{
throw new Exception("This is a mousemove event fired for a mouseclick");
}
else
{
}
}
}
}
|
|
|
|
|
derm2 wrote: Put this onto your form, and expand the root node. Mousemove will be fired, and you will start a drag operation if you don't check the position.
That's not random.
Preview events are tunneling events - they go from root towards top of tree.
If you click in the treeview, the treeview gets a peek at the event before
the actual target control. You are responsible for dealing with that.
That's why I stated, and it's documented, that some controls consume the bubbling mouse
events like buttons, for example. Buttons take the bubbling events and convert them to the Click
event....the event is marked handled so an attached handler won't get called. So now one has
to use the tunneling event, which is simple for a standard button, but not so simple for a
treeview, which is a composite control (it is composed of many elements).
In this case you are getting messages meant for "child" elements within the treeview.
That's also why I stated you may have to do your own hittesting in the mousedown handler,
which wasn't necessary in my example, but is necessary in your example.
That hittesting can be as simple as this:
void Tree2_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
TreeView tv = e.Source as TreeView;
if (tv != null)
{
lastMouseDown = e.GetPosition(this);
readyToDrag = true;
}
}
You may need to do the same thing in the mousemove handler, but I personally
would capture the mouse so I know all the move events are mine until the button
is released.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
How best to handle lookup lists, say for a combo box? Do I add another Observable Collection to my main VM? At what point does my VM become too clunky with so many collections? Thanks!
|
|
|
|
|
I tend to define lookups in their own VM. Let the lookup VM take care of managing the logic behind the lookup.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
OK. And then contain an instance of the lookup VM in my main VM, I think? Or instantiate the main VM and instantiate the lookup VM?
|
|
|
|
|
Ugh. And the lookup VM would have to contain a collection of single VMs?
e.g.
PeopleVM
contains collection of
PersonVMs
And I would do this for everything I want to lookup?
|
|
|
|
|
It's the way I like to use it. It keeps everything nice and compact - and easy to test.
|
|
|
|
|
Hello sir
Now I am working on wpf. I am facing a problem. I used dynamic tab. In dynamic tab have N number of controls. These controls are dynamic. I used basically 3 type of control.
1) Label
2) Text box
3) Mask Text box.
When I move the scrollbar then mask text box is moving out of tab control. In case of other controls are working fine.
I am using in xaml code.
1) ScrollViewer (In scrollview use)
2) Grid (In grid use tab control)
3) Tabcontrol (In tab control have dynamice controls i.e. MaskText box, Lable,Textbox
So kindly tell me the solution. To solve this problem.
Thanks
Asit Sinha
Asit Sinha
|
|
|
|
|
Asit Kumar Sinha wrote: When I move the scrollbar then mask text box is moving out of tab control. In case of other controls are working fine.
Assuming you're referring to the scrollbar provided by the
ScrollViewer, the entire Grid and its contents should scroll.
Asit Kumar Sinha wrote: So kindly tell me the solution.
Kindly provide a working XAML example that demonstrates the problem...
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
So, as many of you probably have, I saw Billy Hollis' demo of his StaffLynx application and was enamoured by its "pseudo-web2.0" design. Having student versions of Visual Studio 2008 and the Expression Studio, I sought to do some of the neat stylings that he did. However, I just don't know how he got his windows styled like this:
http://blogs.msdn.com/blogfiles/shanemo/WindowsLiveWriter/BillyHollisonGettingSmartwithWPF_FB8C/image_2.png[^]
I tried creating a stack panel with a border, then having grids within that, but I can't seem to get the sizing and styling correct. If anyone could point me to a resource with the right "best practices" for this sort of thing, or could show me a demo of similar capabilties, I would be most grateful.
As I see it, its like this:
Bordered Stack Panel
|--Grid with title text
|--Grid with search bar
|--Stack Panel
|--Grid with custom listbox (left)
|--Grid with custom listbox (right)
|--Footer
Thoughts?
|
|
|
|
|
pancakesOfMassDeliciousness wrote: Thoughts?
Yes, two.
1) Your username rocks!
2) Your visual tree outline looks like a good start to me.
Which elements are you having trouble sizing?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
StackPanels cause people a lot of trouble. They only stretch to fill the screen one way, depending on the Orientation. I might do something more like this:
<DockPanel>
<TitleStuff DockPanel.Dock="Top" />
<!-- put buttons in this panel -->
<!-- or possibly a grid with two rows,
one image, one text -->
<StackPanel DockPanel.Dock="Bottom"
Orientation="Horizontal"
HorizontalAlignment="Center" />
<DockPanel Margin="???">
<SubTitle DockPanel.Dock="Top" />
<SearchBar DockPanel.Dock="Top" />
<Add/Edit/Delete DockPanel.Dock="Bottom" />
<Grid>
<!-- rest of stuff here, probably 2 columns,
3 rows. left list box spans bottom two rows -->
</Grid>
</DockPanel>
</DockPanel>
|
|
|
|
|
Hello,
Resurecting an old post here, but did you manage to build that kind of shape or at least find any valid source of information.
I would be pretty interested too.
Thanks in advance for your answer.
Seb
|
|
|
|
|
Nah, never found much of anything.
In the end, I had to concentrate on working on the back-end of my project (Db, services, etc.) and have not yet gotten to the point where I even need to worry about WPF...
If you come across anything, please keep me posted as well.
BTW -- The actual StaffLynx app that Billy Hollis created can be purchased now. Wow: http://www.sommet.com/technology_stafflynx.php[^]
|
|
|
|
|
Thanks for the quick answer.
I think I'll try doing something myself because I didn't manage to find some valid information source.
I'll keep you posted.
Seb
|
|
|
|
|
Why WPF
To build Desktop applications, Microsoft came up with numerous APIs over the years, like Win32 API, MFC, VB 6.0 and lately .NET Winform.
Typically in a full fledged desktop application, you would like to have diverse User interfaces like controls, 2D Graphics, 3D-Graphics and may be optionally (say by clicking on Advanced.. button) you want to have Media and Animation.
Looking at above APIs from Microsoft, It is difficult for a Windows Programmer to master diverse nature of each API for a particular User interface.
Apart from this, in all above technologies more or less the overall Layout was fixed. In other words, Layout couldn't adapt itself easily to different window sizes.
Finally, In all above technologies, there is no way to separate the Graphical contents from code. Say e.g in WinForm Apps, when you drag and drop a control on a form surface framework generates C# code. It also means that to change the look and feel of any control you will have to deal with code.
This is where Microsoft came up with 'Windows Presentation Foundation' (WPF)
Windows Presentation Foundation is a one of the .NET components which allows you to develop the Next Generation Desktop Application with very rich user interfaces.
WPF is available from .NET 3.0 onwards. It is one of the components of .NET 3.0 and .NET 3.5 frameworks
-------------------------------------------------------------------------------------------------
WPF Features:
1) Single Unified programming Model for different User Interfaces like 2D Graphics, 3D Graphics, Media, Document API and Animation
2) Declarative User interface through XAML
3) Separation of Business logic from UI part through *XAML
4) Flexible Layout Model (no use of coordinates of control w.r.t to window)
5) Lookless controls (without fixed size)
6) Enhanced Designer and Developer productivity. Designer can work on UI aspect through *XAML file and developer would be busy with corresponding .cs file
7) Powerful data binding model
8) Hardware acceleration. WPF drawing is performed through DirectX.
---------------------------------------------------------------------------------------------------
WPF's basic programming Infrastructure
-> Declarative programming Style using *XAML (Using XAML is optional)
-> Dependency Properties wrapped under normal .NET properties
-> Routed Events
-> Control Commands
---------------------------------------------------------------------------------------------------
*XAML (Zammel) is XML based markup language to instantiate .NET objects
XAML tags are mapped with .net classes, XAML file is compiled into *BAML
BAML is tokenized binary form of XAML and .NET objects are created using this BAML
BAML enhances performance as compare to creating .NET objects through raw parsing of uncompiled XAML file.
---------------------------------------------------------------------------------
Flavors of WPF applications
1) Traditional Desktop Application
2) Navigation based WPF Application - for light weight desktop app
3) XAML browser Application (XBAP) - natively hosted by browser
Now you have correct perspective, programming WPF is the next natural step.
|
|
|
|
|
Errm. Thank you?
I already know this though - perhaps you might want to put it on a blog somewhere, rather than in a forum.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
amazing list of features that were never pain points
|
|
|
|
|
Hi,
How can I open & run my WPF window from a Windows Form?
I created a WPF application project with a Window named "MyWindow".
I also created a Windows Form named "MyForm" in the same project.
Now, from the App.xaml.cs, I am opening MyForm, where one button is there.
On click of that button, my WPF Window should open.
I used the following code:
<br />
MyWindow w = new MyWindow();<br />
w.Show();<br />
So, now on click of the button, WPF window is opening fine.
But, the problem is, when I am closing my WPF Window, the Form is also closing & exiting the application.
How can I solve this problem? My application should not close until I close my Windows Form. Please help...............
|
|
|
|
|
We really need to see how you've opened the form from App.Xaml before we can comment. Basically, we need to see if the form is running as part of the application message pump.
BTW - this is a really strange architecture. Why are you doing this?
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
It sounds like the WinForm is stealing the events (even though it's not the active window).
"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." - Jason Jystad, 10/26/2001
|
|
|
|
|
Set Application.ShutdownMode property to ShutdownMode.OnExplicitShutdown. In WPF, OnMainWindowClose is the default ShutdownMode. The WPF window you open becomes the main window. So closing that window causes the wpf app to shut down unless you explicitly set a different window as main.
|
|
|
|
|
I've noticed a lot of posts on various blogs, etc about this error when referencing image files. Apparently it means that Silverlight can't find your file, or Silverlight doesn't support the format (.gif).
I think like most people experimenting with Silverlight, I haven't formally studied the language, and so I miss many nuances. Here is one where I'm sure it is documented somewhere but I had to learn the hard way.
I had a situation where I was loading an array of image names to be used as a series of ImageBrush elements, so I added the image files to the Properties folder through Blend, and reference them in my code-behind, as...
"Properties/MyImage.png"
however Silverlight didn't recognize them. I had other images referenced the same way in the XAML code and Silverlight had no problem.
It turns out that if you reference an image file (or I assume any other Resource) in the XMAL, Silverlight compiles the Resource into the App.DLL. If they are only referenced in the code-behind this is not the case, so you must include a copy of the Properties folder relative to where the XAP file is run; in this case off my Bin directory.
Hopefully this post will help others avoid my time consuming mistake,
Steve
|
|
|
|
|
If you have static images to embed in your Silverlight app, you can just add
them to your project and set the Build Action property of the image to "Resource".
So, for example, if you add a folder to your project called Images, and add an
image to that folder called Test1.png, you can access it like this:
<!-- From XAML -->
<Image Source="Images/Test1.png" Stretch="None" />
BitmapImage bmp = new BitmapImage(new Uri("yourappmodulename;component/Images/Test1.png", UriKind.Relative));
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|