WP7 Binding to Local Variable - silverlight

I have a UI element whose color I'd like to set based on a local variable in the page (well actually in a module, but I can move it to the page).
I see in WPF one can bind a UI element to a local variable by making the variable a property and setting the binding RelativeSource to the parent class using FindAncestor:
WPF Binding to local variable
Unfortunately Silverlight doesn't seem to support FindAncestor mode.
Any possible solution, or do I have to create an instance of a custom class and such just for this one variable?

Ive been jumping from Silverlight to WPF a lot over the last few weeks, so the following idea may or may not fly.
Does your page have the name property set on it? If not, set it (x:Name="MyPage").
then you can do:
Fill="{Binding ElementName=MyPage, Path=MyProperty}"
Give that a try (just off the top of my head)

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

How to get the Namescope of a control in UWP/Windows 8.1

WPF has a method called NameScope.GetNameScope(control). How can you get the same thing in UWP/Windows 8.1? The runtime must be doing it internally somehow or else ElementName bindings wouldn't work
XAML namescopes do exist in UWP, but currently there's no public API similar to WPF's GetNameScope(control).
Depending on what you're trying to achieve, you will most likely have to fall back on using FindName or GetTemplateChild methods or ultimatly the VisualTreeHelper.
Because of the separate XAML namescopes, finding named elements within
a template from the scope of the page where the template is applied
requires a different technique. Rather than calling FindName on some
object in the object tree, you first obtain the object that has the
template applied, and then call GetTemplateChild.

TextBlock foreground being reset to inherited value after dynamic resource from merged dictionary is applied

EDIT: I was able to reproduce this in a very stripped-down version of the application. Here is a link to the .zip file
http://www.mediafire.com/?cn918gi15uph1xe
I have the module add the view to two different regions - the status bar region along the top is where the issue occurs. The weird part is, when the same view type is added to the main region, there are no problems. The status bar region is an ItemsControl and the main region is a ContentPresenter. That is the only difference.
Please let me know if you have any insight! Thanks.
-----Original Post-----
Hello all,
I am seeing some weird behavior with WPF. I'm using .NET 4 and PRISM v4. The way we have our application structured is that the skin resource dictionaries exist in their own assembly. The modules do not reference this assembly - instead we have a skin manager class that reads from a config file which skin we want and loads the appropriate components into a merged dictionary. The merged dictionary is set in the Application resources (we clear out the Application.Resources.MergedDictionaries before we add it). The idea is that we can later switch skins at runtime if needed, and the modules don't need to know about the skins until runtime.
Then in our xaml we are referencing styles using DynamicResource. The problem has to do with a TextBlock style defined in the skin and referenced by key, such as
<TextBlock Style="{DynamicResource someKey}" ... />
The style defines the font family, font size, and foreground. The font family and size are applied correctly (I verified this). The foreground, however, is always black. I used Snoop and WPF Inspector to see that the foreground value is "inherited" instead of coming from the style.
I also have a control that inherits from TextBlock and all it does is add some properties that determine what the text value should be (it doesn't affect the style at all). I was able to add a property changed override for the Foreground property and found out that the style's foreground value gets applied, and then the inherited value gets applied after that. I wasn't able to get the .NET source debugging to work so I couldn't figure out why/where it was being called from the second time...
Here is a link to an old, old post from a guy with the exact same problem - he did not find the answer, but instead a workaround. Unfortunately the workaround only works on the inherited control (I can't set InheritanceBehavior for TextBlocks).
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/3501ed19-ab40-4064-81b5-e9b7b9d35b56
My guess is that for TextBlock the foreground property is inherited from its parent unless you explicitly set it on your TextBlock instane. For example if you change the Window or UserControl that this Textblock is in to have a Foreground of blue does it work? One thing you can try doing is in the style instead of setting just Foreground to a color, set TextElement.Foreground. See if that works.
It doesn't explain why the problem exists, but a fix was found here:
http://compositewpf.codeplex.com/discussions/257596
The fix is to load the skins before creating/adding views to the shell.

Set the Style of a control programatically in Silverlight 4?

I am new to Silverlight, but I couldn't find anything about this when I googled it.
I have a button that I am trying to set the style programatically. I have the style defined in a XAML file and I want to pull the style into C# so I can dynamically create a button and assign it this style. So far, this is what I am trying:
button.Style = (Style)Resources["CloseButtonStyle"];
However, it just makes the button have no style. Is there an easy way to do this? I feel like this should be obvious, but I can't get it to work.
You are assuming that your Resources property on the current object is the one that contains the defined style. However, I assume, given the symptoms of your issue, that CloseButtonStyle is actually defined further up the control hierarchy.
Instead, you need to traverse your control hierarchy until you find the resource (or if you know the object that defines it, just refer directly to that object). Unfortunately, Silverlight doesn't include FindResource call like WPF, but it's not too difficult to implement your own.
I can call button1.Style = (Style)Resources["NonExistentKey"]; and it makes my button have no style at all as well, point being that the resource is probably not being found, you won't get an exception.
You directly access the Resources property, but is the style really in the immediate resource dictionary of your Window/UserControl/whatever-you-have?

WPF Visibility based upon global variable value

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.

Resources