Show context menu for some items in NSOutlineView - c#

I have a SourceList (NSOutlineView) and I want to display a context menu for some of the items. Looking around I have found answers in Cocoa and Obj-C but I am trying to do this in MonoMac and C#.
It seems to me I need to do my own custom class which inherits from NSOutlineView and implement the method MenuForEvent. But when I try to replace my old, standard NSOutlineView with my own custom class, nothing shows up during runtime. In my controller I call View.ReplaceSubviewWith(oldTree, newTree).
Do I need to do something else? Or perhaps there's another way to accomplish this?

Instead of trying to swap the instance like that, you should be able to do it declaratively.
Make sure you 'Register' your custom outline view, e.g.:
[Register("MySourceList")]
private class MySourceList : NSOutlineView
{
// Need this constructor for items created in .xib
public MySourceList(IntPtr handle) : base(handle)
{ }
Then, in the Xcode designer, select your outline view and specify the name you registered as the Custom Class for that object:
That way, when your view is created from the nib, the runtime will create the proper instance of your outline view in the first place.

Related

How to access WPF control properties outside of main class

I have a 'public static' class that gets called on application startup. In this class, I'm trying to set the "IsEnabled" and "IsChecked" properties of several check-boxes. I had no problem doing this in MainWindow.xaml.cs but when I try to reference the check-boxes in my custom class, the check-box names aren't resolving in Intellisense/auto-complete. I also get the error, "The name 'cbx_NameOfMyBox' does not exist in the current context"
How can I access control properties outside of MainWindow.xaml.cs?
namespace Widget
{
public static class StartupSequence
{
public static void Begin()
{
GetDomain.Start();
cbx_GpoUpdate.IsEnabled = false;
return;
}
}
}
SUGGESTIONS:
You need to implement MVVM and bind the data.
If you are going to use static class to share data, then i would suggest using singleton class. You should not make the class static. You should make the constructor of the class as private. Then, create a static property which can return your class. (just do some google search to learn about singleton)
SOLUTION TO YOUR ISSUE (as a quick fix) :
You are doing it in the opposite direction. Main purpose of static property is that it can be accessed from anywhere in the project (provided the namespaces are properly referenced). So, instead of trying to access your control from the static class, do it the other way. In your xaml.cs (Control's code behind), like when the control is loaded or initiated or somewhere suitable, add like,
cbx_GpoUpdate.IsEnabled = StartupSequence.your_boolean_property_for_this
You need to have a boolean property in your static class to store the required data and when the control is initiated, you refer it. You can also created different other properties for different controls and in each controls' code behind you can refer them whenever they are loaded or a button is clicked or in any other event scenario.
Note: I started exactly the way you are doing it (in code behind) but after several months and projects later, i learned it the hard way that MVVM pattern is the best for WPF. Now, 3 years straight, all my projects are in MVVM. Start to learn MVVM and move to it as soon as possible. Cheers.

Changing property values in WPF Data Context from another class/ViewModel?

I'm trying to understand data context and how properties within different viewmodels are applied to a window in WPF.
Let's say I have a simple sample form with the following codebehind:
XAML Code Behind
public partial class pageTest : Page
{
public pageTest ()
{
InitializeComponent();
this.DataContext = new AnotherClass();
}
}
}
Now, let's assume we have a property in the AnotherClass class that is a boolean called Visible. This visible value is used to collapse and show a particular element.
Now, my confusion lies in understanding how to manipulate this Visible property from another ViewModel. Seeing that I create a particular instance of the AnotherClass class, what would be the proper method of changing the Visible property from another ViewModel at another point in time?
Hmm ... usually you'll try to avoid code behind in WPF and go the MVVM way.
Regardless of that, you'll have to either make that property static, so it lives across instances, or inject the instance using Dependency Injection (DI), so whomever needs to change it has access to that object.
For a (biased) mvvm review, you can check out this article.
I highly suggest going that way and probably looking DI as well, since they are plenty of frameworks do help you with that (ninject and autofac pop to mind, but google it up, there's plenty).

Bind Click function with parameter

In MVVM Cross for Android in Xamarin Studio,
I can write this in my .axml file to bind a click function to a button:
local:MvxBind="Click SendMessage"
SendMessage is a public method on the MvxViewModel with the signature
public void SendMessage()
{
//do stuff
}
However, I want to do something like like this,
local:MvxBind="Click SendMessage param1: foo, param2: bar"
which should call the method underneath with a signature like this,
public void SendMessage(T foo, T bar)
{
//do stuff
}
where foo and bar might be the current selected item, or the object represented in a particular row of a table etc.
I can't see anywhere that points towards how to do this, and I am hoping that it is a native functionality! Can anyone help?
The binding engine allows you to use either ICommand instances or public void methods. The latter only works if you also install the NuGet package MethodBinding.
As for the amount of parameters supported, it boils down to a single argument, which should correspond to the ViewModel bound to the item in the ListView.
You can use an ICommand instead of a void to execute your code, here you can paas one parameter.
An other option is to bind the parameters you need to objects and access these objects in your code.
Being a bit illiteracy with the exact functionality in xamarin studio, I would like to suggest a general different approach:
How about letting the controls on your View set a class wide property SelectedItem when they're selected, that could then be accessed by the buttons method when it's clicked?

C# Windows Forms: Access Controls(Labels, Buttons) in other Classes

I need to know, how I can access lables or buttons other than in my "Form1"-Class.
My Problem:
I created for example labels, buttons via the design viewer. Now I can access
them in my Form1 Class. (testlabel.Enabled == true) just for example.
What I CAN'T do: Access those labels, buttons in another class! Let's say
I have a class "second-class" and I want to have a method there, that changes
the property of a label to
`testlabel.Enabled == false`
That's not possible, because in that "second-class" it's not visible.
So, is there an obvious easy solution to make those controls accessible in other classes?
Create a method in that (Second class) which takes that component (Label or Button or whatever you want to modify) as parameter into that method.
public void disableLabel(Label inputLabel)
{
inputLabel.Enabled == false
}
Create a method like the above.
Now in the form1 class you just to need to call that method and pass your Label into that method to Disable it.
SecondClass objSecondClass = new SecondClass();
objSecondClass.disableLabel(testlabel);
Every control in a form class is created by default with its property Modifiers set to Private
If you change it to Public you could access the control instance from another class.
However this is really a bad practice to follow. Messing with the visibility of the control is dangerous and could cause very complicated bugs to resolve.
If you really need to change something in your form class then provide a public method and call this method to change the internal functionality of the target form

Unable to access Winforms control in a class

I am currently working in a small windows forms project in C# using Visual studio 2008.
I have added a custom class to the project, but in this class I am unable to access the forms controls (like listbox, textbox, buttons ) in order to programmatically change their properties.
The class file has using system.windows.forms included and all files are in the same namespace.
Surprisingly, I am also unable to access the controls in the form1 class itself, unless I create a method in the class and then intellisense pops up the names of the various controls.
in the custom class however, intellisense does not show the names of the controls at all.
Appreciate if someone coudl shed some light on why this could be happening.
Thanks
Encapsulation means your separate class shouldn't be talking directly to the controls. You should, instead, expose properties and methods on your (outer) Control - for example:
public string TitleText {
get {return titleLbl.Text;}
set {titleLbl.Text = value;}
}
For more complex operations it may be preferable to use a method; properties are fine for simple read/write for discreet values.
This provides various advantages:
you can, if required, abstract the details to an interface (or similar)
you can change the implementation (for example, use the Form's Text for the title) without changing the calling code
it is just... nicer ;-p
Your class needs a reference to the form for this to work. The reason for this is that the form is not a static class so you can have multiple instances of it.
The best way of giving it the reference would probably be to pass it in the classes constructor. Then the class would have a reference to the form and could use that reference to change the controls.
An alternative option that you could use if you are 100% sure that you will have only one instance of your form open is to add a public static property to the forms class that returns the instance of the form. That property would then be available to be used in your other class.
Also, make sure that your controls are public, or better add public methods to your form that can be used to manipulate the controls indirectly.
The controls in Form1 will be private
partial class Form1
{
//elided other good stuff
private System.Windows.Forms.Button button1;
}
So no, you can't access this directly from another class.
You could make it public as #abatishchev suggests (but that would be a really bad idea).
A better plan would be to use properties as #Marc Gravell suggests.
You would still need to pass a reference to the form to the class that you wish to have consume the properties though (as pointed out by #Rune Grimstad).
You are trying to write a class in your application that directly asks the UI for data. This isn't usually considered a very good idea. The class should be entirely concerned with it's own purpose. You should design properties or events for the specific bits of data that the class needs access to and not necessarily pass it the entire form, maybe just the values that it needs to work with or change.
Take a look at how this could be implemented using the MVP pattern (sample code): Implementing MVC with Windows Forms
UPDATE: The code in the class you mention should in fact be part of the form's presenter, which has a reference to the form (through the IView interface). That is how you should be designing your UI code, not by directly accessing other Form's private parts.

Categories