Imports System.Globalization
Imports System.Windows.Data
Imports System.Collections.ObjectModel
Public Class SelectedValueIgnoreCaseConverter
Implements IMultiValueConverter
Public Function Convert(values() As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IMultiValueConverter.Convert
Dim selectedValue As String = TryCast(values(0), String)
Dim options As ObservableCollection(Of String) = TryCast(values(1), ObservableCollection(Of String))
If selectedValue Is Nothing Or options Is Nothing Then
Return Nothing
End If
options.Contains(selectedValue, StringComparer.OrdinalIgnoreCase)
Dim returnValue As String = Utilities.Conversions.ParseNullToString((From o In options Where String.Equals(selectedValue, o, StringComparison.OrdinalIgnoreCase)).FirstOrDefault)
Return returnValue
End Function
Public Function ConvertBack(value As Object, targetTypes() As Type, parameter As Object, culture As CultureInfo) As Object() Implements IMultiValueConverter.ConvertBack
Dim result(2) As Object
result(0) = value
Return result
End Function
End Class
/// <summary>
/// Converts selected value to case ignore.
/// </summary>
/// <seealso cref="System.Windows.Data.IMultiValueConverter" />
public class SelectedValueIgnoreCaseConverter : IMultiValueConverter {
/// <summary>
/// Converts source values to a value for the binding target. The data binding engine calls this method when it propagates the values from source bindings to the binding target.
/// </summary>
/// <param name="values">The array of values that the source bindings in the <see cref="T:System.Windows.Data.MultiBinding" /> produces. The value <see cref="F:System.Windows.DependencyProperty.UnsetValue" /> indicates that the source binding has no value to provide for conversion.</param>
/// <param name="targetType">The type of the binding target property.</param>
/// <param name="parameter">The converter parameter to use.</param>
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>
/// A converted value.If the method returns <see langword="null" />, the valid <see langword="null" /> value is used.A return value of <see cref="T:System.Windows.DependencyProperty" />.<see cref="F:System.Windows.DependencyProperty.UnsetValue" /> indicates that the converter did not produce a value, and that the binding will use the <see cref="P:System.Windows.Data.BindingBase.FallbackValue" /> if it is available, or else will use the default value.A return value of <see cref="T:System.Windows.Data.Binding" />.<see cref="F:System.Windows.Data.Binding.DoNothing" /> indicates that the binding does not transfer the value or use the <see cref="P:System.Windows.Data.BindingBase.FallbackValue" /> or the default value.
/// </returns>
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {
if (typeof(string) != values[0].GetType()) {
return null;
}
string selectedValue = values[0].ToString();
ObservableCollection<string> options = (ObservableCollection<string>) values[1];
if (selectedValue.IsNullOrEmpty()) {
return null;
}
return options.FirstOrDefault(option => option.Equals(selectedValue, StringComparison.OrdinalIgnoreCase));
}
/// <summary>
/// Converts a binding target value to the source binding values.
/// </summary>
/// <param name="value">The value that the binding target produces.</param>
/// <param name="targetTypes">The array of types to convert to. The array length indicates the number and types of values that are suggested for the method to return.</param>
/// <param name="parameter">The converter parameter to use.</param>
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>
/// An array of values that have been converted from the target value back to the source values.
/// </returns>
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) {
object[] result = new object[1];
result[0] = value;
return result;
}
}
3条答案
按热度按时间iswrvxsc1#
我通过实现IMultiValueConverter完成了这一点。
转换器应用于ComboBox上的ItemsSource绑定,并设置两个绑定。第一个绑定要选择的值。第二个绑定到ComboBox的ItemsSource属性,该属性是可能值的列表。
对于转换器,Convert()方法在ItemsSource忽略情况下查找选定值,然后从ItemsSource返回匹配值。
ConvertBack()方法只是将选定的值放回对象数组的第一个元素中。
vu8f3i0k2#
在视图模型上创建一个新的属性,该属性提供转换为字符串形式的属性值。将ComboBox(或其他WPF小部件)绑定到该属性。
例如:
通过这种方式,您可以精确地控制如何格式化该属性值以显示它。然而,现在您必须向该其他属性添加更改通知,以便当您更改OtherProperty的值时,数据绑定知道更新新属性的显示。
eit6fx6z3#
下面是一个c#版本的@bkstill selected value ignore case转换器。