Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
MultiChained.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Globalization;
5 using System.Windows.Data;
6 using System.Windows.Markup;
7 
8 namespace SiliconStudio.Presentation.ValueConverters
9 {
10  /// <summary>
11  /// This converter can wrap an <see cref="IMultiValueConverter"/> and chain the result of this converter to up to <see cref="MaxConverterCount"/>
12  /// <see cref="IValueConverter"/> to further convert the resulting value. The first converter takes the value output by the <see cref="MultiConverter"/>,
13  /// and then each converter takes the previous converter output as input value.
14  /// The parameter and target type of each converter can also be specified. <see cref="IValueConverter.ConvertBack"/> is supported and converters are invoked backward.
15  /// </summary>
16  /// <remarks>This converter is also a <see cref="MarkupExtension"/>, which makes it convenient to use in XAML.</remarks>
18  {
19  private readonly Chained chainedConverter;
20 
21  /// <summary>
22  /// The maximum number of converters that can be chained
23  /// </summary>
24  public const int MaxConverterCount = Chained.MaxConverterCount;
25 
26  /// <summary>
27  /// Initializes a new instance of the <see cref="MultiChained"/> class.
28  /// </summary>
29  public MultiChained()
30  : this(null, null, null, null, null, null, null, null, null)
31  {
32  }
33 
34  /// <summary>
35  /// Initializes a new instance of the <see cref="MultiChained"/> class with the given instances of <see cref="IValueConverter"/>.
36  /// </summary>
37  /// <param name="multiConverter">The multi-value converter.</param>
38  /// <param name="converter">The first value converter.</param>
39  public MultiChained(IMultiValueConverter multiConverter, IValueConverter converter)
40  : this(multiConverter, converter, null, null, null, null, null, null, null)
41  {
42  }
43 
44  /// <summary>
45  /// Initializes a new instance of the <see cref="MultiChained"/> class with the given instances of <see cref="IValueConverter"/>.
46  /// </summary>
47  /// <param name="multiConverter">The multi-value converter.</param>
48  /// <param name="converter1">The first value converter.</param>
49  /// <param name="converter2">The second value converter.</param>
50  public MultiChained(IMultiValueConverter multiConverter, IValueConverter converter1, IValueConverter converter2)
51  : this(multiConverter, converter1, converter2, null, null, null, null, null, null)
52  {
53  }
54 
55  /// <summary>
56  /// Initializes a new instance of the <see cref="MultiChained"/> class with the given instances of <see cref="IValueConverter"/>.
57  /// </summary>
58  /// <param name="multiConverter">The multi-value converter.</param>
59  /// <param name="converter1">The first value converter.</param>
60  /// <param name="converter2">The second value converter.</param>
61  /// <param name="converter3">The third value converter.</param>
62  public MultiChained(IMultiValueConverter multiConverter, IValueConverter converter1, IValueConverter converter2, IValueConverter converter3)
63  : this(multiConverter, converter1, converter2, converter3, null, null, null, null, null)
64  {
65  }
66 
67  /// <summary>
68  /// Initializes a new instance of the <see cref="MultiChained"/> class with the given instances of <see cref="IValueConverter"/>.
69  /// </summary>
70  /// <param name="multiConverter">The multi-value converter.</param>
71  /// <param name="converter1">The first value converter.</param>
72  /// <param name="converter2">The second value converter.</param>
73  /// <param name="converter3">The third value converter.</param>
74  /// <param name="converter4">The fourth value converter.</param>
75  public MultiChained(IMultiValueConverter multiConverter, IValueConverter converter1, IValueConverter converter2, IValueConverter converter3, IValueConverter converter4)
76  : this(multiConverter, converter1, converter2, converter3, converter4, null, null, null, null)
77  {
78  }
79 
80  /// <summary>
81  /// Initializes a new instance of the <see cref="MultiChained"/> class with the given instances of <see cref="IValueConverter"/>.
82  /// </summary>
83  /// <param name="multiConverter">The multi-value converter.</param>
84  /// <param name="converter1">The first value converter.</param>
85  /// <param name="converter2">The second value converter.</param>
86  /// <param name="converter3">The third value converter.</param>
87  /// <param name="converter4">The fourth value converter.</param>
88  /// <param name="converter5">The fifth value converter.</param>
89  public MultiChained(IMultiValueConverter multiConverter, IValueConverter converter1, IValueConverter converter2, IValueConverter converter3, IValueConverter converter4,
90  IValueConverter converter5)
91  : this(multiConverter, converter1, converter2, converter3, converter4, converter5, null, null, null)
92  {
93  }
94 
95  /// <summary>
96  /// Initializes a new instance of the <see cref="MultiChained"/> class with the given instances of <see cref="IValueConverter"/>.
97  /// </summary>
98  /// <param name="multiConverter">The multi-value converter.</param>
99  /// <param name="converter1">The first value converter.</param>
100  /// <param name="converter2">The second value converter.</param>
101  /// <param name="converter3">The third value converter.</param>
102  /// <param name="converter4">The fourth value converter.</param>
103  /// <param name="converter5">The fifth value converter.</param>
104  /// <param name="converter6">The sixth value converter.</param>
105  public MultiChained(IMultiValueConverter multiConverter, IValueConverter converter1, IValueConverter converter2, IValueConverter converter3, IValueConverter converter4,
106  IValueConverter converter5, IValueConverter converter6)
107  : this(multiConverter, converter1, converter2, converter3, converter4, converter5, converter6, null, null)
108  {
109  }
110 
111  /// <summary>
112  /// Initializes a new instance of the <see cref="MultiChained"/> class with the given instances of <see cref="IValueConverter"/>.
113  /// </summary>
114  /// <param name="multiConverter">The multi-value converter.</param>
115  /// <param name="converter1">The first value converter.</param>
116  /// <param name="converter2">The second value converter.</param>
117  /// <param name="converter3">The third value converter.</param>
118  /// <param name="converter4">The fourth value converter.</param>
119  /// <param name="converter5">The fifth value converter.</param>
120  /// <param name="converter6">The sixth value converter.</param>
121  /// <param name="converter7">The seventh value converter.</param>
122  public MultiChained(IMultiValueConverter multiConverter, IValueConverter converter1, IValueConverter converter2, IValueConverter converter3, IValueConverter converter4,
123  IValueConverter converter5, IValueConverter converter6, IValueConverter converter7)
124  : this(multiConverter, converter1, converter2, converter3, converter4, converter5, converter6, converter7, null)
125  {
126  }
127 
128  /// <summary>
129  /// Initializes a new instance of the <see cref="MultiChained"/> class with the given instances of <see cref="IValueConverter"/>.
130  /// </summary>
131  /// <param name="multiConverter">The multi-value converter.</param>
132  /// <param name="converter1">The first value converter.</param>
133  /// <param name="converter2">The second value converter.</param>
134  /// <param name="converter3">The third value converter.</param>
135  /// <param name="converter4">The fourth value converter.</param>
136  /// <param name="converter5">The fifth value converter.</param>
137  /// <param name="converter6">The sixth value converter.</param>
138  /// <param name="converter7">The seventh value converter.</param>
139  /// <param name="converter8">The eighth value converter.</param>
140  public MultiChained(IMultiValueConverter multiConverter, IValueConverter converter1, IValueConverter converter2, IValueConverter converter3, IValueConverter converter4,
141  IValueConverter converter5, IValueConverter converter6, IValueConverter converter7, IValueConverter converter8)
142  {
143  chainedConverter = new Chained();
144  MultiConverter = multiConverter;
145  Converter1 = converter1;
146  Converter2 = converter2;
147  Converter3 = converter3;
148  Converter4 = converter4;
149  Converter5 = converter5;
150  Converter6 = converter6;
151  Converter7 = converter7;
152  Converter8 = converter8;
153  }
154 
155  /// <summary>
156  /// Gets or sets the multi value converter, when this converter is used as an <see cref="IMultiValueConverter"/>
157  /// </summary>
158  public IMultiValueConverter MultiConverter { get; set; }
159  /// <summary>
160  /// Gets or sets the parameter of the multi value converter, when this converter is used as an <see cref="IMultiValueConverter"/>.
161  /// </summary>
162  public object MultiConverterParameter { get; set; }
163  /// <summary>
164  /// Gets or sets the target type of the multi value converter, when this converter is used as an <see cref="IMultiValueConverter"/>.
165  /// </summary>
166  public Type MultiConverterTargetType { get; set; }
167 
168  /// <summary>
169  /// Gets or sets the first converter to chain.
170  /// </summary>
171  public IValueConverter Converter1 { get { return chainedConverter.Converter1; } set { chainedConverter.Converter1 = value; } }
172  /// <summary>
173  /// Gets or sets the second converter to chain.
174  /// </summary>
175  public IValueConverter Converter2 { get { return chainedConverter.Converter2; } set { chainedConverter.Converter2 = value; } }
176  /// <summary>
177  /// Gets or sets the third converter to chain.
178  /// </summary>
179  public IValueConverter Converter3 { get { return chainedConverter.Converter3; } set { chainedConverter.Converter3 = value; } }
180  /// <summary>
181  /// Gets or sets the fourth converter to chain.
182  /// </summary>
183  public IValueConverter Converter4 { get { return chainedConverter.Converter4; } set { chainedConverter.Converter4 = value; } }
184  /// <summary>
185  /// Gets or sets the fifth converter to chain.
186  /// </summary>
187  public IValueConverter Converter5 { get { return chainedConverter.Converter5; } set { chainedConverter.Converter5 = value; } }
188  /// <summary>
189  /// Gets or sets the sixth converter to chain.
190  /// </summary>
191  public IValueConverter Converter6 { get { return chainedConverter.Converter6; } set { chainedConverter.Converter6 = value; } }
192  /// <summary>
193  /// Gets or sets the seventh converter to chain.
194  /// </summary>
195  public IValueConverter Converter7 { get { return chainedConverter.Converter7; } set { chainedConverter.Converter7 = value; } }
196  /// <summary>
197  /// Gets or sets the eighth converter to chain.
198  /// </summary>
199  public IValueConverter Converter8 { get { return chainedConverter.Converter8; } set { chainedConverter.Converter8 = value; } }
200 
201  /// <summary>
202  /// Gets or sets the parameter of the first converter to chain.
203  /// </summary>
204  public object Parameter1 { get { return chainedConverter.Parameter1; } set { chainedConverter.Parameter1 = value; } }
205  /// <summary>
206  /// Gets or sets the parameter of the second converter to chain.
207  /// </summary>
208  public object Parameter2 { get { return chainedConverter.Parameter2; } set { chainedConverter.Parameter2 = value; } }
209  /// <summary>
210  /// Gets or sets the parameter of the third converter to chain.
211  /// </summary>
212  public object Parameter3 { get { return chainedConverter.Parameter3; } set { chainedConverter.Parameter3 = value; } }
213  /// <summary>
214  /// Gets or sets the parameter of the fourth converter to chain.
215  /// </summary>
216  public object Parameter4 { get { return chainedConverter.Parameter4; } set { chainedConverter.Parameter4 = value; } }
217  /// <summary>
218  /// Gets or sets the parameter of the fifth converter to chain.
219  /// </summary>
220  public object Parameter5 { get { return chainedConverter.Parameter5; } set { chainedConverter.Parameter5 = value; } }
221  /// <summary>
222  /// Gets or sets the parameter of the sixth converter to chain.
223  /// </summary>
224  public object Parameter6 { get { return chainedConverter.Parameter6; } set { chainedConverter.Parameter6 = value; } }
225  /// <summary>
226  /// Gets or sets the parameter of the seventh converter to chain.
227  /// </summary>
228  public object Parameter7 { get { return chainedConverter.Parameter7; } set { chainedConverter.Parameter7 = value; } }
229  /// <summary>
230  /// Gets or sets the parameter of the eighth converter to chain.
231  /// </summary>
232  public object Parameter8 { get { return chainedConverter.Parameter8; } set { chainedConverter.Parameter8 = value; } }
233 
234  /// <summary>
235  /// Gets or sets the target type of the first converter to chain.
236  /// </summary>
237  public Type TargetType1 { get { return chainedConverter.TargetType1; } set { chainedConverter.TargetType1 = value; } }
238  /// <summary>
239  /// Gets or sets the target type of the second converter to chain.
240  /// </summary>
241  public Type TargetType2 { get { return chainedConverter.TargetType2; } set { chainedConverter.TargetType2 = value; } }
242  /// <summary>
243  /// Gets or sets the target type of the third converter to chain.
244  /// </summary>
245  public Type TargetType3 { get { return chainedConverter.TargetType3; } set { chainedConverter.TargetType3 = value; } }
246  /// <summary>
247  /// Gets or sets the target type of the fourth converter to chain.
248  /// </summary>
249  public Type TargetType4 { get { return chainedConverter.TargetType4; } set { chainedConverter.TargetType4 = value; } }
250  /// <summary>
251  /// Gets or sets the target type of the fifth converter to chain.
252  /// </summary>
253  public Type TargetType5 { get { return chainedConverter.TargetType5; } set { chainedConverter.TargetType5 = value; } }
254  /// <summary>
255  /// Gets or sets the target type of the sixth converter to chain.
256  /// </summary>
257  public Type TargetType6 { get { return chainedConverter.TargetType6; } set { chainedConverter.TargetType6 = value; } }
258  /// <summary>
259  /// Gets or sets the target type of the seventh converter to chain.
260  /// </summary>
261  public Type TargetType7 { get { return chainedConverter.TargetType7; } set { chainedConverter.TargetType7 = value; } }
262  /// <summary>
263  /// Gets or sets the target type of the eighth converter to chain.
264  /// </summary>
265  public Type TargetType8 { get { return chainedConverter.TargetType8; } set { chainedConverter.TargetType8 = value; } }
266 
267  /// <inheritdoc/>
268  public override object ProvideValue(IServiceProvider serviceProvider)
269  {
270  return this;
271  }
272 
273  /// <inheritdoc/>
274  public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
275  {
276  if (MultiConverter == null) throw new InvalidOperationException("No multi value converter has been set.");
277  var result = MultiConverter.Convert(values, MultiConverterTargetType ?? typeof(object), MultiConverterParameter, culture);
278  return chainedConverter.Convert(result, targetType, parameter, culture);
279  }
280 
281  /// <inheritdoc/>
282  public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
283  {
284  if (MultiConverter == null) throw new InvalidOperationException("No multi value converter has been set.");
285  var result = chainedConverter.ConvertBack(value, MultiConverterTargetType ?? typeof(object), parameter, culture);
286  return MultiConverter.ConvertBack(result, targetTypes, MultiConverterParameter, culture);
287  }
288  }
289 }
object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
MultiChained(IMultiValueConverter multiConverter, IValueConverter converter1, IValueConverter converter2, IValueConverter converter3, IValueConverter converter4, IValueConverter converter5, IValueConverter converter6, IValueConverter converter7)
Initializes a new instance of the MultiChained class with the given instances of IValueConverter.
MultiChained(IMultiValueConverter multiConverter, IValueConverter converter1, IValueConverter converter2, IValueConverter converter3, IValueConverter converter4, IValueConverter converter5, IValueConverter converter6)
Initializes a new instance of the MultiChained class with the given instances of IValueConverter.
MultiChained()
Initializes a new instance of the MultiChained class.
Definition: MultiChained.cs:29
MultiChained(IMultiValueConverter multiConverter, IValueConverter converter1, IValueConverter converter2, IValueConverter converter3, IValueConverter converter4, IValueConverter converter5, IValueConverter converter6, IValueConverter converter7, IValueConverter converter8)
Initializes a new instance of the MultiChained class with the given instances of IValueConverter.
MultiChained(IMultiValueConverter multiConverter, IValueConverter converter1, IValueConverter converter2, IValueConverter converter3, IValueConverter converter4, IValueConverter converter5)
Initializes a new instance of the MultiChained class with the given instances of IValueConverter.
Definition: MultiChained.cs:89
override object ProvideValue(IServiceProvider serviceProvider)
MultiChained(IMultiValueConverter multiConverter, IValueConverter converter1, IValueConverter converter2, IValueConverter converter3, IValueConverter converter4)
Initializes a new instance of the MultiChained class with the given instances of IValueConverter.
Definition: MultiChained.cs:75
MultiChained(IMultiValueConverter multiConverter, IValueConverter converter)
Initializes a new instance of the MultiChained class with the given instances of IValueConverter.
Definition: MultiChained.cs:39
This converter can wrap an IMultiValueConverter and chain the result of this converter to up to MaxCo...
Definition: MultiChained.cs:17
MultiChained(IMultiValueConverter multiConverter, IValueConverter converter1, IValueConverter converter2)
Initializes a new instance of the MultiChained class with the given instances of IValueConverter.
Definition: MultiChained.cs:50
MultiChained(IMultiValueConverter multiConverter, IValueConverter converter1, IValueConverter converter2, IValueConverter converter3)
Initializes a new instance of the MultiChained class with the given instances of IValueConverter.
Definition: MultiChained.cs:62
This converter can chain up to MaxConverterCount IValueConverter to convert a value. The first converter takes the value parameter of the Chained value converter itself, and then each converter takes the previous converter output as input value. The parameter and target type of each converter can also be specified. IValueConverter.ConvertBack is supported and converters are invoked backward.
Definition: Chained.cs:16