[英]WPF ComboBox Binding with Converter
Ok, here's the deal. 好的,这是交易。 I have a CollectionViewSource:
我有一个CollectionViewSource:
<CollectionViewSource x:Key="PA_System_AppStatus">
<CollectionViewSource.Source>
<SystemCols:ArrayList>
<ComboBoxItem Content="Active" />
<ComboBoxItem Content="Denied" />
<ComboBoxItem Content="Granted" />
</SystemCols:ArrayList>
</CollectionViewSource.Source>
</CollectionViewSource>
I also have a ComboBox bound to that: 我还有一个绑定到该框的ComboBox:
<ComboBox x:Name="Perro" Tag="Application"
SelectedValue="{Binding Path=[AppStatus], Converter={StaticResource AppStatusConverter}}"
ItemsSource="{Binding Source={StaticResource PA_System_AppStatus2}}"/>
AppStatus is a char in a DataRow that may be A,D,G. AppStatus是DataRow中的一个字符,可以是A,D,G。 Therefore, I want the ComboBox to display the whole ComboBoxItem above, but under the hood store in the field the char.
因此,我希望ComboBox在上方显示整个ComboBoxItem,但在底层存储char。 For that, I wrote this Converter:
为此,我编写了这个Converter:
public class AppStatusConverter : IValueConverter
{
public object Convert(
object value,
System.Type targetType,
object parameter,
System.Globalization.CultureInfo culture
)
{
string returnValue = null;
if (value != System.DBNull.Value && value != null)
{
if ((string)value == "A")
returnValue = "Active";
else if ((string)value == "D")
returnValue = "Denied";
else if ((string)value == "G")
returnValue = "Granted";
else
returnValue = null;
}
return returnValue;
}
public object ConvertBack(
object value,
System.Type targetType,
object parameter,
System.Globalization.CultureInfo culture
)
{
string returnValue = null;
string tempvalue = ((ComboBoxItem)value).Content.ToString();
if (tempvalue == "Active")
returnValue = "A";
else if (tempvalue == "Denied")
returnValue = "D";
else if (tempvalue == "Granted")
returnValue = "G";
else
returnValue = null;
return returnValue;
}
}
The ConvertBack part works perfectly and, whenever I choose a value, the DataRow gets filled with one of the chars (A,D or G). ConvertBack部分可以完美工作,并且每当我选择一个值时,DataRow就会被填充其中一个字符(A,D或G)。
However, the Convert does not. 但是,转换没有。 For instance, I load a DataRow from a DB.
例如,我从数据库加载DataRow。 The Converter then correctly takes the value inside the 'AppStatus' column and tries to convert it to select one of the ComboBox Items and assign the SelectedValue.
然后,转换器将正确地获取“ AppStatus”列中的值,并尝试将其转换为选择ComboBox项之一并分配SelectedValue。 However, nothing happens.
但是,什么也没有发生。
It seems that the problem is that you are returning a string value from the converter, but the ItemsSource
is an ArrayList
populated with ComboBoxItems, so it checks if the reference is the same. 似乎问题是您正在从转换器返回一个字符串值,但是
ItemsSource
是一个用ComboBoxItems填充的ArrayList
,因此它将检查引用是否相同。 Try to populate your ArrayList
with string values instead of ComboBoxItems. 尝试使用字符串值而不是ComboBoxItems填充
ArrayList
。 You could use a normal string array for this: 您可以为此使用普通的字符串数组:
<x:Array Type="sys:String">
<sys:String>Active</sys:String>
<sys:String>Denied</sys:String>
<sys:String>Granted</sys:String>
</x:Array>
where you add the system namespace: xmlns:sys="clr-namespace:System;assembly=mscorlib"
You could then simplify your converter as follows: 在其中添加系统名称空间的位置:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
然后可以按如下所示简化转换器:
Convert: 兑换:
{
if (!(value is char)) return null;
char c = (char)value;
switch (c)
{
case 'A': return "Active";
case 'D': return "Denied";
case 'G': return "Granted";
}
return null;
}
ConvertBack: ConvertBack:
{
string sVal = value as string;
if (string.IsNullOrEmpty(sVal)) return null;
switch (sVal)
{
case "Active": return 'A';
case "Denied": return 'D';
case "Granted": return 'G';
}
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.