Example #1
0
        private void UpdateDependencyObjectDependencyPropertyWithAttachedPropertiesBasedReplacement(DependencyObject item, BcpBinding BcpBindingN)
        {
            DependencyProperty dp = BcpBindingN.TargetDependencyProperty;

            List <DependencyProperty> ConverterParameters = new List <DependencyProperty>();
            List <DependencyProperty> Bindings            = new List <DependencyProperty>();

            //attached prop for two-way binding operations
            DependencyProperty apIsSourceChanged = GetOrCreateAttachedProperty(dp.Name + "IsSourceChanged", typeof(bool), false);

            //attached prop that stores all ConverterParameters-bindings
            DependencyProperty apConverterParameters = GetOrCreateAttachedProperty(dp.Name + "ConverterParameters", typeof(List <DependencyProperty>), new List <DependencyProperty>());
            //attached prop that stores all MultiBinding-bindings
            DependencyProperty apBindings = GetOrCreateAttachedProperty(dp.Name + "Bindings", typeof(List <DependencyProperty>), new List <DependencyProperty>());

            DependencyProperty apConverter = GetOrCreateAttachedProperty(dp.Name + "Converter", typeof(object), null);

            item.SetValue(apConverter, BcpBindingN.Converter);
            DependencyProperty apEvaluatedResult =
                GetOrCreateAttachedProperty(dp.Name + "EvaluatedResult", typeof(object), null, apEvaluatedResultChanged);

            Binding bindingOrigDpToEvaluatedResult = new Binding("(" + typeof(BindingBase).Name + "." + dp.Name + "EvaluatedResult)");

            bindingOrigDpToEvaluatedResult.Source = item;
            bindingOrigDpToEvaluatedResult.Mode   = BcpBindingN.Mode;
            bindingOrigDpToEvaluatedResult.UpdateSourceTrigger = this.UpdateSourceTrigger;
            BindingOperations.SetBinding(item, dp, bindingOrigDpToEvaluatedResult);

            string[] arrConverterParameters;
            if (BcpBindingN.ConverterParameters.Contains(','))
            {
                arrConverterParameters = BcpBindingN.ConverterParameters.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            }
            else
            {
                var bindings = Regex.Split(BcpBindingN.ConverterParameters, @"Binding ").Where(s => s != "").Select(s => "Binding " + s);
                arrConverterParameters = bindings.ToArray();
            }

            for (int i = 0; i < arrConverterParameters.Length; i++)
            {
                DependencyProperty apConverterParameterBindingSource =
                    GetOrCreateAttachedProperty(dp.Name + "_ConverterParameterBindingSource" + i.ToString(), typeof(object), null, apConverterParameterSourceChanged);
                string  spath        = Regex.Match(arrConverterParameters[i], BINDING_EXTRACT_PATH_REGEX_PATTERN).Groups["path"].Value;
                string  selementname = Regex.Match(arrConverterParameters[i], BINDING_EXTRACT_ELEMENT_NAME_REGEX_PATTERN).Groups["elementname"].Value;
                Binding b            = new Binding(spath);
                if (selementname == "Self")
                {
                    b.Source = item;
                }
                else if (selementname == "TemplatedParent")
                {
                    b.RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent);
                }
                else if (selementname != "")
                {
                    b.ElementName = selementname;
                }
                BindingOperations.SetBinding(item, apConverterParameterBindingSource, b);
                ConverterParameters.Add(apConverterParameterBindingSource);
            }

            item.SetValue(apConverterParameters, ConverterParameters);

            // for single-binding scenarios >>> make it similar to multibinding syntax(with single binding)
            if (BcpBindingN.Path != null)
            {
                BcpBindingN.Bindings = "Binding " + (BcpBindingN.ElementName != "" ? "ElementName=" + BcpBindingN.ElementName : "") + " Path=" + BcpBindingN.Path;
            }
            else
            {
                if (!BcpBindingN.Bindings.Contains("Binding"))
                {
                    BcpBindingN.Bindings = "Binding Path=" + BcpBindingN.Bindings;
                }
            }

            //bind each Binding in the MultiBinding to special dp (instead of the original dp)
            var binds = Regex.Split(BcpBindingN.Bindings, @"Binding ").Where(s => s != "");
            int i1    = 0;

            foreach (var binding in binds)
            {
                string             spath              = Regex.Match(binding, BINDING_EXTRACT_PATH_REGEX_PATTERN).Groups["path"].Value;
                string             selementname       = Regex.Match(binding, BINDING_EXTRACT_ELEMENT_NAME_REGEX_PATTERN).Groups["elementname"].Value;
                DependencyProperty apBinding          = GetOrCreateAttachedProperty(dp.Name + "_Binding" + i1.ToString(), typeof(object), null, apMultiBindingAnySourceChanged);
                Binding            NewBindingToSource = new Binding(spath);
                if (selementname == "Self")
                {
                    NewBindingToSource.Source = item;
                }
                else if (selementname == "TemplatedParent")
                {
                    NewBindingToSource.RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent);
                }
                else if (selementname != "")
                {
                    NewBindingToSource.ElementName = selementname;
                }

                NewBindingToSource.Mode = BcpBindingN.Mode;
                BindingOperations.SetBinding(item, apBinding, NewBindingToSource);
                Bindings.Add(apBinding);
                i1++;
            }
            //save all bindings-dps into apBindings
            item.SetValue(apBindings, Bindings);
        }
Example #2
0
        private void UpdateDependencyObjectDependencyPropertyWithAttachedPropertiesBasedReplacement(DependencyObject item, BcpBinding BcpBindingN)
        {
            DependencyProperty dp = BcpBindingN.TargetDependencyProperty;

            List<DependencyProperty> ConverterParameters = new List<DependencyProperty>();
            List<DependencyProperty> Bindings = new List<DependencyProperty>();

            //attached prop for two-way binding operations
            DependencyProperty apIsSourceChanged = GetOrCreateAttachedProperty(dp.Name + "IsSourceChanged", typeof(bool), false);

            //attached prop that stores all ConverterParameters-bindings
            DependencyProperty apConverterParameters = GetOrCreateAttachedProperty(dp.Name + "ConverterParameters", typeof(List<DependencyProperty>), new List<DependencyProperty>());
            //attached prop that stores all MultiBinding-bindings
            DependencyProperty apBindings = GetOrCreateAttachedProperty(dp.Name + "Bindings", typeof(List<DependencyProperty>), new List<DependencyProperty>());

            DependencyProperty apConverter = GetOrCreateAttachedProperty(dp.Name + "Converter", typeof(object), null);
            item.SetValue(apConverter, BcpBindingN.Converter);
            DependencyProperty apEvaluatedResult =
                GetOrCreateAttachedProperty(dp.Name + "EvaluatedResult", typeof(object), null, apEvaluatedResultChanged);

            Binding bindingOrigDpToEvaluatedResult = new Binding("(" + typeof(BindingBase).Name + "." + dp.Name + "EvaluatedResult)");
            bindingOrigDpToEvaluatedResult.Source = item;
            bindingOrigDpToEvaluatedResult.Mode = BcpBindingN.Mode;
            bindingOrigDpToEvaluatedResult.UpdateSourceTrigger = this.UpdateSourceTrigger;
            BindingOperations.SetBinding(item, dp, bindingOrigDpToEvaluatedResult);
            
            string[] arrConverterParameters;
            if (BcpBindingN.ConverterParameters.Contains(','))
            {
                arrConverterParameters = BcpBindingN.ConverterParameters.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            }
            else
            {
                var bindings = Regex.Split(BcpBindingN.ConverterParameters, @"Binding ").Where(s => s != "").Select(s => "Binding " + s);
                arrConverterParameters = bindings.ToArray();
            }

            for (int i = 0; i < arrConverterParameters.Length; i++)
            {
                DependencyProperty apConverterParameterBindingSource =
                        GetOrCreateAttachedProperty(dp.Name + "_ConverterParameterBindingSource" + i.ToString(), typeof(object), null, apConverterParameterSourceChanged);
                string spath = Regex.Match(arrConverterParameters[i], BINDING_EXTRACT_PATH_REGEX_PATTERN).Groups["path"].Value;
                string selementname = Regex.Match(arrConverterParameters[i], BINDING_EXTRACT_ELEMENT_NAME_REGEX_PATTERN).Groups["elementname"].Value;
                Binding b = new Binding(spath);
                if (selementname == "Self")
                    b.Source = item;
                else if (selementname == "TemplatedParent")
                    b.RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent);
                else if (selementname != "")
                    b.ElementName = selementname;
                BindingOperations.SetBinding(item, apConverterParameterBindingSource, b);
                ConverterParameters.Add(apConverterParameterBindingSource);
            }

            item.SetValue(apConverterParameters, ConverterParameters);

            // for single-binding scenarios >>> make it similar to multibinding syntax(with single binding)
            if (BcpBindingN.Path != null)
            {
                BcpBindingN.Bindings = "Binding " + (BcpBindingN.ElementName != "" ? "ElementName=" + BcpBindingN.ElementName : "") + " Path=" + BcpBindingN.Path;
            }
            else
            {
                if (!BcpBindingN.Bindings.Contains("Binding"))
                {
                    BcpBindingN.Bindings = "Binding Path=" + BcpBindingN.Bindings;
                }
            }

            //bind each Binding in the MultiBinding to special dp (instead of the original dp)
            var binds = Regex.Split(BcpBindingN.Bindings, @"Binding ").Where(s => s != "");
            int i1 = 0;
            foreach (var binding in binds)
            {
                string spath = Regex.Match(binding, BINDING_EXTRACT_PATH_REGEX_PATTERN).Groups["path"].Value;
                string selementname = Regex.Match(binding, BINDING_EXTRACT_ELEMENT_NAME_REGEX_PATTERN).Groups["elementname"].Value;
                DependencyProperty apBinding = GetOrCreateAttachedProperty(dp.Name + "_Binding" + i1.ToString(), typeof(object), null, apMultiBindingAnySourceChanged);
                Binding NewBindingToSource = new Binding(spath);
                if (selementname == "Self")
                    NewBindingToSource.Source = item;
                else if (selementname == "TemplatedParent")
                    NewBindingToSource.RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent);
                else if (selementname != "")
                    NewBindingToSource.ElementName = selementname;

                NewBindingToSource.Mode = BcpBindingN.Mode;
                BindingOperations.SetBinding(item, apBinding, NewBindingToSource);
                Bindings.Add(apBinding);
                i1++;
            }
            //save all bindings-dps into apBindings
            item.SetValue(apBindings, Bindings);
        }