0
我有一个组合框有一些值,我想一次有两件事情。XAML ComboBox默认SelectedIndex没有显示
这里是我的组合框,我想显示10作为默认值,并将其绑定到双?距离属性。
<ComboBox Grid.Row="5" Grid.Column="1"
SelectedIndex="1"
SelectedValue="{Binding Distance, Mode=TwoWay, Converter={StaticResource StringToDoubleConverter}}">
<ComboBoxItem>1</ComboBoxItem>
<ComboBoxItem IsSelected="True">10</ComboBoxItem>
<ComboBoxItem>100</ComboBoxItem>
<ComboBoxItem>1000</ComboBoxItem>
</ComboBox>
这里是转换器:
public class StringToDoubleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
ComboBoxItem item = value as ComboBoxItem;
if (item != null)
{
double d;
if (double.TryParse(item.Content.ToString(), out d))
return d;
}
return null;
}
}
的问题是,在这段代码中,被选中的项目10没有在应用程序启动时显示。 如果我将转换器删除线,那么它会显示选定的项目10,但是,我不能将它绑定到双?距离属性。我不想为它编写代码,例如:Convert.ToDouble(combobox1.SelectedValue)...
我该怎么做才能使这两种工作正常工作?