-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexToBoolConverter.cs
More file actions
48 lines (43 loc) · 1.88 KB
/
Copy pathIndexToBoolConverter.cs
File metadata and controls
48 lines (43 loc) · 1.88 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Globalization;
using System.Windows.Data;
namespace CodingWithCalvin.VsixManifestDesigner.Converters;
/// <summary>
/// Converts an index value to a boolean, returning true if the bound value equals the converter parameter.
/// Used for binding RadioButtons to a SelectedTabIndex property.
/// </summary>
public sealed class IndexToBoolConverter : IValueConverter
{
/// <summary>
/// Converts an integer index to a boolean by comparing with the parameter.
/// </summary>
/// <param name="value">The current index value.</param>
/// <param name="targetType">The target type (bool).</param>
/// <param name="parameter">The index to compare against.</param>
/// <param name="culture">The culture info.</param>
/// <returns>True if value equals parameter; otherwise, false.</returns>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is int index && parameter is string paramString && int.TryParse(paramString, out int paramIndex))
{
return index == paramIndex;
}
return false;
}
/// <summary>
/// Converts a boolean back to an integer index.
/// </summary>
/// <param name="value">The boolean value (IsChecked).</param>
/// <param name="targetType">The target type (int).</param>
/// <param name="parameter">The index value to return if true.</param>
/// <param name="culture">The culture info.</param>
/// <returns>The parameter value if true; otherwise, Binding.DoNothing.</returns>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool isChecked && isChecked && parameter is string paramString && int.TryParse(paramString, out int paramIndex))
{
return paramIndex;
}
return Binding.DoNothing;
}
}