How do xaml elements inherit properties like FontFamily and Foreground? - xaml

I discovered today that when I style something in XAML, if I leave the FontFamily out, it will inherit it from the closest ancestor that has a FontFamily. How is it figuring out which element to reference?
<Page
...
Foreground="Green">
<ContentControl Foreground="Purple">
<TextBlock Text="I am purple unless ContentControl Foreground is not set."/>
</ContentControl>
</Page>
My First thought was that it was using the DataContext somehow, but if the reference object isn't the data context, it still inherits these properties.
I tried googling it, and got a lot of links showing me how to set the Background in a style, but I want to understand how it works when unset...

I can't figure out which UI framework you are using, UWP or WPF. So I write them both.
For WPF, you can read Property Value Inheritance | Microsoft Docs.
For UWP, you can read Dependency properties overview - UWP app developer | Microsoft Docs.
For both WPF and UWP, this kind of property is DependencyProperty. The property value you keep unset is from multiple sources.
WPF
Property system coercion.
Active animations, or animations with a Hold behavior.
Local value. The value you set in your XAML code.
TemplatedParent template properties.
Implicit style.
Style triggers.
Template triggers.
Style setters.
Default (theme) style.
Inheritance. The value you just find out from the ancestors.
Default value from dependency property metadata.
UWP is much simpler
Animated values.
Local value.
Templated properties.
Style setters.
Default value.
You write your own dependency property of WPF as an inherited property, but you cannot do the same in UWP.

Related

Avalonia UI, creating a custom control by drawing things

One could create a control by starting from a container control (like panel) and add other existing controls (like buttons, textbox, etc) on it. But in some cases, there are no such suitable primitive controls and one has to draw things from scratch.
Avalonia UI's Visual Studio extension has a UserControl template, and it seems that it allows adding existing controls using XAML, which is the former case of the previous paragraph. But how to draw from scratch? Where is WinForm's OnPaint() equivalent or WPF's OnRender() equivalent? Is there any example of creating a control from scratch in Avalonia UI?
or WPF's OnRender() equivalent
It's called Render, the name is pretty much the only difference, DrawingContext's API closely resembles WPF one.

How to style a Group background in Windows / Phone 8.1 ListView?

I have a ListView binding to grouped data. What I would like is to be able to bind the background of each group based on the content of the item. GroupStyle.ContainerStyle was deprecated in 8.1 without changing the ItemsPanel of the ListView. This unfortunately comes with a performance hit though.
Is there a way for me to define a background for the entirety of a Group without changing the ItemsPanel of the ListView?
Unfortunately, I can't change the template of the items and header together (though I've tried) because some of the items are duplicated and do not know which group they are in.
There are two different approaches to solve this problem:
add the responsible Property to your ItemTemplate and connect it with a converter, which converts it to a type you need (in your case a BackgroundColor). The problem is, as already mentioned, that this is very costly and in my opinion a converter is not made for such a huge operation. Don't forget that in MVVM the ViewModel should provide all the information which is needed by the one who designs the view.
the better way is to add a Property which is called something like “ItemBackground” and which you bind to the ItemTemplate. I prefer this approach, because there is no logic which background to use in the View.

Force ExpanderView to expand while in the XAML Designer

Is there a way to force the XAML designer to expand and show the contents of an ExpanderView?
I have tried setting the IsExpanded property to true as well as binding it to one of my Design ViewModels that sets it as true and neither method worked.

Is there a WrapPanel (not WrapGrid) control in WinRT-XAML?

A XAML StackPanel aligns controls side-by-side in a single direction. A WrapPanel is similar but like TextWrapping="Wrap" in a XAML TextBox the controls "wrap" to the next column or row when the respective height or width is reached.
Similar, but not the same, WrapGrid wraps content, but in a uniform grid. Though the VariableSizedWrapGrid allows for dissimilar items in the container. Neither of the WrapGrids can be used outside of an ItemsControl. So, they are disqualified.
When developers look in their native XAML Toolbox in Visual Studio there is no WrapPanel. WPF developers had a WrapPanel so they might be looking for this common tool to solve their scenario. So, I have to ask:
Does anyone know of a WrapPanel in XAML-WinRT? (what about one that is virtualized?)
There is one in WinRT XAML Toolkit here. It was ported from Silverlight Toolkit.
I had a same requirement and after googled it for a while, I've decided to use custom control for this. Please find following link for implementation:
http://www.codeproject.com/Articles/463860/WinRT-Custom-WrapPanel
Hope this will help you.

how to create a calculator like interface with xaml?

I wish to use XAML to create a calculator like interface.It should contain buttons upto 1000.
I've read about virtualizing stack panel but i'm not sure how to use it with a listbox and do data binding with that list for the content. Can you please help me with virtualizing stack panel concept
ListBox includes a Virtualizing StackPanel by default, so you dont need to worry about it, or how to implement it. FYI, the Virtualizing StackPanel only creates UI elements (in your case the Buttons), when they come into View. An aditional improvment is to switch on container Recycling:
<ListBox VirtualizingStackPanel.IsVirtualizing="true" VirtualizingStackPanel.VirtualizationMode="Recycling" />