I am creating a Window control by extending the ContentControl and overriding OnApplyTemplate and creating a template in generic.xaml
"public class MyWindow : ContentControl"
Works great. But now I tried to make it dragable. Seemed to be simple. But it turns out it's almost impossible to modify the properties of the window from within the Window itself. If I attach a method to MouseLeftButtonDown with the following contents:
Canvas.SetLeft(_popupElement, 999);
or
Canvas.SetLeft(this, 999);
The code runs, but nothing happens. I have been trying TranslateTransforms,even other properties such as Visibility.
(I got it from GetTemplateChild(PopupElement) as Popup; in OnApplyTemplate, and it is not null. I am actually able to use _PopupElement.IsOpen). I have even tried to encapsulate the value I want to change as a DependencyProperty, and from it's LeftAdjustmentChanged-method,
move the element.
public static readonly DependencyProperty LeftAdjustmentProperty = DependencyProperty.Register("LeftAdjustment", typeof(double), typeof(MyWindow), new PropertyMetadata(new PropertyChangedCallback(LeftAdjustmentChanged)));
...everything to no avail. I will clarify if this was a bit unclear. I have been awake way too long trying to solve this.
eventhandler offer a object sender parameter, i would use that one but thats still not the problem, u need to listen to the mousemove event and update the elementposition due to that eventhandler
there are quite a few useful examples in this forum
a small snipped from me:
your dragndropelement.mouseleftbuttondown += new mouseleft...blabla
{
sender.MouseMove +=
new
MouseEventHandler(sender_MouseMove);
//and a mouseleftbutton up event for dropping your element.
}
in sender_mousemove:
{
Point point = e.GetPosition(wrapper);//wrapper is the canvas where i move the element on
UpdateElementPosition(sender
as
FrameworkElement, point);
...
}
void UpdateElementPosition(FrameworkElement element,
Point mousePoint)
just the mouseup can be a little nasty, when u drag and drop it into a grid or something,.. then u need to count how many times the actual position fits into the width and height of the gridelements, and set the column and row property depending
on that
Thank you for your answer silverstarter. But unfortunately that is not exactly what I'm looking for. I am actually completely unable to modify properties, such as Canvas.Top and Canvas.Left for elements defined in generic.xaml for classes that inherits ContentControl.
This is my main problem. Canvas.SetLeft(); doens't do aything. element.Visibility = Visibility.Collapsed; for example, doesn't do a thing either.
xyzi
Member
26 Points
15 Posts
Cannot modify internal elements from inside Custom ContentControl
Jan 26, 2009 07:50 AM | LINK
silverstarter
Member
218 Points
147 Posts
Re: Cannot modify internal elements from inside Custom ContentControl
Jan 26, 2009 12:16 PM | LINK
eventhandler offer a object sender parameter, i would use that one but thats still not the problem, u need to listen to the mousemove event and update the elementposition due to that eventhandler
there are quite a few useful examples in this forum
a small snipped from me:
your dragndropelement.mouseleftbuttondown += new mouseleft...blabla
{
sender.MouseMove +=
new MouseEventHandler(sender_MouseMove); //and a mouseleftbutton up event for dropping your element. }in sender_mousemove:
{
Point point = e.GetPosition(wrapper);//wrapper is the canvas where i move the element onUpdateElementPosition(sender
as FrameworkElement, point);...
}
void UpdateElementPosition(FrameworkElement element, Point mousePoint){
Canvas.SetLeft(element, mousePoint.X); Canvas.SetTop(element, mousePoint.Y); }just the mouseup can be a little nasty, when u drag and drop it into a grid or something,.. then u need to count how many times the actual position fits into the width and height of the gridelements, and set the column and row property depending on that
hope it helps or give at least some useful ideas
silverstarter
xyzi
Member
26 Points
15 Posts
Re: Re: Cannot modify internal elements from inside Custom ContentControl
Jan 26, 2009 12:28 PM | LINK
silverstarter
Member
218 Points
147 Posts
Re: Re: Cannot modify internal elements from inside Custom ContentControl
Jan 26, 2009 01:12 PM | LINK
aaah now i see what your problem is turning around ;)
may simply replace usercontrol with canvas or grid or whatever u want to use,
may even frameworkelement, then u can add it as a (in)visible part of a parentelement.
like your object (of type canvas or whatever) is named myobj
the mainelement is a canvas named maincanv
maincanv.children.add(myobj);
when u type myobj.visibilty= visibility.collapsed; i ll totaly disappear untill u set it back to visibility.visible;
xyzi
Member
26 Points
15 Posts
Re: Re: Re: Cannot modify internal elements from inside Custom ContentControl
Jan 26, 2009 06:06 PM | LINK