There is a ComboBox with two parameters. It is required to have one of the values ​​in it by default. The SelectedIndex parameter is set, but the ComboBox is still loaded without a default value.

<ComboBox Name="StatusAssetClass" Background="White" Height="45" HorizontalAlignment="Stretch" Margin="16,10,16,10" SelectedIndex="0" ItemsSource="{x:Bind Status, Mode=OneWay}" Style="{StaticResource ValidatableComboBoxStyle}" SelectedItem="{x:Bind SelectedStatus, Mode=TwoWay}"> <ComboBoxItem>Active</ComboBoxItem> <ComboBoxItem>Inactive</ComboBoxItem> </ComboBox> 

enter image description here

  • Remove SelectedIndex, leave only SelectedItem - Gardes
  • Removed. The result is the same as in the picture - Alexcandra Khotko
  • Do you install SelectedStatus in the code after launch? - Andrey NOP
  • SelectedStatus is the DependencyProperty to get what was selected "Active" or "Inactive" - Alexcandra Khotko
  • First, do not add Items (ComboBoxItem) at the same time and bind to ItemsSource. It's amazing that the application works at all. Secondly, it is not clear why your SelectedItem is used to. If I were you, if you need a combo box with these two items, then I would: 1) Remove the ItemsSource. 2) SelectedIndex left equal to 0. 3) SelectedItem has banned the property in one direction (Mode = OneWayToSource). And if you also need to change the status from the code, you would remove the SelectedItem in general and bind only to the SelectedIndex. - John

2 answers 2

work example

Create such a ViewModel

 public class MainPageViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private readonly IMainPage _mainPage; //ctor public MainPageViewModel(IMainPage mainPage) { _mainPage = mainPage ?? throw new ArgumentNullException(nameof(mainPage)); } /// <summary> /// Содержимое комбобокса /// </summary> public List<string> StatusList { get; set; } = new List<string> { "Active", "Inactive" }; /// <summary> /// Индекс выбранного в комбобоксе /// </summary> private int _SelectedIndexStatus; public int SelectedIndexStatus { get { return _SelectedIndexStatus; } set { _SelectedIndexStatus = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedIndexStatus))); } } /// <summary> /// Кнопка ОК /// </summary> public DelegateCommand ButtonOKCommand => new DelegateCommand(OnButtonOK); private void OnButtonOK() { _mainPage.ShowMessage($"Вы выбрали: {StatusList[SelectedIndexStatus]}"); } } 

Such a codebeehind

 public interface IMainPage { void ShowMessage(string message); } /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> public sealed partial class MainPage : Page, IMainPage { public MainPage() { this.InitializeComponent(); //привязка ViewModel var vm = new MainPageViewModel(this); this.DataContext = vm; } public async void ShowMessage(string message) { var messageDialog = new MessageDialog(message); await messageDialog.ShowAsync(); } } 

Xaml such

 <ComboBox HorizontalAlignment="Left" Margin="100,0,0,0" Grid.Row="1" VerticalAlignment="Center" Width="152" ItemsSource="{Binding StatusList}" SelectedIndex="{Binding SelectedIndexStatus, Mode=TwoWay}"/> <Button Content="OK" Margin="100,0,0,0" Grid.Row="2" VerticalAlignment="Center" Command="{Binding ButtonOKCommand, Mode=OneTime}"/> 
  • my ComboBox is used to be on control, which is on View, which changes the logic a bit. - Alexcandra Khotko
  • @AlexcandraKhotko "... ComboBox is used to be on control ..." - this is how? I did not understand. - Bulson
  • my ComboBox is on InfoTabControl, and this very InfoTabControl is transferred to another view using the <c: InfoTabControl /> binding - Alexcandra Khotko
  • @AlexcandraKhotko under the tags to your question there is a menu item to править . Add your complete XAML with <c:InfoTabControl/> and then you can see how to solve your problem. - Bulson
  • My answer solves my problem. - Alexcandra Khotko
  <ComboBox Name="StatusAssetClass" Background="White" Height="45" HorizontalAlignment="Stretch" Margin="16,10,16,10" SelectedIndex="0" Style="{StaticResource ValidatableComboBoxStyle}"> <ComboBoxItem>Active</ComboBoxItem> <ComboBoxItem>Inactive</ComboBoxItem> </ComboBox> 

I get the data from the ComboBox in this way:

 StatusAssetClass.SelectionBoxItem.ToString() 
  • Well, that is, you still did not understand the bindings to the properties, but decided to work as usual in the WinForms style - a jumble of code in event handlers, i.e. all code in codebeehind. - Bulson