Tuesday, January 31, 2012

Correlated Action ComboBox Behavior


<UserControl xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:loc="clr-namespace:TestCorrTextBoxCustBehavior">

       <ComboBox Height="23" HorizontalAlignment="Left" Margin="12,48,0,0" Name="cb1" VerticalAlignment="Top" Width="120" >
            <i:Interaction.Behaviors>
                <loc:CorrelatedActionComboBoxBehavior SourceRegularExpression="['Basket Swap'|'CDS']" SourceTriggerType="RegExpression"  TargetControlId="lb1" TargetActionType="SetValue" TargetValue="2"/>
            </i:Interaction.Behaviors>
            <ComboBoxItem>1</ComboBoxItem>
            <ComboBoxItem>Basket Swap</ComboBoxItem>
            <ComboBoxItem>CDS</ComboBoxItem>
        </ComboBox>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="268,48,0,0" Name="textBox2"  VerticalAlignment="Top" Width="120" />
        <ListBox Name="lb1" Width="100" Height="200" Margin="355,91,48,20">
            <ListBoxItem>1</ListBoxItem>
            <ListBoxItem>2</ListBoxItem>
            <ListBoxItem>3</ListBoxItem>
        </ListBox>



   public class CorrelatedActionComboBoxBehavior : Behavior</ComboBox>
    {

        public SourceTriggerType SourceTriggerType { get; set; }
        public string SourceRegularExpression { get; set; }
        public string TargetControlId { get; set; }
        public TargetActionType TargetActionType { get; set; }
        public string TargetValue { get; set; }
       
        protected override void OnAttached()
        {
            if (!(AssociatedObject is ComboBox)) return;
            object obj = AssociatedObject.Parent;
            Control mw = ((System.Windows.Controls.Panel)(obj)).Parent as Window;
            if (mw==null)
             mw = ((System.Windows.Controls.Panel)(obj)).Parent as Control;
            if (mw == null) return;

            AssociatedObject.DropDownClosed += (sender, e) =>
                { 
                    ComboBox cb = AssociatedObject as ComboBox;
                    if (SourceTriggerType == SourceTriggerType.RegExpression)
                    {
                        Regex r = new Regex(SourceRegularExpression);

                        Control c = FindChild(mw, TargetControlId);
                        c.IsEnabled = true;
                        if (r.IsMatch(cb.Text) )
                        {
                           if ( TargetActionType== TargetActionType.Disable ) c.IsEnabled = false;
                           if (TargetActionType == TargetActionType.SetValue)
                           {
                               ListBox lb = c as ListBox;
                               if (lb != null && TargetValue!="") SetListBoxValue(lb, TargetValue);
                           }

                        }
                    }
                };
        }

        private void SetListBoxValue(ListBox lb, string p)
        {
            for(int i=0;i< lb.Items.Count;i++)
            {
                if ((lb.Items[i] as ListBoxItem).Content.ToString()  == p) lb.SelectedIndex = i;
            }
        }

        public static T FindChild(DependencyObject parent, string childName)    where T : DependencyObject {      
            // Confirm parent and childName are valid.   
            if (parent == null) return null;    
            T foundChild = null;    
            int childrenCount = VisualTreeHelper.GetChildrenCount(parent);  
            for (int i = 0; i < childrenCount; i++)   {   
                var child = VisualTreeHelper.GetChild(parent, i);    
                // If the child is not of the request child type child   
                T childType = child as T;    
                if (childType == null)     {     
                    // recursively drill down the tree    
                    foundChild = FindChild(child, childName);     
                    // If the child is found, break so we do not overwrite the found child.     
                    if (foundChild != null) break;     }    
                else if (!string.IsNullOrEmpty(childName))     {   
                    var frameworkElement = child as FrameworkElement;     
                    // If the child's name is set for search     
                    if (frameworkElement != null && frameworkElement.Name == childName)       {    
                        // if the child's name is of the request name       
                        foundChild = (T)child;         break;       }     }  
                else     {       
                    // child element found.      
                    foundChild = (T)child;       break;     }   }  
            return foundChild; } 

     }

    public enum SourceTriggerType
    {
        RegExpression
    }

    public enum TargetActionType
    {
        Disable,
        SetValue
    }

No comments: