In my UserControl, I have 3 DataGrid. In each DataGrid, I have combobox in Header. I am able to bind the list of DataGrid, but not able to bind the lsit of items for Combobox & its Selected property. Below is the code :
<DataGrid AutoGenerateColumns="False" Name="squeezeDg" Height="170" Width="550" BorderBrush="#FFB7B39D" HorizontalAlignment="Left" VerticalAlignment="Top" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" Background="LightYellow" RowBackground="LightGray" AlternatingRowBackground="#FFFFFFF5" BorderThickness="10" CanUserReorderColumns="False" CanUserSortColumns="False" CanUserDeleteRows="False" CanUserAddRows="False" FontSize="13" ItemsSource="{Binding StageModelList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0,0,-1,0" ><DataGrid.Columns><DataGridTextColumn Header="Stage" Width="75" Binding="{Binding StageName, ValidatesOnExceptions=True}" /><DataGridTextColumn Width="60" Binding="{Binding Bhp, Mode=TwoWay, ValidatesOnExceptions=True}"><DataGridTextColumn.Header><Grid Width="50"><Grid.RowDefinitions><RowDefinition Height="25" /><RowDefinition/></Grid.RowDefinitions><TextBlock Text="BHP" Grid.Row="0"/><ComboBox Grid.Row="1" Width="50" HorizontalAlignment="Center" Name="cboBhp" ItemsSource="{Binding Path=DataContext.InjItems, RelativeSource={RelativeSource AncestorType=UserControl}}" SelectedValue="{Binding Path=DataContext.BhpUnit, RelativeSource={RelativeSource AncestorType=UserControl}, Mode=TwoWay}"></ComboBox></Grid></DataGridTextColumn.Header></DataGridTextColumn>
Model object who is set as the DataContext for the UserControl i.e. object Step3InfoData
private string bhpUnit; public Step3InfoData() { init(); InjItems = new ObservableCollection<string>(); InjItems.Add("pascal"); InjItems.Add("Braye"); BhpUnit = "pascal"; } public string BhtUnit { get { return bhpUnit; } set { if (bhpUnit != value) { bhpUnit = value; Changed("BhtUnit"); } } } public ObservableCollection<StageModel> StageModelList { ............... // BHP is bound to property of StageModel object & it binds perfectly well } public ObservableCollection<string> InjItems { get; set; }
I tried different ways to set the ItemSource of the combobox, but cannot set it. I have several other Combo's in same pattern and is coded in same style. Collection like InjItems is bound to more than 1 combo.
Why am not able to bind InjItems to the Itemsource of Combobox & set its selected to BhpUnit property of the model ? Can anyone please help me. I believe if 1 Combo works properly then my all Combo can be set accordingly and make workable.
Thanks
Thanks
If you find any answer helpful, then click "Vote As Helpful" and if it also solves your question then also click"Mark As Answer".