I have a listview and a textbox in my xaml file.
View:
<ListView Grid.Column="0" ItemsSource="{Binding I2CDeviceList}" SelectedItem="{Binding SelectedI2CDeviceList, Mode=TwoWay}" Name="I2cDeviceList" >
<ListView.View>
<GridView>
<GridViewColumn Header="I2C Device" Width="Auto" DisplayMemberBinding="{Binding I2CDevName}" />
<GridViewColumn Header="I2C Device Address" Width="Auto" DisplayMemberBinding="{Binding I2CDeviceAddress}" />
</GridView>
</ListView.View>
</ListView>
<TextBox Height="23" Grid.Column="1" Name="AddressI2C" Text="{Binding Path=AddressMessage, Mode=TwoWay}" />
View Model:
//List View Property
public ObservableCollection<I2CModel> I2CDeviceList
{
get { return _I2CDeviceList; }
set
{
_I2CDeviceList = value;
NotifyPropertyChanged("I2CDeviceList");
}
}
private I2CModel _selectedI2CDeviceList;
public I2CModel SelectedI2CDeviceList
{
get { return _selectedI2CDeviceList; }
set
{
_selectedI2CDeviceList = value;
AddressMessage = _selectedI2CDeviceList.I2CDeviceAddress; //Displays Address in My textBox
NotifyPropertyChanged("SelectedI2CDevSize");
}
}
// Property for textBox
private string _AddressMessage;
public string AddressMessage
{
get
{
return _AddressMessage;
}
set
{
_AddressMessage = value;
NotifyPropertyChanged("AddressMessage");
}
}
My requirement is,
When i launch the application, is it possible to have the first item of the listview selected by default? I.e. if the first item in my listview is “Chip Id”, “0x03”, on startup it should be selected by default and the address (0x03) must also get displayed in AddressMessage Textbox.
In your ViewModel Set SelectedI2CDeviceList to default value at the time of launch. I use ViewModel Constructor to set SelectedItem so that when my window is launched my view shows the selected value.