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)...
我該怎麼做才能使這兩種工作正常工作?