I have this code and I would like to open a popup on a button click for users to input a date along with a date picker and then pass the date as the parameter.
<StackPanel>
<BarContainerControl>
<BarSubItem Content="List">
<BarButtonItem Content="Button" Command="{Binding cnt}"></BarButtonItem>
</BarSubItem>
</BarContainerControl>
</StackPanel>
private void HandleStuff()
{
//Does stuff here
}
I have tried multiple things like PopupBaseEdit and FlyoutControl it didn't work.
There is a bit to do here. It is not too hard if you are just using the code-behind file, and a bit more involved if you are using MVVM.
You will need to declare the Command, implement an Action method for it (along with a second bool-valued function to determine whether it is enabled), instantiate the command linking it to the action and function, and then you can bind to it in your XAML (assuming that the Command is visible in the code-behind /the ViewModel has been set as the DataContext for your Window).
Do you have more of this done than just the little snippets you provided above? If not, there is a lot to do!
The alternative, if you don't care about MVVM and you are just using code-behind, is to attach an event handler in your code-behind to some event (like the Click event?) of your BarButtonItem and launch the DatePicker from there. That's much more in the style of Windows Forms than WPF, which intentionally builds heavily on the MVVM framework.
Related
I have a Window in XAML with some different charts. What I wanted to do is to click that chart and open another window that shows me the same chart and another elements added to the GUI, but I donĀ“t know what I am looking to, I am not sure if MVVM would help me or shoukd I start looking for something else?
You are looking for input bindings:
<ChartPart Background="Transparent">
<ChartPart.InputBindings>
<MouseBinding Command="{Binding ShowInAntoherView}"
CommandParameter="{Binding Path}"
Gesture="LeftDoubleClick""/>
</ChartPart.InputBindings>
(Background=Transparent is important, because if the background is null, control does not pass a check if it was clicked)
This allows you invoke commands with parameters on pretty much all controls on a variety of possible user actions.
Just pass a clicked chart viewModel to a command that will navigate to it - done.
What a <ChartPart> is in your case depends on a library you use (in your case it is Livecharts), but it should not be a problem to figure out.
I have a UWP question about inheriting/ passing a event to a user control from the parent view to child.
I created a user control to display text overlays (see code below). We had a parent view that would display an overlay when the window is resized (see code below). The overlay would display the dimensions of the window when this even is triggered.
I moved the overlay to a user control and now I'm trying to pass that resized event to the overlay control. The hope is that we can register more events to the overlay control so it can display more then the resize
information. However, I'm not sure the best way to do this. My first idea was inheriting from the view, so i could just listen to the event from the overlay control, but that resulted in errors.
I believe due to the fact that the parent view has a ViewModel (i also created one for the overlay, not sure if its actually needed yet).
I have been reading about a lot of possible ways to do this, but I'm not sure which would be the best way to do this. Does anyone have any insight on this issue ? I would be open to suggestions, links, or just a general answer of
what is the best way to achieve this in our project.
Parent view
User Control
Parent Event
Control class
Some information i have been reading about:
https://documentation.devexpress.com/WPF/17449/MVVM-Framework/ViewModels/ViewModel-relationships-ISupportParentViewModel
https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/preview-events
https://social.msdn.microsoft.com/Forums/windows/en-US/742077f6-e875-44d1-8bc4-6e6516db9eda/passing-the-parent-control-event-to-child-controls?forum=winforms
https://learn.microsoft.com/en-us/windows/uwp/xaml-platform/events-and-routed-events-overview
http://irisclasson.com/2013/12/10/passing-event-arguments-from-xaml-in-windows-store-apps-inputconverter-inputconverterparameter-etc/
https://learn.microsoft.com/en-us/windows/uwp/launch-resume/how-to-create-and-consume-an-app-service
Update
Adding the viewModel to the parent viewModel (terminal), and passing it to the control via the Datacontext did not work
As you're already using MVVM, I'd recommend going the full route utilizing "Interactivity", "Commands", and "child ViewModels". This is a commonly used patter in MVVM WPF applications, and can be applied to UWP apps as well.
Using "Interactivity" and interactions
The interactivity / behaviors library from Microsoft allows you to bind events in XAML to an ICommand in the ViewModel. You can get the managed NuGet package here.
From the official examples on GitHub, shortened:
<Button x:Name="button1" Content="Increment">
<Interactivity:Interaction.Behaviors>
<Interactions:EventTriggerBehavior EventName="Click" SourceObject="{Binding ElementName=button1}">
<Interactions:InvokeCommandAction Command="{Binding UpdateCountCommand}"/>
</Interactions:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</Button>
Forward command data to child ViewModel
Having this event now routed to your command in your parent ViewModel, you can now either call your overlay ViewModel and pass the info directly to it:
private readony IOverlayViewModel _overlayViewModel;
public ICommand UpdateCountCommand { get; set; }
ctor(IOverlayViewModel overlayViewModel)
{
_overlayViewModel = overlayViewModel;
UpdatedCountCommand = new MyICommandImplementation(UpdatedCountCommand_Executed);
}
private void UpdatedCountCommand_Executed(/* Add correct method signature */)
{
// If needed, retrieve data from parameter...
// Update overlay ViewModel text
_overlayViewModel.Text = ""; // Whichever text was calculated before
}
Or you use a messenger (mediator pattern) to send it to an overlay.
I was misusing the bindings. x:Bind and Binding are using different types of context. For this binding to work we would need to set the parent's element Datacontext to 'this'. x:Bind on the other hand does this implicitly.
<views:OverlayView DataContext="{x:Bind ViewModel.Overlay}"></views:OverlayView>
I am trying to create a simple onscreen keypad created using buttons (currently a User-control), on those buttons i have a click event, when i click/touch a button i want the value of that button sent to a Text-block in my Main-window.
I can't/don't understand how to make the User-control (keypad) see the Text-block (in Main-window) to add in the value that i need.
I have seen solutions that use command Bindings and solutions that use the visual tree traversing but all of them are the main window accessing the user control, not the other way around.
All the examples are the other way around because that is how a UserControl is supposed to work.
A UserControl is a packaged piece of re-usable functionality. It should not know anything about the code that is using it.
Instead you should expose routed events in your UserControl for things like a when number was selected, and subscribe to them in your main window.
There are many ways to achieve what you want. If your MainWindow.xaml has a UserControl and you want to react to a change from the control in the MainWindow.xaml.cs file, then you could add a delegate to the UserControl code behind and register a handler for it in the MainWindow.xaml.cs file. Implementing new delegates are generally somewhat simpler than implementing RoutedEvents, which is another way that you could handle this situation.
Using a delegate like this will enable you to effectively pass a signal to the main view from the child UserControl code behind, which you can react to in any way you want to. Rather than explain the whole story again here, please see my answers from the Passing parameters between viewmodels and How to call functions in a main view model from other view models? posts here on Stack Overflow for full details on how to achieve this.
I am using MVVM in my project and I am implementing the Diagram Designer and I am using RAD Diagram control of Telerik.I am dragging the shape from my RadToolBox to canvas for which I have implemented the event.Now on this event I want to save the diagram in RavenDB once the shape has been dropped on Canvas ( Ruler).This whole thing is in the User control stored in one library and I am creating the reference of this library in my actual WPF form which is in other library.My requirement is I want to call the Save command which is present in View Model of WPF form, from my xaml.cs of the user control and this user control is present in other library.
How shall i achieve the same.Please help.
Why do have multiple assemblies? If you don't need them, then make your life simpler and just use NameSpaces to separate your controls from your forms in the same assembly.
Right now that's said :-)
The easy way is via an Event. Add an event to your UserControl, and then in the Window add the code when the event happens.
Have a read through MSDN Events
I really dont Why you would need to call a save command from another WindowsForm.
Telerik UI Diagram provides indefined save Command which is used to save the Diagram,
InXAML,
<Grid.CommandBindings>
<CommandBinding Command="telerik:DiagramCommands.Save" Executed="ExecuteSave" />
</Grid.CommandBindings>
And In code behind define save function,
private void ExecuteSave(object sender, ExecutedRoutedEventArgs e)
{
e.Handled = true;
this.diagram.save();
}
I am in the process of learning WPF (I don't know much yet). I know how to associate a menu subitem to an event by directly editing the xaml but, I'd like to know how to do the same thing using Visual Studio.
For instance, consider the following XAML snippet:
<Menu DockPanel.Dock ="Top"
HorizontalAlignment="Left" Background="White" BorderBrush ="Black">
<MenuItem Header="_File"/>
<Separator/>
<MenuItem Header ="_Exit"
MouseEnter ="MouseEnterExitArea"
MouseLeave ="MouseLeaveArea"
Click ="FileExit_Click"/>
I'd like to be able to associate the "_Exit" menuitem with the appropriate event handlers for MouseEnter, MouseLeave and Click using Visual Studio instead of manually editing the xaml text. I was trying to accomplish this by using the "Items" collection editor but, it seems to only allow editing of the subitem's properties and not the events. Is there another editor/dialog/etc that allows editing the subitem's events ?
Thank you for your help,
John.
P.S: thank you H.B for taking the time to format the xaml properly. :-)
It does not seem to be possible to do this.
I realize this is an old thread but since I came across it, I figured someone else may benefit from a method that I found to use Visual Studio to view/edit the Event Handlers definitions of sub-MenuItems.
This method still requires that you access the XAML file but you do not have to edit it. Simply select (click) the record in the XAML file which defines the sub-MenuItem and you will be able to access the Events page in the Properties Window (F4).
If Microsoft is listening, I would say this is a design flaw (bug?) in the Collection Editor.
I'm not sure what you mean by "edit with visual studio", so I hope I'm on the right track here...
You can give the items names in xaml:
<MenuItem Name="exitMenuItem" ....>
then in the c# codebehind:
exitMenuItem.Click += (s,e) =>
{
DoIt(); // your event handling code
}
on the Properties window for that item switch to the Events tab then double click on the box next to the event you want.
in 2008 I think that there is a button that looks like a lightning bolt that you can click to get the events. It should be in the same area as the buttons to sort alphabetically or to order by category or whatever.