Bin
Bin
CC#
Created by Bin on 11/19/2022 in #help
❔ 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.
public class CircleModel
{
public Point Position { get; set; }
public Brush Color { get; set; }
}

public class WindowViewModel
{
public Point PositionOffset { get; set; }
public ObservableCollection<CircleModel> Circles = new();
}
public class CircleModel
{
public Point Position { get; set; }
public Brush Color { get; set; }
}

public class WindowViewModel
{
public Point PositionOffset { get; set; }
public ObservableCollection<CircleModel> Circles = new();
}
<ItemsControl ItemsSource="{Binding Circles}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Rectangle Width="512" Height="512" Fill="{Binding Color}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<!--HOW TO OFFSET X AND Y BY "WindowViewModel.PositionOffset"!? Below is definitely NOT syntatically correct-->
<Setter Property="Canvas.Left" Value="{Binding Position.X + PositionOffset.X}"/>
<Setter Property="Canvas.Top" Value="{Binding Position.Y + PositionOffset.Y}"/>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
<ItemsControl ItemsSource="{Binding Circles}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Rectangle Width="512" Height="512" Fill="{Binding Color}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<!--HOW TO OFFSET X AND Y BY "WindowViewModel.PositionOffset"!? Below is definitely NOT syntatically correct-->
<Setter Property="Canvas.Left" Value="{Binding Position.X + PositionOffset.X}"/>
<Setter Property="Canvas.Top" Value="{Binding Position.Y + PositionOffset.Y}"/>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
2 replies
CC#
Created by Bin on 11/16/2022 in #help
❔ Multithreaded application blocked a lot (no locking) [Answered]
14 replies
CC#
Created by Bin on 11/6/2022 in #help
Wpf choppy UI thread performance when launched without debugging
3 replies
CC#
Created by Bin on 10/10/2022 in #help
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)
<i:EventTrigger EventName="MouseMove">
<i:InvokeCommandAction Command="{Binding MouseMoveCommand}"
PassEventArgsToCommand="True"/>
</i:EventTrigger>
<i:EventTrigger EventName="MouseMove">
<i:InvokeCommandAction Command="{Binding MouseMoveCommand}"
PassEventArgsToCommand="True"/>
</i:EventTrigger>
My viewmodel, which has the command (only relevant lines shown here)
private void OnMouseMove(object? arg)
{
MouseEventArgs e = (MouseEventArgs)arg; // args or e is null, here i need that MouseEventArgs
}
private void OnMouseMove(object? arg)
{
MouseEventArgs e = (MouseEventArgs)arg; // args or e is null, here i need that MouseEventArgs
}
3 replies
CC#
Created by Bin on 9/24/2022 in #help
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:
<Button x:Name="MyButton" Content="Click to fire FileOpen event">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding ClickCommand}"/>
</i:EventTrigger>

<i:EventTrigger EventName="FileOpen">
<i:InvokeCommandAction Command="{Binding FileOpenCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<Button x:Name="MyButton" Content="Click to fire FileOpen event">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding ClickCommand}"/>
</i:EventTrigger>

<i:EventTrigger EventName="FileOpen">
<i:InvokeCommandAction Command="{Binding FileOpenCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
12 replies
CC#
Created by Bin on 9/22/2022 in #help
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
CC#
Created by Bin on 9/10/2022 in #help
Wpf won't compile because SomeClass do not exist in SomeNamespace (but it actually exist!) (solved)
29 replies