Reputation: 321
UPDATE 1
It's not about 'select nTH item on startup'... it's more like select THE ITEM that is sitting there defined as as the initial item and get the combo box updated. I need to set ItemsSource as CompositeCollection where one of the items is defined as specified (doesn't have to be item 0) and set the mentioned item selected on start up. The fact of Binding set to Content of the item plays crucial role in here. The code below just demonstrates an example application.
END OF UPDATE 1
I've encountered a small issue that I hope I could get solution for in here. I've a combo box and I'd like to initialize it with particular item selected on startup. The problem is that when I start application control is empty and get's its value on the first open. I've managed to extract the problematic code to the simplest form possible (exclude as many variables as I could) and it looks as follows
XAML code for the window
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
xmlns:loc ="clr-namespace:WpfApplication1"
x:Class="WpfApplication1.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">
<StackPanel Orientation="Vertical">
<StackPanel.Resources>
<ComboBoxItem x:Key="toSelectInitially" Content="{Binding Path=ActiveItem, Mode=OneWay}"/>
</StackPanel.Resources>
<ComboBox SelectedIndex="0"
Height="30">
<ComboBox.ItemsSource>
<x:Array Type="{x:Type ComboBoxItem}">
<ComboBoxItem Content="{Binding Path=ActiveItem, Mode=OneWay}"/>
<ComboBoxItem Content="AAA"/>
<ComboBoxItem Content="BBB"/>
</x:Array>
</ComboBox.ItemsSource>
</ComboBox>
<ComboBox SelectedItem="{StaticResource ResourceKey=toSelectInitially}"
Height="30" Loaded="ComboBox_Loaded">
<ComboBox.ItemsSource>
<x:Array Type="{x:Type ComboBoxItem}">
<StaticResource ResourceKey="toSelectInitially"/>
<ComboBoxItem Content="AAA"/>
<ComboBoxItem Content="BBB"/>
</x:Array>
</ComboBox.ItemsSource>
</ComboBox>
<ComboBox SelectedValue="{Binding Path=ActiveItem, Mode=OneWay}"
SelectedValuePath="Content"
Height="30">
<ComboBox.ItemsSource>
<x:Array Type="{x:Type ComboBoxItem}">
<ComboBoxItem Content="{Binding Path=ActiveItem, Mode=OneWay}"/>
<ComboBoxItem Content="AAA"/>
<ComboBoxItem Content="BBB"/>
</x:Array>
</ComboBox.ItemsSource>
</ComboBox>
</StackPanel>
</Window>
Code behind :
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
this.DataContext = new VMSimple();
}
}
}
Simple ViewModel:
using System;
using System.ComponentModel;
namespace WpfApplication1
{
class VMSimple : INotifyPropertyChanged
{
public VMSimple()
{
ActiveItem = string.Concat("Active Item : ", Guid.NewGuid().ToString());
}
private string mActiveItem;
public string ActiveItem
{
get { return mActiveItem; }
private set
{
if (Equals(mActiveItem, value)) return;
mActiveItem = value;
OnPropertyChanged("ActiveItem");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
I've tried to make the code copy-paste-work.
Apparently all of the approaches behave the same (selected value, index, item). Problem goes away if I set list as Items instead of ItemsSource it works but this is not an option. Keep in mind that this is simplified presentation of more complex code where i try to use CompositeCollection but I've replaced it with array to check if this is not causing the problem.
Upvotes: 1
Views: 11541
Reputation: 81253
In case you want your first item to be selected as soon as the itemSource get initialized for your combobox, set the IsSynchronizedWithCurrentItem
to true for your combobox like this -
<ComboBox IsSynchronizedWithCurrentItem="True"/>
Upvotes: 2
Reputation: 1333
Here is a complete example of how i set the first combobox item:
XAML
<ComboBox ItemsSource="{Binding Path=ComboItems}" SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}"/>
VIEWMODEL
public class ViewModel : INotifyPropertyChanged
{
private List<string> m_ComboItems= new List<string>();
private string m_SelectedItem;
public ViewModel()
{
m_ComboItems.Add("AA");
m_ComboItems.Add("BB");
m_ComboItems.Add("CC");
this.SelectedItem = m_ComboItems.First<string>();
}
public List<string> ComboItems
{
get { return m_ComboItems;}
}
public string SelectedItem
{
get { return m_SelectedItem; }
set
{
m_SelectedItem = value;
this.OnPropertyChanged("SelectedItem");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
If you want to change the selectedItem to something other than the first item in the list then you will need to set the SelectedItem to the object in the list e.g. SelectedItem=m_ComboItems[1] will put "BB" as the selected item.
Hope this Helps!
Upvotes: 2