WPF Visibility based upon global variable value - wpf

I have a boolean global variable that is set true if an administrator is logged in and wants to amend the content of the lists and comboboxes. As a result, a button is displayed beside each combo to display a dialog box when clicked.
If I was not coding for WPF, I would probably include some sort of code similar to the following in each Window:
If gAdminEditLists=True Then btnUpdateCombo.Visibility=Visible Else btnUpdateCombo.Visibility=Collapsed
In my WPF app, I am using a style for the button that is used throughout the application and I am guessing that the best way forward is to set the Visibility of the button within the style based upon the value of the gAdminEditLists variable.
The only way I can see of doing this is to use some sort of converter within the button style that converts the gAdminEditLists value to visible or collapsed.
I'm not too sure how to proceed with this or whether this is the best approach, so any suggestions would be appreciated.

well your problem is that you're using a global variable isn't it?
if you had a property that implemented notification or a dependency property you would be ok. the closest thing to a global variable is a property on the app ( i assume thats where your global is )
defining dependency properties on app.xaml, however you could just use INotifyPropertyChanged.
binding to properties in app.xaml.cs
enjoy!

Have a look at the BooleanToVisibilityConverter class in the System.Windows.Controls namespace.

Related

Changing the default setting of a WinForms control property

I seek to change the default setting of a .NET Windows Forms control's property, without creating a custom control inheriting from it.
What I want is, for example, to add a TextBox on my Form that already has the TextAlign property set to HorizontalAlignment.Right instead of HorizontalAlignment.Left. Even if only solution-wide, if achievable, I would love to know. This would save a lot of time for when one is working with a LOT of controls and needs to set their properties to specific, non-default, values.
Creating a custom control is just too overkill for this, and would clutter my solution with unnecessary things. I have also considered running a regex on the designer files solution-wide (to add the non-default values to such controls), but writing regex like that can (also) be time consuming/problematic.
Any ideas?
There is a way to do that. But you need to set the binding initially. Later on you can set in one go to all the controls of your choice. This might be not your exact answer but this is how we define styles like as in WPF
(1) Click the Application Property Bindings
(2) Define Binding
(3)Now for textbox you can set the initial default
(4) Now You can set the property binding of every textbox intially or you can code which will set the application settings at starup or on form load.
This will you help to change the default propety at once later on

Using the namespace that is in XAML in the source code

From this document:
http://www.telerik.com/help/silverlight/radmaskedinput-features-extensions.html
I am interested in <telerik:RadMaskedNumericInput maskedInput:MaskedInputExtensions.Maximum="1000" />
But in their examples they are setting a property like MaskedInputExtensions.Maximum directly in the XAML. So if I want to use it I have to go to go to all my XAML files and set it there.
But since I have created my own control so I have both a CS source code for it and also my own XAML for the style of my control.
Is there a way I can set it in either of those places to make it more reusable?
The best way to do it is using styles.
Check this out.
http://blogs.msdn.com/b/pakistan/archive/2013/03/07/xaml-how-to-style.aspx
http://www.codeproject.com/Articles/180656/Styles-in-Silverlight
It means that you have a pre-defined style for every element of a certain type, so if you want to change the same property for every element of that kind you just change this specific style.
If this is not what you want you can also create a property called "Maximum" for example and bind it to your controls manually. Once you change this property it will reflect on your controls.
Hope it helps!!

Proper way to set MenuItem visibility in Context Menu?

I am trying to do things the .net way and declare my menu items for my context menu like a good citizen in xaml :).
I have a listbox that contains a bunch of list items based of ItemsSource.
QUESTION: How can I dynamically set the visibility of a particular menu item based off a function to be defined in the Page class?
I could just data bind this to a property in my DataContext class, but there are a few things that are more ui-specific that it really should not know about, so I'd like to keep it clean.
Can I somehow bind the visibility to a function in the page class and pass it the data context and have it return the appropriate visibility value required? Or is there another way?
Thanks!
swine
Bindings are useful because the UI updates whenever the bound value changes. Since a function doesn't update, even if you could bind to a function, the UI would never change. The UI would also never know when to call the bound function.
Why don't you just bind to a bool DataProperty and then use the function to update the bool. If you explain more of what you are trying to do, then I can give you a more detailed solution.

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.

WPF: Switch Template based on UserControl Data

Trying to implement what I thought was a simple concept. I have a user control (view) bound to a view model which provides a list of data. I have added toggle buttons to the usercontrol and would like to allow the user to use these toggle buttons to switch out which template is used to show the data. All of the templates used for the data work, and they are very different from one another so it's not just simple changes to a single template. I'd like to get this as much in XAML as possible.
Here's what I have now:
Where the data appears I have <UserControl Template="{StaticResource ListSwitchingControlTemplate}" />
In that control template I have all "sub templates" - really it's just all 3 representations with their visibility set to Collapsed. Then I use a data trigger on that control template to show the currently selected view. This works, but I noticed that all 3 representations get bound - they each act like they are active (and they are I guess).
I'd rather be able to truly switch the template at run time. I tried converting the containing user control to use a ContentTemplate for itself, but that just messes up all of the binding I have elsewhere. If only UserControls could use DataTriggers I'd be ok.
Any suggestions on how to cleanly go about getting this behavior. I have an idea that I'm just missing something simple.
Thanks,
Dave
you could do it via code?
http://www.switchonthecode.com/tutorials/wpf-tutorial-how-to-use-a-datatemplateselector ???
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/4fd42590-8375-46d0-b7bc-6c217df0f0ba/
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/dbbbb5d6-ab03-49a0-9e42-686fd41e0714
One way to do this would be to use a DataTemplateSelector.
Basically, you create a class that inherits from DataTemplateSelector and override its SelectTemplate virtual function. The return value from the function is the DataTemplate you want to use and in that function you have access to the object and its properties, which you can use to decide which template to select.
There is an example on MSDN here:
http://msdn.microsoft.com/en-us/library/system.windows.controls.datatemplateselector.aspx

Resources