Silverlight & WPF: VisualStateManager in ControlTemplate does not work - wpf

So, hello everybody.
I've made two test programs for creating a own control. One in Silverlight, one in WPF. I created a kind of RangeSlider. This Slider has of course two Orientations, Horizontal and Vertical.
First I used two different techniques to create my RangeSlider. In WPF I used Triggers, in Silverlight (u know there arent Triggers) I changed the Visibility of the Horizontal and Vertical Template in CodeBehind.This runs.
Now:
I'm trying to use one technique for both, Silverlight and WPF. Therefore I use VisualStateManager.
I've a Template defining two Sliders (one for left value, the other for right value). Simplified on the important values it looks like that:
...
<ControlTemplate>
<Grid x:Name="PART_Content">
<!-- VSM: See following code sequence -->
<Grid x:Name="PART_HorizontalTemplate">
<Slider x:Name="PART_HorizontalSliderLeft"
Template="{StaticResource HorizontalSliderTemplate}"
Orientation="{TemplateBinding Orientation}" />
...
</Grid>
<Grid x:Name="PART_VerticalTemplate">
...
</Grid>
</Grid>
</ControlTemplate>
Additionally there is the VSM to switch between Horizontal and Vertical look:
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState x:Name="Vertical">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="PART_HorizontalTemplate"
Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame Value="{x:Static Visibility.Collapsed}" KeyTime="0"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Horizontal" />
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
This still runs.
As you see in the code sequence, the visual of Slider is defined by a Template, here HorizontalSliderTemplate (I reduce code again):
<ControlTemplate x:Key="HorizontalSliderTemplate" TargetType="{x:Type Slider}">
<Border x:Name="Border" ...>
<!-- VSM here. Like above. -->
<Grid x:Name="Grid">
<Rectangle x:Name="PART_SelectionRange"/>
<Track x:Name="PART_Track">
...
</Track>
</Grid>
</Border>
</ControlTemplate>
Actually there is also a VerticalSliderTemplate.
But I want to combine both ControlTemplate in one Template and use VSM. Here we come to my problem:
I do not get running the VSM in the 'inner' ControlTemplate. It's nearly the same code as the running VSM-Part, just the TargetName is changed. I do not know how to debug what's running on GoToState, but I believe the VSM in the template is never found and from there never execute.
I can imagine that there's just a little detail missing, but I 'cant see the wood for the trees'. Maybe there is an important thing what I do not know about Templates or about VSM, and I'm off the track.
Or do I have to trigger the 'inner' VSM from outside, or there's a possibility to access onto Elements from 'outside VSM'?
Or is there no access on VSM in 'inner' Templates?
I hope I could explain my problem good enough and there is someone, who knows a solution or maybe a keyword what I can look for. Just entering keywords VSM, ControlTemplate, Storyboard, etc. in google gives no helping hand.
Thanks in advance.

I don't think you can have multiple VSMs inside one ControlTemplate.
Why don't you just use one VSM to toggle both.
<ControlTemplate>
<Grid x:Name="PART_Content">
<Grid x:Name="PART_HorizontalTemplate">
...
</Grid>
<Grid x:Name="PART_VerticalTemplate">
...
</Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState x:Name="Vertical">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="PART_HorizontalTemplate"
Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame Value="{x:Static Visibility.Collapsed}" KeyTime="0"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Horizontal">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="PART_VerticalTemplate"
Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame Value="{x:Static Visibility.Collapsed}" KeyTime="0"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</ControlTemplate>

So, I got a solution on it.
In the 'inner' template I added to the element I want to toggle a DataTrigger. This DataTrigger is bound to the PART_HorizontalTemplate Visibility and contains a Storyboard that do the action I need.
Maybe it's not really the kind of solution I was looking for, because it stretches the code a lot and makes it therefore looking more complex. But - and thats most important - it runs well.

Related

Add Visual State to nested controls with WPF 4

I'd like to know if is it possible to apply a Visual State (in WPF 4) to nested controls. I've got a stack panel that contains some elements I'd like to change according to variation state.
<StackPanel x:Name="panPremioRaggiunto">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup Name="StatiComuni">
<VisualState Name="PremioNonRaggiunto" />
<VisualState Name="PremioRaggiunto">
<Storyboard>
<ColorAnimation Storyboard.TargetName="lblPremioRaggiunto" Storyboard.TargetProperty="Foreground" To="Green" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<TextBlock x:Name="lblPremioRaggiunto">TEXT</TextBlock>
</StackPanel>
When I try to change the state of entire stack panel with this code
VisualStateManager.GoToState(panPremioRaggiunto, "PremioRaggiunto", False)
nothing happens: nested textblock named lblPremioRaggiunto don't change his color according. Can I apply a visual state in this manner?
Thanks,
Danilo.
Yes, VisualStateManager can change the state of any control. You must have some other issue with your code.
Fixed issue: visual states are defined outside a control template so I have to use VisualStateManager.GoToElementState instead of GoToState.

VisualStateManager does nothing (silverlight)

I am building a custom control using studio 2010 and silverlight 4.
I am trying to use the visual state manager.
With the following xml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:SilverView">
<Style TargetType="controls:ScaleImage">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:ScaleImage">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition To="MouseOver"
GeneratedDuration="0:0:.5"/>
<VisualTransition To="Normal"
GeneratedDuration="0:0:.5"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal">
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="img"
Storyboard.TargetProperty="Width"
From="50" To="100"/>
</Storyboard>
</VisualState>
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="img"
Storyboard.TargetProperty="Width"
From="50" To="100"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Image Name="img" Width="50">
<Image.RenderTransform>
<ScaleTransform x:Name="scale"/>
</Image.RenderTransform>
</Image>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Nothing happens when I mouse over the image.
How do I get the image to enlarge when the mouse is over it?
Thanks
The VisualStateManager.VisualStateGroups attached property defines the set of visual states however the names of the groups and the names of the states are just names, they do not actually enable the functionality they describe automatically.
It's up to code in your control to decide when it is in a specific state and then inform the VisualStateManager of that choice. You do that with code like this:-
VisualStateManager.GotoState(this, "MouseOver", true);
Typically you would collect information like whether the mouse is over the control via the various control events and have a central UpdateVisualState function that sets all the appropriate states.
In the XAML above you are only defining state groups and states with names like "MouseOver". You are not actually causing the state to change, as they are apparently not connected to any events.
If you are not already, try using GoToState behaviours to trigger the state changes of your control.
Do you have any more code or XML that triggers a state change?

Animating same property from mutually exclusive VisualStateGroups

My question is simply: is it even possible?
Suppose I want to style a ListBoxItem such that it has a black foreground by default, blue when selected, and red when the mouse is over it. I ended up with something like this:
<!-- assume the default foreground color is black -->
<ControlTemplate TargetType="ListBoxItem">
<Grid Background="{TemplateBinding Background}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimation Duration="0:0:0.2" To="Red" Storyboard.TargetName="contentControl" Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected"/>
<VisualState x:Name="Selected">
<Storyboard>
<ColorAnimation Duration="0:0:0.2" To="Blue" Storyboard.TargetName="contentControl" Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentControl x:Name="contentControl" Foreground="{TemplateBinding Foreground}" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}"/>
</Grid>
</ControlTemplate>
The problem is that the ListBoxItem class has correctly placed selection states in their own visual state group, separate to common states such as mouse over. That means that a ListBoxItem can be in both the selected and mouse over state.
If the ListBoxItem is selected and correctly displayed in blue, mousing over it will revert it to black because it transitions back to the normal state.
Is there any way for me to handle this without resorting to subclassing ListBoxItem and adding my own custom states? Everything I've read suggests that it is not possible, but it seems ridiculously limiting to me. What am I missing?
You basically asking for Foreground to be black AND blue at the same time. Now that is just impossible. This conflict could be resolved if individual states had precedence, like MouseOver > Selected > Normal > Unselected. But it would introduce unnecessary complication to already complicated visual state manager. Typically this situation is resolved by adding new element and animating that element's properties in one of the conflicting state groups.
Custom styled listbox - how can I keep the style for a selected item?

Is it possible for a button click to permanently change an image's source using only a XAML trigger?

I'm trying to solve the following problem on a WPF datagrid:
I've templated the DataGrid items to contain a button - when clicked, it displays the RowDetails for the grid. I'm having trouble getting that button styled the way I want. Specifically, I want it to appear as a "hyperlink" with a small plus-sign icon next to it. When clicked to display the RowDetails, that icon should change to a minus-sign.
Here's the ControlTemplate I'm attempting to apply to the Button:
<ControlTemplate x:Key="linkButtonTemplate" TargetType="{x:Type Button}">
<StackPanel Orientation="Horizontal">
<Image x:Name="expanderIcon" Source="Images/Plus.png"/>
<TextBlock Foreground="Blue" TextDecorations="Underline" Padding="5 0 0 0">
<ContentPresenter />
</TextBlock>
</StackPanel>
</ControlTemplate>
So, when the button is clicked, I want to change that Image's Source property. And when it's clicked again, I want to change it back. I've come across the following suggested solutions, and none seem ideal:
1. Use a Trigger on the Button.IsPressed event: It works! ...Except the image reverts once the event ends. (i.e. once the IsPressed property is no longer true). So it doesn't work.
2. Use an EventTrigger: These at least let you catch the Button's Click event... and
Unlike Trigger, EventTrigger has no
concept of termination of state, so
the action will not be undone once the
condition that raised the event is no
longer true.
So, great! But they appear to only let you respond with Storyboard actions, not a simple property Setter. Unless I'm missing something, these are not meant for this scenario.
3. Use an Attached Behavior: I'm currently delving into this option, but I just can't help thinking there ought to be a XAML-contained means of doing this. If not, so be it - I'll learn Attached Behaviors.
But my hope is that someone has an angle on this that I haven't encountered.
For example, is there a way to hook the property of the DataRow containing the button that indicates if the RowDetails are currently viewable? Seems like you'd need to crack open the code behind to accomplish that, too... Am I wrong?
I would not claim that EventTriggers are not meant for this, single frame animations are fine and can even be found in the default templates of certain controls, e.g.
<VisualState Name="CalendarButtonFocused">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="CalendarButtonFocusVisual" Storyboard.TargetProperty="Visibility" Duration="0">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
So here is an example of how to change the Source that way:
<Image Name="img" Source="C:\Users\Public\1.png"/>
<Button Content="!">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.Target="{x:Reference img}"
Storyboard.TargetProperty="Source">
<DiscreteObjectKeyFrame KeyTime="0:0:0">
<DiscreteObjectKeyFrame.Value>
<BitmapImage UriSource="C:\Users\Public\2.png"/>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Button.Triggers>
</Button>
You could use a retemplated ToggleButton for a scenario like this and bind the images visibilities to the IsChecked Property. IT would be also much easier to bind the Visibility of the RowDetails to that.

Intermittent InavlidCastException in a UserControl.InitializeComponent()

Here is the setup:
I have a Silverlight Control Library "Controls", in which I have a customer control defined for presenting dialogs:
public class Dialog : ContentControl
{
public Dialog()
: base()
{
DefaultStyleKey = typeof(Dialog);
}
<...normal custom control stuff...>
}
also the default style is in generic.xaml:
<Style TargetType="src_general:Dialog">
<Setter Property="Padding" Value="25"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="src_general:Dialog">
<Grid x:Name="RootElement" >
<vsm:VisualStateManager.VisualStateGroups>
<vsm:VisualStateGroup x:Name="DiakogStyleStates">
<vsm:VisualState x:Name="OkCancel">
<Storyboard>
</Storyboard>
</vsm:VisualState>
<vsm:VisualState x:Name="OkOnly">
<Storyboard>
<ObjectAnimationUsingKeyFrames Duration="0:0:0" Storyboard.TargetName="CancelButton" Storyboard.TargetProperty="Visibility" >
<DiscreteObjectKeyFrame KeyTime="0:0:0" >
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</vsm:VisualState>
<vsm:VisualState x:Name="CancelOnly">
<Storyboard>
<ObjectAnimationUsingKeyFrames Duration="0:0:0" Storyboard.TargetName="OkButton" Storyboard.TargetProperty="Visibility" >
<DiscreteObjectKeyFrame KeyTime="0:0:0" >
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</vsm:VisualState>
<vsm:VisualState x:Name="None">
<Storyboard>
<ObjectAnimationUsingKeyFrames Duration="0:0:0" Storyboard.TargetName="CancelButton" Storyboard.TargetProperty="Visibility" >
<DiscreteObjectKeyFrame KeyTime="0:0:0" >
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Duration="0:0:0" Storyboard.TargetName="OkButton" Storyboard.TargetProperty="Visibility" >
<DiscreteObjectKeyFrame KeyTime="0:0:0" >
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</vsm:VisualState>
</vsm:VisualStateGroup>
</vsm:VisualStateManager.VisualStateGroups>
<Popup x:Name="DialogPopup">
<src_general:WindowFrame x:Name="Frame">
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ContentPresenter Grid.Row="0" x:Name="ContentPresenter" Margin="{TemplateBinding Padding}"/>
<!--Action Buttons-->
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right" Margin="15">
<src_general:GlassButton x:Name="CancelButton" Content="Cancel" Margin="2"/>
<src_general:GlassButton x:Name="OkButton" Content="Ok" Margin="2"/>
</StackPanel>
</Grid>
</src_general:WindowFrame>
</Popup>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I use this dialog in a lot of places with no problem. However, in one application, nested about 3-4 usercontrols from the RootVisual, I use it the following way:
<general:Dialog x:Name="AddUpdateDialog" DialogStyle="OkCancel" Title="Add/Update Connection" Closed="AddUpdateDialog_Closed" ValidationGroup="AddConnection">
<Grid Width="300">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="Name:" Style="{StaticResource LabelText}"/>
<TextBox Grid.Row="0" Grid.Column="2" Text="{Binding Name, Mode=TwoWay}" Style="{StaticResource TextBoxInput}" MaxLength="49">
<val:ValidationManager.Validator>
<val:RequiredValidator ManagerName="AddConnection" ErrorMessage="Name is required."/>
</val:ValidationManager.Validator>
</TextBox>
</Grid>
</general:Dialog>
When I run this app I intermittently (about every 5-10 starts get the following exception:
"Unable to cast object of type System.Windows.Controls.ContentControl to type hookitupright.com.silverlight.controls.general.Dialog." that occurs in the InitializeComponent() for the parent UserControl of the above XAML.
To be specific, it occurs right here:
this.AddUpdateDialog = ((hookitupright.com.silverlight.controls.general.Dialog)(this.FindName("AddUpdateDialog")));
When I put a breakpoint there, most of the time the FindName returns a Dialog typed object, but sometimes, it returns a ContentControl (the base for Dialog) and it fails. The XAML has not changed. It is static...Since the exception is intermittent and occurs within generated code I am at a loss.
I have tried:
Moved all of the content for the Dialog into a separate UserControl - only seemed to make problem worse
Comment out parts and see when it works...well, if I comment out the TextBox completely, it no longer fails. Everything else (including the custom validation attached property) seems to have no impact.
2a. Thinking it may have something to do with the TwoWay binding to the TextBox, I removed the binding. Still fail.
UPDATE: So given (2) above I left the Textbox commented out decided to move on to other things and come back to this with hopes that something will reveal itself to me. Unfortunately, it seems to also fail with the Textbox out, just less frequently.
In addition, I have this control in the exact same configuration in another usercontrol in the same app (and at the same level in the VisualTree) and it does not fail at all. So I literally copied and pasted the failing XAML into the Main.xaml (my root visual) and of course, it does not fail there either. Assuming that the the XAML is loaded in sequence (top to bottom) the failing control is likely one of the last ones loaded. My only hypothesis now is that there is some timing thing that is happening whereby as Iam still loading the visual tree, I start to get *Completed events from the loading of the data via WCF service and that these trigger a layout before the visual tree is fully loaded which causes some ill side effects... I will test this.
The problem is that it does NOT fail every time. It blows up about 20% of the time. When it works, everything works even this dialog?
This problem is related if not the same problem: When I "fix" the invalidcast by commenting out needed functionality, I will far less frequently but intermittently get this invalid attribute (when the attribute/property is in fact there).
After much, much research and frustration, I believe it to be related to this: http://blogs.msdn.com/silverlight_sdk/archive/2008/10/24/loaded-event-timing-in-silverlight.aspx
The Loaded event in Silverlight does not match the definition of (a) what it is in WPF and consequently (b) the definition that they pasted into the Silverlight documentation from WPF
WTF???
I have modified some code based on the above and now it does not seem to fail. I wish I could tell you exactly why it did what did and why what I did fixed it (or at least masked it again??), but I can't. Maybe someone else will encounter this and can figure it out.
Here is what I was doing and what I changed:
The Textbox above makes use of an Attached Property for a Validator (like the ASP.NET validators and similar to the ones in the Silverlight Toolkit). When a Validator (base class for RequireValidator) is attached to a control, it links to the control to provide validation as an attached behavior. The trick is that it then tries to Validate() the current control. For a RequiredValidator on a TextBox control it calls string.IsNullOrEmpty() for the Text property on the linkedControl. All of this works fine as these are just properties on the TextBox Control. However, just after that, the validator needs to somehow tell the control to display the error indicator and any error message. I do this through providing a custom Style that includes two new VisualStates ("ValidInput" & "InvalidInput") in their own VisualStateGroup ("ValidationStates") and that one of the Style's Elements is a Control that supports and interface called IValidationNotificationControl.
Ok, enough background. From the documentation, I knew that I could only access the elements of the TextBox Style (the new visualStates and the notification icon) AFTER the template had been applied so I hooked into the Loaded event for the linkelement for the validator and called Validate() there for the first time. Well, someone at MS saved about 15 minutes by copying an pasting the Loaded Event description from WPF and that cost me about 3-4 days of heartache. Turns out that there is no guarantee that the template is applied then in Loaded Event in silverlight. So, I simply added a call to ApplyTemplate() in the Loaded event handler for the linkedelement and the failure went away.
My guess is that the error was intermittent because the Layout (and therefore the application of the Template) occurs asynchronously and sometimes it was there when I hit the Loaded Event and sometimes it was not. But I still think that it is a Silverlight Bug as to where the error manifested itself and may even point to a security hole (if, but some action, somewhere else in the code, I can cause the XAML Parser to return a type different than what teh actual XAMl indicates...). Oh well...it seems to be working now. Maybe this will help someone else.
Maybe this helps someone too: take a look at my answer in another thread:

Resources