Pass parameter from Winform to UserControl - forms

I have a UserControl in a Winform and I would like to pass a parameter, here's what I'm doing:
In the User Control:
public List<ushort[]> pixels16 { get; set;}
In the Form:
VolumeView controlv = new VolumeView(); //VolumeView is the UserControl
controlv.pixels16 = pixels16_img; //pixels16 is the variable in the Form that I need to use in the UserControl
It works, however, when I run a function in the UserControl, then the value is set to null:
public void CreateVolume()
{
pixels16 is null!
}
what I'm doing wrong? I basically need to use a Form variable in a function within the UserControl..
Thanks!

Have you tried passing the object as a parameter in the UserControl's constructor?

Related

How to do binding based on a condition in .net maui?

I'd like to ask how to do the conditional binding in a .net Maui app. for example
if I have a class
public class ClassA
{
public string Property1{get;set;}
public string Property2{get;set;}
}
and I don't have access to the class implementation so I cannot add methods
and in my view, I want to bind to Property1 if some condition is true and bind to Property2 if that condition was false.
Is there a way to do conditional binding from the view itself without changing ClassA implementation?
I think you could achieve this goal by setting binding property in code-behind (xaml.cs file) instead of .xaml file.
For example:
In MainPage.xaml, you have a label with name labelA:
<Label x:Name="labelA" />
In MainPage.xaml.cs constructor, you could bind Text property of labelA to one of viewModel properties based on a condition.
public MainPage(ClassA viewModel)
{
InitializeComponent();
BindingContext = viewModel;
if (true condition)
{
labelA.SetBinding(Label.TextProperty, nameof(viewModel.Property1));
}
else
{
labelA.SetBinding(Label.TextProperty, nameof(viewModel.Property2));
}
}

Argument exception when I try to set a variable with a custom set in unity

I tried to use the Generate() function only if a variable has changed without having to check it every frame. I used the following tutorial to achieve this. but for some reason, whenever i try to set the variable, I get this error:
ArgumentException: GetComponent requires that the requested component 'List`1' derives from MonoBehaviour or Component or is an interface.
the script:
public GameObject CEMM;
private int ListLength;
public static int ListLengthProperty
{
get
{
return JLSV.instance.ListLength;
}
set
{
JLSV.instance.ListLength = value;
JLSV.instance.Generate();
}
}
private void Awake()
{
instance = this;
}
I tried to set the value like this: JLScrollView.ListLengthProperty = JLScrollView.instance.CEMM.GetComponent<List<JLClass>>().Count;
The generic type parameter that you use when calling GetComponent must be a class that derives from Component (or an interface type). List is a plain old class object, which is why you are getting the exception from this:
GetComponent<List<JLClass>>()
I'm not really sure what value you are trying to assign to the property. If you are trying to get the number of components of a certain type on the GameObject you can use GetComponents.
JLScrollView.ListLengthProperty = JLScrollView.instance.GetComponents<JLClass>().Length;

mapping the data of the child form on the parent form symfony2

I have created a Form Type that adds an autocomplete feature for Entities, however it requires some configuration for every Entity, i.e: I have to pass the configuration to the options array, so I decided to make a new FormType for each Entity using the AutoCompleteType I created and reuse them.However I want these Formtypes i.e: the ones for each particular Entity, to return the Entity when getData() is called on it, what happens now is that I have to first retrieve the field of ParentForm containing the AutoCompleteType then call getData() to retrieve my Entity.How can I map this information directly on the ParentForm?
//the FormType of Some Entity using the AutoComplete
...
class SomeEntityAutoCompleteType extends AbstractType{
public function buildForm(FormBuilderInterface $builder, array options){
$builder->add('some_entity', 'entity_autocomplete', array(...));
}
}
//the controller
public function someAction(){
$form = $this->get('form.factory')->create(new SomeEntityAutoCompleteType());
...
//I want the below line to return my entity
$form->getData();
//but I have to use this one right now
$form['some_entity']->getData()
}
note: I haven't actually tested the other approach but from what I understand of the Symfony Form Component it should be the way I described;
I solved it by setting the parent type of my SomeEntityAutoCompleteType to the main autocomplete type I had created and configuring the options using the setDefaultOptions() method.
//SomeEntityAutoCompleteType
public function setDefaultOption(OptionsResolverInterface $resolver){
$resolver->setDefaults(...);
}
public function getParent(){
return "autocomplete_type";//this is the main autocomplete type I mentioned
}

How to save the selected value from dropdownlist to database using Entity Framework in ASP.NET MVC 4

In my one of the view I add a dropdown and I bind this drop down with my database like this..
public ActionResult PostMarks()
{
JoinMod std = new JoinMod();
std.Drp_bind = (from nm in db.TbStudent
select new SelectListItem
{
Text = nm.StudentName,
Value = SqlFunctions.StringConvert((double)nm.StudentId).Trim()
}).ToList<SelectListItem>();
return View(std);
}
Here is the dropdownlist in my view
<p>StudentName</p>
#Html.DropDownList("StudentName",Model.Drp_bind,"Select")
And on Post I am trying to save the data into the database like this
[HttpPost]
public ActionResult PostMarks(Marks marks)
{
db.TbMarks.Add(marks);
db.SaveChanges();
return RedirectToAction("ShowAllMarks");
}
Now when I check my database after save data in database the Id save in the database is zero from the dropdownlist. Please experts help me to solve this issue
You should be using #HTML.DropDownListFor, specifying a lambda that indicates which property in your model you want to bind the list's selected value to on the POST.
Given a view model like so:
public class MarksViewModel
{
public Marks Marks { get; set; }
public IEnumerable<SelectListItem> Drp_bind { get; set; }
}
The drop down list in the CSHTML can be declared like so:
#Html.DropDownListFor(m => m.Marks.StudentId, m.Drp_bind, "Select a Student")
The first argument is an expression describing the property on your model that you want to populate with the selected value from the drop down list in the post back. The second is the list comprising the values to which we need to data bind.
Note that your CSHTML will need more form fields within it bound to the other properties of the Marks property.
Your POST method would now take MarksViewModel as an argument and extract the Marks property from it to add to the DbContext for saving.

how to parametrize an import in a View?

I am looking for some help and I hope that some good soul out there will be able to give me a hint :)
I am building a new application by using MVVM Light. In this application, when a View is created, it instantiates the corresponding ViewModel by using the MEF import.
Here is some code:
public partial class ContractEditorView : Window
{
public ContractEditorView ()
{
InitializeComponent();
CompositionInitializer.SatisfyImports(this);
}
[Import(ViewModelTypes.ContractEditorViewModel)]
public object ViewModel
{
set
{
DataContext = value;
}
}
}
And here is the export for the ViewModel:
[PartCreationPolicy(CreationPolicy.NonShared)]
[Export(ViewModelTypes.ContractEditorViewModel)]
public class ContractEditorViewModel: ViewModelBase
{
public ContractEditorViewModel()
{
_contract = new Models.Contract();
}
}
Now, this works if I want to open a new window in order to create a new contract... or in other words, it is perfect if I don't need to pass the ID of an existing contract.
However let's suppose I want to use the same View in order to edit an existing contract. In this case I would add a new constructor to the same View, which accepts either a model ID or a model object.
"Unfortunately" the ViewModel is created always in the same way:
[Import(ViewModelTypes.ContractEditorViewModel)]
public object ViewModel
{
set
{
DataContext = value;
}
}
As far as I know, this invokes the standard/no-parameters constructor of the corresponding ViewModel at composition-time.
So what I would like to know is how to differentiate this behavior? How can I call a specific constructor during composition time? Or how can I pass some parameters during the Import?
I really apologize if this question sounds silly, but I have only recently started to use MEF!
Thanks in advance,
Cheers,
Gianluca.
You CAN do this. Check out the Messenger implementation in MVVM-Light. You can pass a NotificationMessage(Of Integer) to send the right ID to the view model. The view model has to register for that type of message, and load it when a message is sent.
MEF Imports by default only have a parameterless constructor.