I have a following piece of XAML which binds a button click using prism. Can someone guide how can I achieve the same behavior in code behind since I need to create the button dynamically? Thanks.
<telerik:RadButton Margin="2"
TabIndex="3"
prism:Click.Command="{Binding cmdNew}">
EDIT : One thing I missed to mention in my post was that these command names are stored in database and I need to generate these command objects on the fly. So in this case if "cmdNew" was stored in db and I had to bind it dynamically to the button how would I go about doing that? I have looked at DelegateCommand but not sure if that can be helpful in this scenario. Your response is much appreciated.
Click.SetCommand(button, <value>)
Use the event (I think it is called) Click = "NameOfMethod" ), where NameOfMethod is a method in your cs file belonging to the screen.
When the button is clicked, the method NameOfMethod will be called.
Related
I have a MenuItem Checkbox in my main window as well as in another two windows:
<MenuItem x:Name = 'FileMenu' Header = '_File'>
<MenuItem x:Name = 'EnableAutostart' Header = '_Enable Autostart' IsChecked="{Binding AutoStart, Mode= TwoWay}" IsCheckable="true" ToolTip = 'Enables the autostart.' InputGestureText ='Ctrl+S'></MenuItem>
I use it to define whether or not the script automatically starts on user logon (a function checks for the scheduled activity and sets the checkbox accordingly) and when clicked it enables or disables it.
Now what if I want to keep all the 3 different windows checkboxes synced?
I read a lot about binding but can't really get it working, I tried to set the binding on a bool "Autostart" and to set it true before showing the window but it does nothing at all.
How can I define a bool in my code and make it so that, whenever I change that bool, it reflects on all the menuitems checkboxes bound to it? (even if the other windows are yet to be created/shown)
I read about needing to make it a property... can I just make a class, set a property to it called something like "AutoStart" and it would work?
Thank you #bluuf, you kind of gave me a hint.
Anyway comes out i actually achieved to do a databind previously in my many tests but as you said, the INotifyPropertyChanged is not so easy to handle in powershell, or maybe it is?
Good guy Trevor Jones (link at bottom) explained an easy way and i did the last steps to understand it and apply it to my code. Thank you Trevor.
He basically explains that this one collection:
System.Collections.ObjectModel.ObservableCollection[Object]
Has an already implemented iNotifyPropertyChanged that works well in powershell, without adding c# classes.
You just create that collection, add your bool to it, and point the binding.source to it.
Trevor's article, actually read it, worth your time:
https://smsagent.blog/2017/02/03/powershell-deepdive-wpf-data-binding-and-inotifypropertychanged/
P.S. Trevor also points to this article which explains how to implement the c# class (and more), if anyone is interested:
https://www.ephingadmin.com/better-know-a-powershell-ui-50-shades-of-bindings-part-1/
I might be going completely wrong here but I am trying to perform 2 actions based on a single click of a ModernButton (Part of ModernUI).
Basically I have an 'Add' button on one page. When a user clicks it I want to call a 'Add' method on my underlying ViewModel and also navigate to a new view. I currently have this working from 2 separate buttons as a test but want them both working from one.
Example buttons:
<mui:ModernButton x:Name="btnAdd" Command="{Binding AddCommand}" CommandParameter="{Binding Project}"/>
<mui:ModernButton x:Name="btnAdd2" Command="NavigationCommands.GoToPage" CommandParameter="/View/pages/ProjectView.xaml"/>
I think I am getting confused as its a combination of 1 action on the UI and 1 on the ViewModel underneath.
It seems like a straightforward requirement which makes me think I may be going wrong in my approach.
As always, any help is very much appreciated.
Thanks.
You can always copy the way Microsoft implements this problem, see the CompositeCommand, and either bind directly to this command, or create a multivalueconverter which takes multiple commands and returns a CompositeCommand.
I am working on WPF MvvmLight application and once I click on first page it navigates to second page. The second page has a textbox on which the focus is set. On this textbox, I am not able to put any data or any character. But I can do copy and paste. What might be the reason for this strange behavior.
<TextBox VerticalAlignment="Center" x:Name="txtsearchYouTube" Height="25"
Margin="100,0,100,0" Canvas.Top="275" Width="500"
Loaded="txtsearchYouTube_Loaded" Canvas.Left="50"
Text="{Binding SeachKeyWord,Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"/>
1) The Binding is not the issue, in fact even if it is wrong, you will still be able to write in that TextBox, it just won't be put in SearchKeyWord. By the way, you mispelled it (you forgot an R in seaRch).
2) If you're really only setting the focus on the loaded event, and nothing else, then it's not the problem either.
3) MVVM Light is just a conveniency framework, it does not alter WPF for you as it just provides tools, it definitely doesn't mess with TextBoxes.
That said, here is what I would check in your situation:
If SearchKeyWord is a dependency property look where it is declared to see if there is a callback/coercevalue/validatevalue declared. Since the binding is two-way, it could very well always invalidate the values recieved (preventing you from actually writing anything in there)
Check all the way up your visual tree, for any implicit styles for the TextBox type. They could have triggers that mess up with your data.
Hope this helps,
Bab.
i dont know whats wrong with your code. but you can help your self. so just remove your binding and loaded event and check if you can put any data in your textbox.
if yes pls post all code from your event and viewmodel otherwise its hard to help.
you wrote you can copy and paste? what does it mean? your textbox show the text you paste in? or your viewmodel got the pasted value?
EDIT: you can also use Snoop to check your textbox binding and properties at runtime.
Hey a similar question has been asked before, but none that answers mine exactly.
I have an MVVM aplication that includes an "Options" button - click this and the options dialog opens - this is done via a command.
Once the user has saved their options, I want to tell the main shell to reload its options. What is the best way of doing this?
My button looks like this:
<Button Width="50" Command="{Binding SettingsCommand}" CommandParameter="" ...>
<Image Source="Images/Settings.png" Width="16" Height="16" />
</Button>
Thanks,
Dave.
The standard way is to implement INotifyPropertyChanged on your viewmodel, and have your command fire the PropertyChanged event after it's done changing properties (which in this case would be after the dialog closes). When you fire PropertyChanged, your bindings will read the new property values.
main shell to reload its options
Thats usualy acieved using DataBinding. When you are closing this dialog, simply update values in your ViewModel and it should bind to View easily.
It looks, like your understanding of MVVM is little wrong.
I suggest having an OptionsService that the ViewModel uses within the Dialog. Save changes to the service and if any dependent VM's need to change the display state based on the options change then they register (via an event) on the service that the options have changed. The service would also have a way to get the options data as well.
If you use Dependency Injection then this should be straight forward as you can inject an IOptionsService into all the ViewModels.
I am using VS 2010 and a datagrid.
I need the ability to either single click or double click and process the action in my ViewModel.
Is this possible?
I could not find an example using MVVM.Can you provide one,a link or a snippet etc..
thanks a lot
You can use an attached behavior to map a command to the event. See Marlon Grech's blog for an example.
You may want to check this out, and the given links in the post: http://forums.silverlight.net/forums/t/188159.aspx. Hopefully this is along the lines of your implementation.