Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Enum types with DataTriggerBehavior #157

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 105 additions & 45 deletions samples/CS/XAMLBehaviorsSample/DataTriggerControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,23 @@
d:DesignWidth="800">

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="7*" />
</Grid.RowDefinitions>
<Pivot>
<PivotItem Header="GreaterThan">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<Viewbox VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,0,0,10">
<TextBlock Text="DataTriggerBehavior" FontSize="40" VerticalAlignment="Stretch" Foreground="{StaticResource DarkGreyBrush}" />
</Viewbox>

<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<ScrollViewer Grid.Column="1" ScrollViewer.VerticalScrollBarVisibility="Auto" >
<StackPanel >
<TextBlock TextWrapping="WrapWholeWords" Margin="10,0,0,0" FontSize="20" Foreground="{StaticResource DarkGreyBrush}">
<TextBlock.Text>
DataTriggerBehavior performs an action when the data the behaviors is bound to meets a specified condition.
<ScrollViewer Grid.Column="1" ScrollViewer.VerticalScrollBarVisibility="Auto" >
<StackPanel >
<TextBlock TextWrapping="WrapWholeWords" Margin="10,0,0,0" FontSize="20" Foreground="{StaticResource DarkGreyBrush}">
<TextBlock.Text>
DataTriggerBehavior performs an action when the data the behaviors is bound to meets a specified condition.
In this example, when the bound data of the slider's value reaches above 50, the behavior triggers an action to change the color of the rectangle.
</TextBlock.Text>
</TextBlock>
<RichTextBlock TextWrapping="WrapWholeWords" Margin="10,20,0,0" FontSize="15" Foreground="{StaticResource RoyalBlueBrush}" xml:space="preserve" IsTextSelectionEnabled="True">
</TextBlock.Text>
</TextBlock>
<RichTextBlock TextWrapping="WrapWholeWords" Margin="10,20,0,0" FontSize="15" Foreground="{StaticResource RoyalBlueBrush}" xml:space="preserve" IsTextSelectionEnabled="True">
<Paragraph>&lt;Rectangle x:Name="DataTriggerRectangle"&gt;
&lt;Interactivity:Interaction.Behaviors&gt;
&lt;Interactions:DataTriggerBehavior Binding="{Binding Value, ElementName=slider}" ComparisonCondition="GreaterThan" Value="50"&gt;
Expand All @@ -46,29 +39,96 @@
&lt;/Interactivity:Interaction.Behaviors&gt;
&lt;/Rectangle&gt;</Paragraph>
</RichTextBlock>
</StackPanel>
</ScrollViewer>
<Grid Grid.Column="0" Margin="0,0,10,0">
<Grid.RowDefinitions>
<RowDefinition Height="5*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Rectangle x:Name="DataTriggerRectangle" Grid.Row="0" Fill="{StaticResource RoyalBlueBrush}" Stroke="{StaticResource HighlightBrush}" StrokeThickness="5">
<Interactivity:Interaction.Behaviors>
<Interactions:DataTriggerBehavior Binding="{Binding Value, ElementName=slider}" ComparisonCondition="GreaterThan" Value="50">
<Interactions:ChangePropertyAction TargetObject="{Binding ElementName=DataTriggerRectangle}" PropertyName="Fill" Value="{StaticResource PaleYellowBrush}"/>
</Interactions:DataTriggerBehavior>
<Interactions:DataTriggerBehavior Binding="{Binding Value, ElementName=slider}" ComparisonCondition="LessThanOrEqual" Value="50">
<Interactions:ChangePropertyAction TargetObject="{Binding ElementName=DataTriggerRectangle}" PropertyName="Fill" Value="{StaticResource RoyalBlueBrush}"/>
</Interactions:DataTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</Rectangle>
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="{Binding Value, ElementName=slider}" VerticalAlignment="Center" Margin="0,0,10,6" FontSize="26.667" Width="45" Foreground="{StaticResource DarkGreyBrush}"/>
<Slider x:Name="slider" Grid.Row="1" Width="150" Margin="10,0,0,0"/>
</StackPanel>
</Grid>
</StackPanel>
</ScrollViewer>
<Grid Grid.Column="0" Margin="0,0,10,0">
<Grid.RowDefinitions>
<RowDefinition Height="5*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Rectangle x:Name="DataTriggerRectangle" Grid.Row="0" Fill="{StaticResource RoyalBlueBrush}" Stroke="{StaticResource HighlightBrush}" StrokeThickness="5">
<Interactivity:Interaction.Behaviors>
<Interactions:DataTriggerBehavior Binding="{Binding Value, ElementName=slider}" ComparisonCondition="GreaterThan" Value="50">
<Interactions:ChangePropertyAction TargetObject="{Binding ElementName=DataTriggerRectangle}" PropertyName="Fill" Value="{StaticResource PaleYellowBrush}"/>
</Interactions:DataTriggerBehavior>
<Interactions:DataTriggerBehavior Binding="{Binding Value, ElementName=slider}" ComparisonCondition="LessThanOrEqual" Value="50">
<Interactions:ChangePropertyAction TargetObject="{Binding ElementName=DataTriggerRectangle}" PropertyName="Fill" Value="{StaticResource RoyalBlueBrush}"/>
</Interactions:DataTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</Rectangle>
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="{Binding Value, ElementName=slider}" VerticalAlignment="Center" Margin="0,0,10,6" FontSize="26.667" Width="45" Foreground="{StaticResource DarkGreyBrush}"/>
<Slider x:Name="slider" Grid.Row="1" Width="150" Margin="10,0,0,0"/>
</StackPanel>
</Grid>
</Grid>
</PivotItem>
<PivotItem Header="Enum">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>

<ScrollViewer Grid.Column="1" ScrollViewer.VerticalScrollBarVisibility="Auto" >
<StackPanel >
<TextBlock TextWrapping="WrapWholeWords" Margin="10,0,0,0" FontSize="20" Foreground="{StaticResource DarkGreyBrush}">
<TextBlock.Text>
DataTriggerBehavior performs an action when the data the behaviors is bound to meets a specified condition.
In this example, when the bound enum changes its value, the behavior triggers an action to change the color of the rectangle based on which enum value it equals to.
</TextBlock.Text>
</TextBlock>
<RichTextBlock TextWrapping="WrapWholeWords" Margin="10,20,0,0" FontSize="15" Foreground="{StaticResource RoyalBlueBrush}" xml:space="preserve" IsTextSelectionEnabled="True">
<Paragraph>&lt;Rectangle x:Name="DataTriggerEnumRectangle" Grid.Row="0" Fill="{StaticResource RoyalBlueBrush}" Stroke="{StaticResource HighlightBrush}" StrokeThickness="5"&gt;
&lt;Interactivity:Interaction.Behaviors&gt;
&lt;Interactions:DataTriggerBehavior Binding="{Binding State}" Value="StateOne"&gt;
&lt;Interactions:ChangePropertyAction PropertyName="Fill" Value="Red"/&gt;
&lt;/Interactions:DataTriggerBehavior&gt;
&lt;Interactions:DataTriggerBehavior Binding="{Binding State}" Value="StateTwo"&gt;
&lt;Interactions:ChangePropertyAction PropertyName="Fill" Value="Green"/&gt;
&lt;/Interactions:DataTriggerBehavior&gt;
&lt;Interactions:DataTriggerBehavior Binding="{Binding State}" Value="StateThree"&gt;
&lt;Interactions:ChangePropertyAction PropertyName="Fill" Value="Blue"/&gt;
&lt;/Interactions:DataTriggerBehavior&gt;
&lt;/Interactivity:Interaction.Behaviors&gt;
&lt;/Rectangle&gt;</Paragraph>
</RichTextBlock>
</StackPanel>
</ScrollViewer>
<Grid Grid.Column="0" Margin="0,0,10,0">
<Grid.RowDefinitions>
<RowDefinition Height="5*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Rectangle x:Name="DataTriggerEnumRectangle" Grid.Row="0" Fill="{StaticResource RoyalBlueBrush}" Stroke="{StaticResource HighlightBrush}" StrokeThickness="5">
<Interactivity:Interaction.Behaviors>
<Interactions:DataTriggerBehavior Binding="{Binding State}" Value="StateOne">
<Interactions:ChangePropertyAction PropertyName="Fill" Value="Red"/>
</Interactions:DataTriggerBehavior>
<Interactions:DataTriggerBehavior Binding="{Binding State}" Value="StateTwo">
<Interactions:ChangePropertyAction PropertyName="Fill" Value="Green"/>
</Interactions:DataTriggerBehavior>
<Interactions:DataTriggerBehavior Binding="{Binding State}" Value="StateThree">
<Interactions:ChangePropertyAction PropertyName="Fill" Value="Blue"/>
</Interactions:DataTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</Rectangle>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="16" Text="{Binding State}"/>

</Grid>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Button x:Name="b1" Grid.Column="0" HorizontalAlignment="Center" Content="StateOne" Click="B1_Click"/>
<Button x:Name="b2" Grid.Column="1" HorizontalAlignment="Center" Content="StateTwo" Click="B2_Click"/>
<Button x:Name="b3" Grid.Column="2" HorizontalAlignment="Center" Content="StateThree" Click="B3_Click"/>
</Grid>
</Grid>
</Grid>
</PivotItem>
</Pivot>

</Grid>
</UserControl>
44 changes: 43 additions & 1 deletion samples/CS/XAMLBehaviorsSample/DataTriggerControl.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
Expand All @@ -17,11 +19,51 @@

namespace XAMLBehaviorsSample
{
public sealed partial class DataTriggerControl : UserControl

public sealed partial class DataTriggerControl : UserControl, INotifyPropertyChanged
{
private StateEnum _state;

public event PropertyChangedEventHandler PropertyChanged;
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

public StateEnum State {
get
{
return _state;
}
set
{
_state = value;
NotifyPropertyChanged();
}
}

public DataTriggerControl()
{
this.InitializeComponent();
this.DataContext = this;
}

private void B1_Click(object sender, RoutedEventArgs e)
{
this.State = StateEnum.StateOne;
}

private void B2_Click(object sender, RoutedEventArgs e)
{
this.State = StateEnum.StateTwo;
}

private void B3_Click(object sender, RoutedEventArgs e)
{
this.State = StateEnum.StateThree;
}
}
}
10 changes: 10 additions & 0 deletions samples/CS/XAMLBehaviorsSample/StateEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XAMLBehaviorsSample
{
public enum StateEnum { StateOne, StateTwo, StateThree };
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private static bool Compare(object leftOperand, ComparisonConditionType operator
{
if (leftOperand != null && rightOperand != null)
{
rightOperand = TypeConverterHelper.Convert(rightOperand.ToString(), leftOperand.GetType().FullName);
rightOperand = TypeConverterHelper.Convert(rightOperand.ToString(), leftOperand.GetType());
}

IComparable leftComparableOperand = leftOperand as IComparable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Microsoft.Xaml.Interactions.Core
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Markup;
using Interactivity;
using System.Reflection;

/// <summary>
/// A helper class that enables converting values specified in markup (strings) to their object representation.
Expand Down Expand Up @@ -63,6 +64,15 @@ public static Object Convert(string value, string destinationTypeFullName)

return null;
}
public static Object Convert(string value, Type destinationType)
{
var typeInfo = destinationType.GetTypeInfo();

if (typeInfo.IsEnum)
return Enum.Parse(destinationType, value);
pdecostervgls marked this conversation as resolved.
Show resolved Hide resolved

return Convert(value, destinationType.FullName);
}

private static String GetScope(string name)
{
Expand Down