This is my Xaml code
<Grid>
<ComboBox Name="C1" ItemsSource="{Binding}" HorizontalAlignment="Left" Margin="126,127,0,0" VerticalAlignment="Top" Width="218" SelectionChanged="C1_SelectionChanged" >
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Margin="2" Text="{Binding CUS_DESCRIPTION}" />
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid x:Name="gd" TextElement.Foreground="Black">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Margin="5" Grid.Column="0" Text="{Binding CUSTOMER_NAME}"/>
<TextBlock Margin="5" Grid.Column="1" Text="{Binding CUS_DESCRIPTION}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="ComboBoxItem.IsSelected" Value="True">
<Setter TargetName="gd" Property="Background" Value="Gray"></Setter>
<Setter TargetName="gd" Property="TextElement.Foreground" Value="White"></Setter>
</Trigger>
<Trigger Property="ComboBoxItem.IsMouseOver" Value="True">
<Setter TargetName="gd" Property="Background" Value="Blue"></Setter>
<Setter TargetName="gd" Property="TextElement.Foreground" Value="White"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
</Grid>
I have created a combo box with data binding in WPF application. Combo box items are add by using Observable Collection object.I am not sure how to set value of "comboboxselecteditem". I am not able to set a value to combo box as a default value.I need like when window opens the combo box "selectedboxitem" should hold the value that I set.The value is already loaded in combo box by using c# code.
c# code:
public partial class MainWindow : Window
{
private ObservableCollection<CUSTOMER> CUSTOMERS=new ObservableCollection<CUSTOMER> ();
public MainWindow()
{
InitializeComponent();
OleDbConnection connect = new OleDbConnection();
connect.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\mani\Documents\RAVI.mdb";
try
{
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * from RICE_CUSTOMER";
cmd.Connection = connect;
connect.Open();
cmd.ExecuteNonQuery();
OleDbDataReader R1 = cmd.ExecuteReader();
if (R1.HasRows)
{
while (R1.Read())
{
CUSTOMERS.Add(new CUSTOMER() { CUSTOMER_NAME = R1[0].ToString(), CUS_DESCRIPTION = R1[3].ToString() });
}
DataContext = CUSTOMERS;
}
connect.Close();
///HOW TO SET COMBOBOX TO THIS VALUE. THIS VALUE ALREADY LOADED IN COMBOBOX BY ABOVE METHOD
string name="DINESH";
string DESC = "HELLO";
C1.SelectedItem = C1.Items.OfType<CUSTOMER>().FirstOrDefault(x => x.CUS_DESCRIPTION==DESC && x.CUSTOMER_NAME==name);
}
catch (OleDbException ex)
{
MessageBox.Show(ex.ToString());
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Window1 W = new Window1();
W.Show();
this.Hide();
}
class CUSTOMER
{
public string CUSTOMER_NAME{get;set;}
public string CUS_DESCRIPTION{get;set;}
}
private void C1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var obj = (CUSTOMER)C1.SelectedItem;
var getvalue = obj.CUSTOMER_NAME;
MessageBox.Show(getvalue);
}
}
}
I tried
"C1.SelectedItem = C1.Items.OfType<CUSTOMER>().FirstOrDefault(x => x.CUS_DESCRIPTION==DESC && x.CUSTOMER_NAME==name);"
but no solution.
IndexOf()
method to get index of your item and useSelectedIndex
instead. – vendettamitC1.SelectedIndex = C1.IndexOf(C1.Items.OfType<CUSTOMER>().FirstOrDefault(x => x.CUS_DESCRIPTION==DESC && x.CUSTOMER_NAME==name));
– vendettamit