Bonjour,
J'essaye actuellement de sauvegarder un xaml.
J'utilise :
Code c# : Sélectionner tout - Visualiser dans une fenêtre à part XamlWriter.Save();
Mais comme expliqué dans msdn, la méthode perd les expression non statiques.
Pour le binding j'ai trouvé une solution:
Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 EditorHelper.Register<BindingExpression, BindingConvertor>();
Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 class BindingConvertor : ExpressionConverter { public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (destinationType == typeof(MarkupExtension)) return true; else return false; } public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(MarkupExtension)) { BindingExpression bindingExpression = value as BindingExpression; if (bindingExpression == null) throw new Exception(); return bindingExpression.ParentBinding; } return base.ConvertTo(context, culture, value, destinationType); } }
Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 static class EditorHelper { public static void Register<T, TC>() { Attribute[] attr = new Attribute[1]; TypeConverterAttribute vConv = new TypeConverterAttribute(typeof(TC)); attr[0] = vConv; TypeDescriptor.AddAttributes(typeof(T), attr); } }
Comme sur des roulettes, et le code est facilement adaptable pour le multibinding.
Mais ça ce corse avec un behavior.
Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 [TypeConverter(typeof(MultiBindingBehaviorConverter))] public static readonly DependencyProperty MultiBindingProperty = DependencyProperty.RegisterAttached( "MultiBinding", typeof(MultiBinding), typeof(MultiBindingBehavior), new FrameworkPropertyMetadata(null, MultiBindingChangedCallback) ); public static MultiBinding GetMultiBinding(DependencyObject obj) { return (MultiBinding)obj.GetValue(MultiBindingProperty); } public static void SetMultiBinding(DependencyObject obj, ElementType value) { obj.SetValue(ElementTypeProperty, value); } private static void MultiBindingChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { Grid aGrid = (d as Grid); MultiBinding aMultiBinding = GetMultiBinding(d); }
Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 <behaviors:MultiBindingBehavior.MultiBinding> <MultiBinding Converter="{StaticResource myTextToTextBlockBrushConv}"> <MultiBinding.ConverterParameter> <x:Array Type="{x:Type sys:Object}"> <sys:String>C:\WINDOWS\Fonts\SQUICKT_.TTF</sys:String> <sys:Double>10</sys:Double> <Color>Black</Color> </x:Array> </MultiBinding.ConverterParameter> <Binding ElementName="Prix1_1_2_Cond"/> <Binding Path="Data.Attributes[Cond].Value"/> </MultiBinding> </behaviors:MultiBindingBehavior.MultiBinding>
L'enregistrement du Multibinding se faisait bien car avant elle était faite dans le Tag de ma Grid.
Et j'ai créé un behavior afin de ne pas l'encombrer.
J'ai exploré quelques pistes, les Expression, CustomTypeProvider et plusieurs autres.
Mais je me suis embrouillé en cours de route.
Quelqu'un aurait il une idée sur une direction à prendre?
Merci pour tout avis et indication.






Répondre avec citation
Partager