I need to pass a parameter to a binded command of a menuitem.click
the menu items are created dynamically in code behind, how can i create binding and a commandparameter in code behind?
Here is the answer: Binding Declarations Overview.
Basically you create an instance of Binding type and then assign it to an object via BindingOperations.SetBinding() method.
Related
I'm trying to build a datagrid with columns and a button in code behind and I want to assign the command parameter of this button to the value of the "ID" column which is "TDENT_ID". Here's my code
this line is not working as you can see :
BtnDetail.SetBinding(System.Windows.Controls.Primitives.ButtonBase.CommandParameterProperty, new Binding() { Source = dtGrid, Path = "TDENT_ID" });
How can I write it?
FrameworkElementFactory:
"This class is a deprecated way to programmatically create templates, which are subclasses of FrameworkTemplate such as ControlTemplate or DataTemplate; not all of the template functionality is available when you create a template using this class. The recommended way to programmatically create a template is to load XAML from a string or a memory stream using the Load method of the XamlReader class." Microsoft Docs
Your Bindingis wrong. It currently points to the DataGrid which does not have a property TDENT_ID.
This property is on your data item, which is the DataContext of the column's template.
Therefore, correct Binding would be:
// Set up a Binding that uses the current DataContext as Binding.Source
BtnDetail.SetBinding(ButtonBase.CommandParameterProperty, new Binding(nameof(myItemType.TDENT_ID)));
Microsoft Docs: Specifying the binding source
Try to learn XAML. It makes writing such UI related code much easier, which makes you more productive.
I'm trying to create a CustomControl to which I can pass an "Person" object. How can I pass the person object to the custom control like this PersonDetails="{Binding Path=Person}" to the CusomControl ?
instead of passing Address="{Binding Path=Person.Address}"Address="{Binding Path=Person.FirstName}" ect
You have to declare PersonDetails as a dependency property.
Then you can use it as any other WPF control property - both in XAML (including data binding) and in code.
let say there is a textbox and i want to control the visibility of this control using MVVM, is there a sample on how to do this? First create a dependency property then get it hooked up in the ViewModel. Thanks.
Typically, you wouldn't need to use a dependency property in this case. Dependency properties really only need to be implemented for things like controls themselves, not for determining behavior. Behavior, such as the visibility of an element, can be handled directly via data binding.
Your ViewModel would just have some property, and you'd bind the TextBox.Visibility property directly to the ViewModel property.
The one "sticky point" is that you often will want to have some type of IValueConverter that will convert from your property type to a Visibility enum.
I am trying to use MVVM light to achieve something like this. I have the following scenario:
In my Model--I have set the properties like ActivityName,Image and there is a class constructor whose is accepting 2 parameters like name and image.
Im my DataAccess--I have set the database connection and implement the required method who will fetch the data from DB and I am storing in the List and returning the list to ViewModel.
In my ViewModel--I have created the list property who will return the list by calling the GetActivities() method that I have defined in the DataAccess.
Now my problem is I am not getting how to bind it in the View so that by clicking on a button it will display the list of activities with image. By clicking on some button a new window should open with the desired result. How to bind the above list and implement the button functionality using MVVM light.
Kindly help?
Thanks
First of all, use an ObservableCollection instead of a List as it would notify the view when property or collection changes.
Then set the DataContext of your view to the viewmodel. If you use MVVMLight View Class, then the DataContext would be automatically set. You've to just give the ViewModel Name there.
Then set the ItemsSource of the DataGrid like this <dg:DataGrid ItemsSource="{Binding YourListInViewModel}"/>
For handling click event you can use the Event-To-Command behaviour and write your logics in the Button's corresponding Command handler.
Bind to DataContext of the control
I have a static class called commands. One the RoutedCommands in it is called ConfirmNoPrint. I want to Execute it in code behind from my custom control like this:
Commands.ConfirmNoPrint.Execute(null, [WHAT_DO_I_PUT_HERE]);
In the custom control class I have an instance of Binding whose RelativeSource property is set like this:
_mainControlBinding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(UserControl), 2);
Can I use one of the properties of _mainControlBinding to get the instance of IInputElement I need to pass as the second parameter of Commands.ConfirmNoPrint.Execute ?
The command binding for ConfirmNoPrint is the parent of my custom control, but it is in a different assembly. I can't add a reference to it since it would cause a circular reference.
I am barking up the wrong tree entirely?
Routed commands are, by definition, routed. If I understand your problem well, you just have to pass this as the second parameter of your command (assuming you're into the control class). The command will be bubbling up the visual tree until it encounters the command binding on the parent.