VisualStateManager.GoToState for FrameworkElement - c#

I have troubles animating a StackPanel with the VisualStateManager.
VisualStateManager.GoToState() accepts a Control as parameter, but StackPanel is of type UIElement/FrameworkElement.
How can I call VisualStateManager.GoToState() on a StackPanel?

You can use VisualStateManager.GoToElementState
This post might help you: WPF using VisualStateManager to animate panels in & out.
Edit:
The methods above are only for WPF and not available in Silverlight. For Silverlight the ExtendedVisualStateManager from the Blend SDK might be helpful:
http://msdn.microsoft.com/de-de/library/microsoft.expression.interactivity.core.extendedvisualstatemanager.gotoelementstate(v=expression.40).aspx
You would have to include the Microsoft.Expression.Interactions assembly though.

Related

When attaching a property or behavior, how can I access to native control behind xamarin forms elements?

Every Xamarin forms element, has a render, and every renderer has a native control behind itself.
Whenever I develop an Attached property or behavior, I've access to Xamarin forms element, but I've no access to native control behind that.
Is there any way to access that native control?
Note: I'm talking about built-in Xamarin forms elements such as Entry, this is not about new controls, elements and renders.
Thanks in advance.
You should create a custom control, like YourEntry, which inherits built-in Entry, with a custom renderer which inherits the EntryRenderer. There in any overridden method you can call this.Control and this.Element like this:
var entry = (YourEntry)this.Element;
this.Control.Font = UIFont.FromName(entry.FontFamily, (float)entry.FontSize);
this.Control is your native control and this.Element is your Xamarin.Forms control.
I think this article will help you with converting Xamarin.Forms Views into Native Views.
http://www.michaelridland.com/xamarin/creating-native-view-xamarin-forms-viewpage/
Using xamarin platform effects, attached properties and global styles, you can have access to all UIKit controls behind Entry tags in iOS as an example. You can apply some codes to any native control behind xamarin forms components in any platform.
https://medium.com/#ysmoradi/how-to-add-xamarin-native-platform-effect-to-all-elements-9214cf6d7b8a

What WP 8.1 XAML control could look and behave like the system Task switcher?

I'm writing a Windows Phone 8.1 Store app and I need to create a control so that it looks and behaves similar to WP 8.1 system task switcher (that appears when holding back hardware button).
It should show some images and support sliding left or right when swiping. Does anyone know what control should I use or do I need to create a completely new control from scratch?
So, the sollution was easy. The control I was looking for was... a ScrollViewer. It has 2 properties in WinRT XAML that make the ScrollViewer scrolling behave like I wanted: HorizontalSnapPointsAlignment and HorizontalSnapPointsType.
If you want to try this behavior, this MSDN code sample will expose it for you.
One point to mention. If you wish to set such behavior to, for example, a ListView you should firstly get its internal ScrollViewer in code and then set its HorizontalSnapPointsAlignment and HorizontalSnapPointsType properties. You can use GetFirstDescendantOfType<T>() extension method from WinRT XAML toolkit.
var sv = myListView.GetFirstDescendantOfType<ScrollViewer>();
sv.HorizontalSnapPointsAlignment = SnapPointsAlignment.Center;
sv.HorizontalSnapPointsType = SnapPointsType.Mandatory;

How can I add an InkCanvas element to a Winform?

From what I understand so far, the InkCanvas element is in the WPF Framework. To use that, I need an ElementHost control to host the InkCanvas element. I've been to the MSDN links, but the example it gives talks of creating a WPF User Control Library project and so on. It's not that bad, but it seems a bit much to just add a control to a Winform. Is there a simpler way to do this, or am I trying to oversimplify this?
Thanks.
This should work:
ElementHost host = new ElementHost();
InkCanvas ic = new InkCanvas();
host.Child = ic;
Controls.Add(host);
As mentioned in comments, one needs to add the WPF assemblies as reference (WindowsBase, PresentationCore, PresentationFramework).

Bindings not applied to dynamically-loaded xaml

I'm using XamlReader successfully to load a xaml file and create a FrameworkElement to work with.
The xaml I'm loading has binding expressions in it such as:
<TextBlock Text="{Binding DataContextTextProperty}" />
If I place the FrameworkElement I get back from XamlReader.Load() into a WPF window, the binding all works fine.
However, in this case I'm using Laurent Bugnion's excellent article on creating PNGs from WPF/XAML. Since the result of XamlReader.Load() is written directly to a PNG via a VisualBrush, it seems the necessary mechanics of WPF to invoke binding expressions are bypassed.
This leads me to believe that the actual bindings aren't really being invoked just by calling XamlReader.Load(), or that they're not working because of something I don't know about to do with there not being a visual tree until you add the FrameworkElement to an existing visual tree or something.
Is there something I can do to ensure these bindings are invoked?
Many thanks in advance.
I FIXED IT!!
Ahem, allow me to explain...
I have no idea how I got to it now, but I found a helpful-sounding article on MSDN regarding Initialization for Objects Not in an Object Tree.
In it I found the following code example:
Button b = new Button();
b.BeginInit();
b.Background = Brushes.Blue;
b.Width = b.Height = 200;
b.EndInit();
b.Measure(paperSize);
b.Arrange(new Rect(paperSize));
b.UpdateLayout();
I looked at the (again, excellent) example from Laurent that I mentioned in the question above, and customised the use of XamlReader as follows:
var element = (FrameworkElement)XamlReader.Load(xamlInput);
element.BeginInit();
element.DataContext = dataContext;
...
element.Measure(renderingSize);
element.Arrange(renderingRectangle);
element.EndInit();
element.UpdateLayout();
I added the BeginInit(), EndInit() and UpdateLayout() (though by process of elimination I believe UpdateLayout() is the key) and now the binding expressions in my dynamically-loaded xaml are working correctly. Hurrah!

WPF Transparency and Switching Styles between Transparent and Non-Transparent

2 Questions :
Firstly:
Is it possible to Toggle Transparency on a WPF window? Any pointers greatly appreciated!
Secondly:
Most controls on my window inherit their Transparancy from the parent window, however I have a Datagrid control with its own style - The style is in an external file that I reference (Style="{DynamicResource MyDGStyle}")..... in the xaml code behind can I switch Styles? (Ideally I would achieve this using a Style Trigger, but don't think I can).
Thanks very much
Joe
Edit (can't seem to reply)
Thanks alex, NVM
Regarding the Toggling Transparency, as long as I can set the 'Background' property of the Window at runtime from a color to 'Transparent' at that runtime, thats fine.
Regarding switching styles, just extending your code alex, presumably I can do something like
void OnButtonPress()
{
var transparentStyle = Themes.CurrentTheme.MyDGNonTransparentStyle;
var nonTransparentStyle = Themes.CurrentTheme.MyDGNonTransparentStyle;
if (isTransparent) // Change to Non-Transparent
this.MyGrid.Style = (Style)this.FindResource(nonTransparentStyle);
else // Change to Transparent
this.MyGrid.Style = (Style)this.FindResource(nonTransparentStyle);
}
?
Thanks
Joe
3rd Edit
Thanks guys,
Sorry to confuse you - my second question was since my datagrid has its own style (and doesn't inherit from the window) I will need to set its style depending on the current state (Transparent / Non-ransparent) - so I need to change the datagrid style at runtime - now since this can be done with a window, can I assume it can be done with a datagrid?
Thanks
Joe
Is it possible to Toggle Transparency on a WPF window?
Yes, it is:
<Window WindowStyle="None"
AllowsTransparency="True"
Background="#88aa3366">
</Window/>
The bad news is that you have to implement the logic of window header by yourself.
This article might be helpfull.
in the xaml code behind can I switch Styles?
The question is a little bit unclear, maybe this helps:
var key = Themes.CurrentTheme.MyDGStyle;
this.MyGrid.Style = (Style)this.FindResource(key);

Categories

Resources