public void MultiDataTriggerBasicTest() { Condition condition1 = new Condition { Binding = new Binding { Path = PropertyPath.Parse("X") }, Value = 1 }; Condition condition2 = new Condition { Binding = new Binding { Path = PropertyPath.Parse("Y") }, Value = 2 }; MultiDataTrigger multiDataTrigger = new MultiDataTrigger(); multiDataTrigger.Conditions.Add(condition1); multiDataTrigger.Conditions.Add(condition2); multiDataTrigger.Setters.Add(new Setter { Property = new DependencyPropertyPathElement(Value1Property), Value = 1 }); FrameworkElement element = new FrameworkElement(); element.DataContext = new Point(1, 2); Assert.AreEqual(0, element.GetValue(Value1Property)); element.Triggers.Add(multiDataTrigger); Assert.AreEqual(1, element.GetValue(Value1Property)); element.DataContext = Point.Zero; Assert.AreEqual(0, element.GetValue(Value1Property)); element.DataContext = new Point(1, 2); Assert.AreEqual(1, element.GetValue(Value1Property)); element.Triggers.Remove(multiDataTrigger); Assert.AreEqual(0, element.GetValue(Value1Property)); }
private DataTemplate CreateItemTemplate() { var template = new DataTemplate(); template.VisualTree = new FrameworkElementFactory(typeof(StackPanel)); var txtBlock = new FrameworkElementFactory(typeof(TextBlock)); txtBlock.SetBinding(TextBlock.TextProperty, new Binding("Street1")); template.VisualTree.AppendChild(txtBlock); txtBlock = new FrameworkElementFactory(typeof(TextBlock)); txtBlock.SetBinding(TextBlock.TextProperty, new Binding("City")); template.VisualTree.AppendChild(txtBlock); var trigger = new MultiDataTrigger(); trigger.Conditions.Add( new Condition( new Binding("Cty"), //misspelling "Tallahassee" ) ); trigger.Conditions.Add( new Condition( new Binding("StateOrProvince"), "Florida" ) ); template.Triggers.Add(trigger); return template; }
private Style CreateStyle() { var style = new Style(); var trigger = new MultiDataTrigger(); trigger.Conditions.Add( new Condition( new Binding("Cty"), //misspelling "Tallahassee" ) ); trigger.Conditions.Add( new Condition( new Binding("Age"), 21 ) ); style.Triggers.Add(trigger); return style; }
public MultiDataTriggerItem(MultiDataTrigger trigger, FrameworkElement source, TriggerSource triggerSource) : base(source, TriggerType.MultiDataTrigger, triggerSource) { _trigger = trigger; _source = source; }
// Given a multi data trigger and associated context information, // evaluate the old and new states of the trigger. // The short-circuit logic of multi property trigger applies here too. // we bail if any of the "other" conditions evaluate to false. private static void EvaluateOldNewStates( MultiDataTrigger multiDataTrigger, DependencyObject triggerContainer, BindingBase binding, BindingValueChangedEventArgs changedArgs, UncommonField<HybridDictionary[]> dataField, Style style, FrameworkTemplate frameworkTemplate, out bool oldState, out bool newState ) { BindingBase evaluationBinding = null; object evaluationValue = null; TriggerCondition[] conditions = multiDataTrigger.TriggerConditions; // Set up the default condition: A trigger with no conditions will never evaluate to true. oldState = false; newState = false; for( int i = 0; i < multiDataTrigger.Conditions.Count; i++ ) { evaluationBinding = conditions[i].Binding; Debug.Assert( evaluationBinding != null, "A null binding was encountered in a MultiDataTrigger conditions collection - this is invalid input and should have been caught earlier."); if( evaluationBinding == binding ) { // The binding that changed belonged to the current condition. oldState = conditions[i].ConvertAndMatch(changedArgs.OldValue); newState = conditions[i].ConvertAndMatch(changedArgs.NewValue); if( oldState == newState ) { // There couldn't possibly be a transition here, abort. The // returned values here aren't necessarily the state of the // triggers, but we only care about a transition today. If // we care about actual values, we'll need to continue evaluation. return; } } else { evaluationValue = GetDataTriggerValue(dataField, triggerContainer, evaluationBinding); if( !conditions[i].ConvertAndMatch(evaluationValue) ) { // A condition other than the one changed has evaluated to false. // There couldn't possibly be a transition here, short-circuit and abort. oldState = false; newState = false; return; } } } // We should only get this far only if the binding change causes // a true->false (or vice versa) transition in one of the conditions, // AND that every other condition evaluated to true. return; }
// This block of code attempts to minimize the number of // type operations and assignments involved in figuring out what // trigger type we're dealing here. // Attempted casts are done in the order of decreasing expected frequency. // rearrange as expectations change. private static void DetermineTriggerType( TriggerBase triggerBase, out Trigger trigger, out MultiTrigger multiTrigger, out DataTrigger dataTrigger, out MultiDataTrigger multiDataTrigger, out EventTrigger eventTrigger ) { if( (trigger = (triggerBase as Trigger)) != null ) { multiTrigger = null; dataTrigger = null; multiDataTrigger = null; eventTrigger = null; } else if( (multiTrigger = (triggerBase as MultiTrigger)) != null ) { dataTrigger = null; multiDataTrigger = null; eventTrigger = null; } else if( (dataTrigger = (triggerBase as DataTrigger)) != null ) { multiDataTrigger = null; eventTrigger = null; } else if( (multiDataTrigger = (triggerBase as MultiDataTrigger)) != null ) { eventTrigger = null; } else if( (eventTrigger = (triggerBase as EventTrigger)) != null ) { ; // Do nothing - eventTrigger is now non-null, and everything else has been set to null. } else { // None of the above - the caller is expected to throw an exception // stating that the trigger type is not supported. } }
private static void SetAllColumns(Grid grid) { grid.ColumnDefinitions.Clear(); var pgList = GetAllColumns(grid); foreach (var productgroup in pgList) { var cd = new ColumnDefinition(); cd.Width = new GridLength(ProductGroup.HeaderColumnWidth, GridUnitType.Pixel); grid.ColumnDefinitions.Add(cd); foreach (var product in productgroup.Products) { foreach (var rework in product.Reworks) { cd = new ColumnDefinition(); cd.DataContext = rework; var style = new Style(typeof(ColumnDefinition)); style.Setters.Add(new Setter( ColumnDefinition.WidthProperty, new GridLength(ProductGroup.ColumnWidth, GridUnitType.Pixel))); #region open animation var animation = new GridLengthAnimation(); animation.From = new GridLength(0, GridUnitType.Pixel); animation.To = new GridLength(ProductGroup.ColumnWidth, GridUnitType.Pixel); animation.Duration = TimeSpan.FromSeconds(0.2); Storyboard.SetTargetProperty(animation, new PropertyPath("Width")); var storyboard = new Storyboard(); storyboard.Children.Add(animation); #endregion #region close animation var animation2 = new GridLengthAnimation(); animation2.From = new GridLength(ProductGroup.ColumnWidth, GridUnitType.Pixel); animation2.To = new GridLength(0, GridUnitType.Pixel); animation2.Duration = TimeSpan.FromSeconds(0.2); Storyboard.SetTargetProperty(animation2, new PropertyPath("Width")); var storyboard2 = new Storyboard(); storyboard2.Children.Add(animation2); #endregion if (rework.IsRework) { var multitrigger = new MultiDataTrigger(); multitrigger.Conditions.Add(new Condition(new Binding("Product.IsExpanded"), true)); multitrigger.Conditions.Add(new Condition(new Binding("Product.ProductGroup.IsExpanded"), true)); multitrigger.EnterActions.Add(new BeginStoryboard { Storyboard = storyboard }); multitrigger.ExitActions.Add(new BeginStoryboard { Storyboard = storyboard2 }); style.Triggers.Add(multitrigger); } else { var trigger = new DataTrigger(); trigger.Binding = new Binding("Product.ProductGroup.IsExpanded"); trigger.Value = true; trigger.EnterActions.Add(new BeginStoryboard { Storyboard = storyboard }); trigger.ExitActions.Add(new BeginStoryboard { Storyboard = storyboard2 }); style.Triggers.Add(trigger); } cd.Style = style; grid.ColumnDefinitions.Add(cd); } } } grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); }
private static TriggerBase CreateTrigger(EntityFieldInfo fieldInfo, IDataService dataService, GenericDataListState state, Nullable<Boolean> isLocked, FrameworkElement element) { TriggerBase trigger = null; if (isLocked.HasValue) { MultiDataTrigger multiDataTrigger = new MultiDataTrigger(); Condition conditionState = new Condition(); conditionState.Binding = CreateBindingState(element); conditionState.Value = state; multiDataTrigger.Conditions.Add(conditionState); Condition conditionIsLocked = new Condition(); conditionIsLocked.Binding = CreateBindingIsLocked(element); conditionIsLocked.Value = isLocked.Value; multiDataTrigger.Conditions.Add(conditionIsLocked); multiDataTrigger.Setters.Add(DataControlTemplateBuilder.CreateSetter(fieldInfo, dataService, state, isLocked, element)); trigger = multiDataTrigger; } else { DataTrigger dataTrigger = new DataTrigger(); dataTrigger.Binding = DataControlTemplateBuilder.CreateBindingState(element); dataTrigger.Value = state; dataTrigger.Setters.Add(DataControlTemplateBuilder.CreateSetter(fieldInfo, dataService, state, isLocked, element)); trigger = dataTrigger; } return trigger; }
private void ProcessVisualTriggers(Style style) { // Walk down to bottom of based-on chain if (style == null) { return; } ProcessVisualTriggers(style._basedOn); if (style._visualTriggers != null) { // Merge in "self" and child TriggerBase PropertyValues while walking // back up the tree. "Based-on" style rules are always added first // (lower priority) int triggerCount = style._visualTriggers.Count; for (int i = 0; i < triggerCount; i++) { TriggerBase trigger = style._visualTriggers[i]; // Set things up to handle Setter values for (int j = 0; j < trigger.PropertyValues.Count; j++) { PropertyValue propertyValue = trigger.PropertyValues[j]; // Check for trigger rules that act on container if (propertyValue.ChildName != StyleHelper.SelfName) { throw new InvalidOperationException(SR.Get(SRID.StyleTriggersCannotTargetTheTemplate)); } TriggerCondition[] conditions = propertyValue.Conditions; for (int k = 0; k < conditions.Length; k++) { if (conditions[k].SourceName != StyleHelper.SelfName) { throw new InvalidOperationException(SR.Get(SRID.TriggerOnStyleNotAllowedToHaveSource, conditions[k].SourceName)); } } // Track properties on the container that are being driven by // the Style so that they can be invalidated during style changes StyleHelper.AddContainerDependent(propertyValue.Property, true /*fromVisualTrigger*/, ref this.ContainerDependents); StyleHelper.UpdateTables(ref propertyValue, ref ChildRecordFromChildIndex, ref TriggerSourceRecordFromChildIndex, ref ResourceDependents, ref _dataTriggerRecordFromBinding, null /*_childIndexFromChildID*/, ref _hasInstanceValues); } // Set things up to handle TriggerActions if (trigger.HasEnterActions || trigger.HasExitActions) { if (trigger is Trigger) { StyleHelper.AddPropertyTriggerWithAction(trigger, ((Trigger)trigger).Property, ref this.PropertyTriggersWithActions); } else if (trigger is MultiTrigger) { MultiTrigger multiTrigger = (MultiTrigger)trigger; for (int k = 0; k < multiTrigger.Conditions.Count; k++) { Condition triggerCondition = multiTrigger.Conditions[k]; StyleHelper.AddPropertyTriggerWithAction(trigger, triggerCondition.Property, ref this.PropertyTriggersWithActions); } } else if (trigger is DataTrigger) { StyleHelper.AddDataTriggerWithAction(trigger, ((DataTrigger)trigger).Binding, ref this.DataTriggersWithActions); } else if (trigger is MultiDataTrigger) { MultiDataTrigger multiDataTrigger = (MultiDataTrigger)trigger; for (int k = 0; k < multiDataTrigger.Conditions.Count; k++) { Condition dataCondition = multiDataTrigger.Conditions[k]; StyleHelper.AddDataTriggerWithAction(trigger, dataCondition.Binding, ref this.DataTriggersWithActions); } } else { throw new InvalidOperationException(SR.Get(SRID.UnsupportedTriggerInStyle, trigger.GetType().Name)); } } // Set things up to handle EventTrigger EventTrigger eventTrigger = trigger as EventTrigger; if (eventTrigger != null) { if (eventTrigger.SourceName != null && eventTrigger.SourceName.Length > 0) { throw new InvalidOperationException(SR.Get(SRID.EventTriggerOnStyleNotAllowedToHaveTarget, eventTrigger.SourceName)); } StyleHelper.ProcessEventTrigger(eventTrigger, null /*_childIndexFromChildID*/, ref _triggerActions, ref EventDependents, null /*_templateFactoryRoot*/, null, ref _eventHandlersStore, ref _hasLoadedChangeHandler); } } } }
// Token: 0x060008DF RID: 2271 RVA: 0x0001CD9C File Offset: 0x0001AF9C private void ProcessVisualTriggers(Style style) { if (style == null) { return; } this.ProcessVisualTriggers(style._basedOn); if (style._visualTriggers != null) { int count = style._visualTriggers.Count; for (int i = 0; i < count; i++) { TriggerBase triggerBase = style._visualTriggers[i]; for (int j = 0; j < triggerBase.PropertyValues.Count; j++) { PropertyValue propertyValue = triggerBase.PropertyValues[j]; if (propertyValue.ChildName != "~Self") { throw new InvalidOperationException(SR.Get("StyleTriggersCannotTargetTheTemplate")); } TriggerCondition[] conditions = propertyValue.Conditions; for (int k = 0; k < conditions.Length; k++) { if (conditions[k].SourceName != "~Self") { throw new InvalidOperationException(SR.Get("TriggerOnStyleNotAllowedToHaveSource", new object[] { conditions[k].SourceName })); } } StyleHelper.AddContainerDependent(propertyValue.Property, true, ref this.ContainerDependents); StyleHelper.UpdateTables(ref propertyValue, ref this.ChildRecordFromChildIndex, ref this.TriggerSourceRecordFromChildIndex, ref this.ResourceDependents, ref this._dataTriggerRecordFromBinding, null, ref this._hasInstanceValues); } if (triggerBase.HasEnterActions || triggerBase.HasExitActions) { if (triggerBase is Trigger) { StyleHelper.AddPropertyTriggerWithAction(triggerBase, ((Trigger)triggerBase).Property, ref this.PropertyTriggersWithActions); } else if (triggerBase is MultiTrigger) { MultiTrigger multiTrigger = (MultiTrigger)triggerBase; for (int l = 0; l < multiTrigger.Conditions.Count; l++) { Condition condition = multiTrigger.Conditions[l]; StyleHelper.AddPropertyTriggerWithAction(triggerBase, condition.Property, ref this.PropertyTriggersWithActions); } } else if (triggerBase is DataTrigger) { StyleHelper.AddDataTriggerWithAction(triggerBase, ((DataTrigger)triggerBase).Binding, ref this.DataTriggersWithActions); } else { if (!(triggerBase is MultiDataTrigger)) { throw new InvalidOperationException(SR.Get("UnsupportedTriggerInStyle", new object[] { triggerBase.GetType().Name })); } MultiDataTrigger multiDataTrigger = (MultiDataTrigger)triggerBase; for (int m = 0; m < multiDataTrigger.Conditions.Count; m++) { Condition condition2 = multiDataTrigger.Conditions[m]; StyleHelper.AddDataTriggerWithAction(triggerBase, condition2.Binding, ref this.DataTriggersWithActions); } } } EventTrigger eventTrigger = triggerBase as EventTrigger; if (eventTrigger != null) { if (eventTrigger.SourceName != null && eventTrigger.SourceName.Length > 0) { throw new InvalidOperationException(SR.Get("EventTriggerOnStyleNotAllowedToHaveTarget", new object[] { eventTrigger.SourceName })); } StyleHelper.ProcessEventTrigger(eventTrigger, null, ref this._triggerActions, ref this.EventDependents, null, null, ref this._eventHandlersStore, ref this._hasLoadedChangeHandler); } } } }