Set the cursor to WaitCursor with PowerShell and WPF GUI - wpf

If I use a regular windows form in PowerShell using this works.
$Form.Cursor = [System.Windows.Forms.Cursors]::WaitCursor
How can I achieve the same when dealing with a WPF application in PowerShell?

The property name on WPF elements is the same (Cursor).
Assign one of the static member values from the [System.Windows.Input.Cursors] class:
$wpfElement.Cursor = [System.Windows.Input.Cursors]::Wait

Related

Refresh the UI of Windows Form

I'm working on an old project that supports Windows Forms.
This project contains some ResourceManager for the support of a few localizations. The idea is that you call ResourceManager["SomeResource"] instead of Resource.SomeResource and it returns you a localized string.
And these localized strings are used in the code of the initialization of the form. For example, you have Form1, and in Form1.Design.cs there is some code like this:
Label label1 = new Label();
label1.Text = ResourceManager["SomeResource"];
So the label will be created with an already localized string in the Text.
And we need to add the functionality of changing the UI language without reloading the Form.
We can just set the every Text property of every controls again. But it's a lot of code, the form contains a lot of controls.
We can call the Form.InitializeComponents(), this method will recreate all controls with new localized strings, but in some cases, it works slowly because it reloads some big data again.
Is there some other way to refresh all UI controls and get the new localized strings? Do Windows Forms support some mechanism like Binding in the WPF to create the "connection" between the Text properties and localized resources?
I think that you can achieve this by use of Invalidate() either on the form itself or on a container control that your other controls may be encompassed by.

Button custom attributes for windows forms

I need to add a secondary Id or even some custom text to controls like Button, Textbox, ListBox etc., so that i can later use it for programmatic purposes and should not be displayed to user.
I can do this in ASP.net using Attributes property for almost any control, but when I checked with windows forms I found it doesn’t have this property, can I find any other alternatives in windows forms?
You can use Control.Tag property for this.
myButton.Tag = whatever;
myTextBox.Tag = whatever;
...
Use the Control.Tag property. Source: MSDN
"A common use for the Tag property is to store data that is closely associated with the control."
This sounds like what you're after.

Open usercontrol at runtime-WPF?

In my WPF application, I have a table which stores the frequently used window names for each user. At runtiime, I make a list of it.
List<string> LstUserWindows= new List<string>();
What I need is I need to open each window depending on the names of the windows in the list. (I am using usercontrols as windows). Something like below:
foreach (var rec in LstUserWindows)
{
UserControl mainUC = this.FindName(rec.MyWindow) as UserControl;
displayUserControls(mainUC,null);
}
I'm not sure which approach you have currently taken in storing the UserControl instances, but here are two possible approaches you could take.
If all UserControl instances already exist within your UI but are simply hidden, then you should be able to use the FindName(...) (as you've mentioned in your question) and then change the Visibility property of the UserControl.
If you have not yet loaded the UserControl instances and you want to dynamically create the control given it's name, then you need to look into using Reflection. Using this approach, you can acquire the Type information from the Assembly and work on using Reflection to construct the object. Alternately, you could use the Activator class to construct an instance of the required control type. For that approach, you would do something like this.
foreach (var rec in LstUserWindows)
{
UserControl control = (UserControl)System.Activator.CreateInstance("AssemblyName", rec);
displayUserControls(control, null);
}
Note: I'm not sure if the parameter structure is correct (I cannot currently test it). Check out the MSDN Documentation for more help on it.

Show/hide controls based on user role in WPF

I need to do a quick sample WPF application where the controls on the forms should be made visible or hidden based on the user roles.
Something like this will be great,
How to manipulate WPF GUI based on user roles
I am not sure where to put the XAML defined in the thread(<Control ) so that the every control in the form uses RoleToVisibilityConverter to show or hide the controls.
i am very new to windows dev..could you please help me ?
Regards
Bala
You could solve this by binding the Visibility to a corresponding property in your code-behind/ViewModel.
This is an example from a binding in one of my testcontrols using WPF (in combination with Caliburn):
Visibility="{Binding Path=IsAdmin}"
Here I have a bool property in my ViewModel called IsAdmin.
For me, the easiest way was creating a global variable in my app. For that, go to your Project->Properties->Settings.settings and create a new variable (called, for example, Administrator), and set it to True of False depending on what you want to do. Something like this:
Then, at your MainWindow, when its initialized, you can write this:
if (!Properties.Settings.Default.Administrator)
{
DisableSettings();
}
And then, disable the fields you want to.

Creating Silverlight UserControl

I'm creating a silverlight user control that I should be able to drag and drop via blend. But this control needs to accept a map that is already on the page.
For eg.
Main.xaml contains a map control.
MapEditor.xaml contains buttons and other controls. In the .cs file, it needs to access a map control (the one in Main.xaml).
How do I go about getting this done?
I was thinking about adding a parameter in the contructor for MapEditor but how would I pass in the map as a parameter in design mode?
Thanks.
ps. I'm going to break out this control into a silverlight library so it could be used in multiple projects later.
You don't want to be giving your control a parameterised constructor, XAML will only construct types using their default constructor.
Simple Approach
The easiest approach would be to add DependencyProperty to your control to which you would assign the Map control (I'll use the type name MyMap in this example):-
public MyMap Map
{
get { return (MyMap)GetValue(MapProperty); }
set { SetValue(MapProperty, value); }
}
public static DependencyPropery MapProperty = new DependencyProperty("Map",
typeof(MyMap), typeof(MapEditor), new PropertyMetaData(null));
Now in Blend the Map property will appear in the Miscellaneous category in the Properties tab. You can then use the "Element Property" tab of the "Create Data Binding" to select the Map control to which it should bind.
Hard Core Approach
That said I would be inclined to build a proper customisable control following these guidelines Creating a New Control by Creating a ControlTemplate. With the addition that I would extend the ContentControl base class and include a ContentPresenter at the heart of the template. The control would make the assumption that the child control is a MyMap control.
This approach allows the entire appearance of the MapEditor control to be styled in Blend and it allows the Map control that is to be "edited" to be drap-drop onto the MapEditor as a child control.

Resources