Bin
❔ WPF binding expression of property from ItemsControl item with viewmodel property
Lets say I need to draw list of circles in WPF using ItemsControl and put it in Canvas so i can position each individual circles freely. Each circle has position property, that is the actual position of the circle. Then in my viewmodel, i have PositionOffset property that whatever the Canvas item position should be offsetted by that.
For example i have a circle at position (5,2) and my viewmodel PositionOffset is (10,10), then i want that circle to be positioned at (15,12) in the canvas of ItemsControl. So yes, this means in my itemscontrol.itemtemplate, when i need to set the property of Canvas.Left etc, i need to do something like {Binding Position.X + PositionOffset.X}.
How do i do this? Below is the example of the code.
2 replies
Microsoft.Xaml.Behaviors.Wpf MouseMove event passing event args to command is passing null
I have wpf application view. It is capturing MouseMove event, which is sending MouseEventArgs as the argument of the event.
Here i am handling that event using Microsoft.Xaml.Behaviors.Wpf inside <i:InvokeCommandAction> tag of the XAML file, wire it to my command of my viewmodel, and setting the PassEventArgsToCommand property to true.
This means the MouseEventArgs from the event should be passed to my command argument. However, when i'm trying to run it and when it invokes the command (when i move the mouse), the command argument is null and ofc it throws nullreference exception when im dereferencing the argument. My guess this is a bug unless if im missing something. So how to fix the event to not pass null to my command? (which should pass MouseEventArgs)
My view (only relevant lines shown here)
My viewmodel, which has the command (only relevant lines shown here)
3 replies
WPF cannot capture custom event using Microsoft.Xaml.Behaviors.Wpf (solved)
Suppose i have a project that raise an event. When i click on a button, obviously "Click" event will be raised. Inside the click handler method, i raised a custom event called "FileOpen" event.
I have successfully captured "Click" event by testing whether MessageBox popped or not. However, it fails to capture "FileOpen" custom event, it doesn't pop another MessageBox that tells "FileOpen event captured".
I'm guessing its because i put the wrong name (maybe wrong format, e.g must be fully qualified name or so) in the XAML of line
<i:EventTrigger EventName="FileOpen">
. So how do i capture this custom event?
XAML for the button:
12 replies
Wpf how to send custom event (or RoutedEvent) from parent to all childs that implement the handler.
Hi guys, as the title says, i want to send an event from parent control to all childrens that implement the handler of that type of event. The child itself is a UserControl, which then the UserControl will handle it.
In this project, i am using an MVVM pattern. I have read a little bit about RoutedEvent and some related terminologies such as direct, bubbling, and tunneling event (and still don't know how to use that knowledge, just ignore if it doesn't affect this question).
Let's say that i have a button that when clicked, it will raise an event (or notify somehow) the parent that the user wants to open a file. Then, the parent will send this event to all of its childrens that implement the handler (similar to tunneling routed event).
If possible, i don't want to manually set every kind of children types (in the definition of the code-behind) to listen to the specific parent like finding the parent control then listen to the event of that parent control (e.g Parent.EventName += this.HandlerName).
In WPF, If i'm not wrongly understood, the children should automatically listen to the event when we declaring the control in XAML and setting the attribute SomeEventName="HandlerMethodName" right? e.g Click="HandlerMethodName".
So again, how do i automatically make the child to listen to specific parent event (any parent, as long the event type matches), then the parent will send the event to all childs that implement the handler?
Sorry if my message is too long. I just want to be as clear as possible of what i want and what i have know
Example project:
Below screenshot is a very simple project that has a button and a UserControl. When the user clicked on the button, the button raised the parent event (for example the window itself). Then i want the UserControl to listen to this parent event. The UserControl will handle this event, and change the TextBlock content based on the event argument.
9 replies