wpf
-
I have populated a combo box as follows: <ComboBox Height="23" HorizontalAlignment="Left" Margin="120,6,0,0" Name="cboHealth" SelectedIndex="{Binding Path=HealthLevelID}" VerticalAlignment="Top" Width="256" /> private void PopulateHealthLevels() { List<clsDetails> healthlevels = new List<clsDetails>(); healthlevels = clsAccess.GetHealthLevels(); foreach (clsDetails details in healthlevels) { cboHealth.Items.Add(details.HealthLevelName); } } The datatable returns the HealthLevelID field as well as otherr fields. HealthLevelID has values of 1 and 2 for Normal and unNormal respectively. The combo box does get populated but it does not show the correct item for the selected row. What am I doing wrong please? Thanks
-
I have populated a combo box as follows: <ComboBox Height="23" HorizontalAlignment="Left" Margin="120,6,0,0" Name="cboHealth" SelectedIndex="{Binding Path=HealthLevelID}" VerticalAlignment="Top" Width="256" /> private void PopulateHealthLevels() { List<clsDetails> healthlevels = new List<clsDetails>(); healthlevels = clsAccess.GetHealthLevels(); foreach (clsDetails details in healthlevels) { cboHealth.Items.Add(details.HealthLevelName); } } The datatable returns the HealthLevelID field as well as otherr fields. HealthLevelID has values of 1 and 2 for Normal and unNormal respectively. The combo box does get populated but it does not show the correct item for the selected row. What am I doing wrong please? Thanks
A couple of things here. 1. Your title needs some work. Simply stating wpf is not enough. 2. The code should be enclosed in pre tags. Now, your problem lies in the fact that SelectedIndex is actually zero based, so your values will not match. SelectedIndex should only be used to indicate the relative position of the object in your collection.
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
-
I have populated a combo box as follows: <ComboBox Height="23" HorizontalAlignment="Left" Margin="120,6,0,0" Name="cboHealth" SelectedIndex="{Binding Path=HealthLevelID}" VerticalAlignment="Top" Width="256" /> private void PopulateHealthLevels() { List<clsDetails> healthlevels = new List<clsDetails>(); healthlevels = clsAccess.GetHealthLevels(); foreach (clsDetails details in healthlevels) { cboHealth.Items.Add(details.HealthLevelName); } } The datatable returns the HealthLevelID field as well as otherr fields. HealthLevelID has values of 1 and 2 for Normal and unNormal respectively. The combo box does get populated but it does not show the correct item for the selected row. What am I doing wrong please? Thanks
Seems that ComboBox doesnt know that HealthLevelID was changed, try this:
BindingExpression binding = BindingOperations.GetBindingExpression(cboHealth, ComboBox.SelectedIndexProperty); if (binding != null) { binding.UpdateTarget(); }
Also, make sure that HealthLevelID is public
modified on Thursday, February 3, 2011 6:35 AM
-
A couple of things here. 1. Your title needs some work. Simply stating wpf is not enough. 2. The code should be enclosed in pre tags. Now, your problem lies in the fact that SelectedIndex is actually zero based, so your values will not match. SelectedIndex should only be used to indicate the relative position of the object in your collection.
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
Hi, At present the HealthLevelID has only values 1 or 2 in it's tblHealthlevels table. Do you mean that since I am using selectedindex in xamml, I can change the integer values in the sql server table to be 0 and 1 instead of 1 and 2 and then it will work? If so, what if I add more values in the table such as 1, 2, 3, 4, ... Will my existing xaml work ok? Thanks
-
A couple of things here. 1. Your title needs some work. Simply stating wpf is not enough. 2. The code should be enclosed in pre tags. Now, your problem lies in the fact that SelectedIndex is actually zero based, so your values will not match. SelectedIndex should only be used to indicate the relative position of the object in your collection.
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
-
Rather than relying on the selected index, I prefer to use a selected item.
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
-
Rather than relying on the selected index, I prefer to use a selected item.
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
-
I have populated a combo box as follows: <ComboBox Height="23" HorizontalAlignment="Left" Margin="120,6,0,0" Name="cboHealth" SelectedIndex="{Binding Path=HealthLevelID}" VerticalAlignment="Top" Width="256" /> private void PopulateHealthLevels() { List<clsDetails> healthlevels = new List<clsDetails>(); healthlevels = clsAccess.GetHealthLevels(); foreach (clsDetails details in healthlevels) { cboHealth.Items.Add(details.HealthLevelName); } } The datatable returns the HealthLevelID field as well as otherr fields. HealthLevelID has values of 1 and 2 for Normal and unNormal respectively. The combo box does get populated but it does not show the correct item for the selected row. What am I doing wrong please? Thanks
You're missing Mode=TwoWay from your binding expression (assuming it's the same in WPF as Silverlight)
C# has already designed away most of the tedium of C++.