Sco*_*ttG 3 data-binding silverlight combobox
我在我的数据库中有一点定义0 =不,1 =是.我有一个silverlight组合,其中包含"是"和"否"的值.如何将我的位值绑定到组合?
您没有说明您正在使用哪种数据访问机制,但典型工具会将位字段显示为布尔属性.最简单的方法是使用值转换器.
这是基本想法(可能需要更多防御性编码): -
public class BoolToStringConverter : IValueConverter
{
public String FalseString { get; set; }
public String TrueString { get; set; }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
return FalseString;
else
return (bool)value ? TrueString : FalseString;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value.Equals(TrueString);
}
}
Run Code Online (Sandbox Code Playgroud)
在您的应用程序中,您现在可以将其添加到Resources属性(通常是App.xaml)
<Resources>
<local:BoolToStringConverter x:Key="CvtYesNo" FalseString="No" TrueString="Yes" />
</Resources>
Run Code Online (Sandbox Code Playgroud)
现在你要像这样创建你的组合框: -
<ComboBox SelectedItem="{Binding YourBitField, Converter={StaticResource CvtYesNo}, Mode=TwoWay}">
<sys:String>Yes<sys:String>
<sys:String>No<sys:String>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
718 次 |
| 最近记录: |