Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Displaying Value in ComboBox

$
0
0

I have a UserControl that displays a ListBox of items whose data is bound to a List in a ViewModel.  When one item is selected, its data is displayed in a  ComboBox within a ContentControl to show a few additional details and to allow the user to edit some of the data.  Both the items in the ListBox and the ContentControl use DataTemplates to display the data.  In the ContentControl, there is a ComboBox whose options are supplied from another class.  The data I am using is a list of allergies for a particular patient.  The ComboBox displays the options for allergy severity with the DisplayMemberPath using the description and the SelectedValuePath using the code (which is not the same as the index).  The patient data only stores the code.  When I select a specific allergy, all of the data is displayed in the ContentControl, except for the severity.  The ComboBox is always showing blank, even though all of the values are in the dropdown.  If I select one of the values from the dropdown, the change is reflected in the ListBox, and also in some other features that are part of a DataTrigger, so I know the value of the selected severity is known within the ContentControl.  I know showing this value in the ComboBox should be pretty simple, but nothing I have tried seems to make any difference.   

I am including a very simplified version of the UserControl and classes involved.  This is how I initially thought this should work.

UserControl:

<Grid><Grid.RowDefinitions><RowDefinition Height="*" /><RowDefinition Height="Auto" /></Grid.RowDefinitions><StackPanel  Name="StackPanel2" Grid.Row="0"  HorizontalAlignment="Left"><ListBox Name="ListBoxCurrent" HorizontalAlignment="Center" VerticalAlignment="Top"
                         HorizontalContentAlignment="Stretch" MinWidth="395" SelectionMode="Single"
                         ItemsSource="{Binding Path=CurrentAllergies}"
                         AllowDrop="True" GotFocus="ListBoxCurrent_GotFocus"><ListBox.ItemTemplate><DataTemplate><Border x:Name="border" BorderBrush="Gray" BorderThickness="2"
                Padding="5" Margin="5"><Grid Name="AllergyGrid"><Grid.RowDefinitions><RowDefinition/><RowDefinition/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="Auto"/><ColumnDefinition Width="Auto"/></Grid.ColumnDefinitions><TextBlock Grid.Row="0" Grid.Column="0" Name="AllergenNameLabel" 
                                           Text="Allergen: " FontWeight="Bold"/><TextBlock Grid.Row="0" Grid.Column="1" Name="AllergenName" 
                                           Text="{Binding Path=Allergy}" FontWeight="Bold" TextWrapping="Wrap"/><TextBlock Grid.Row="1" Grid.Column="0" Name="AllergySeverityLabel" Text="Severity: "/><TextBlock Grid.Row="1" Grid.Column="1" Name="AllergySeverity" 
                                           Text="{Binding Path=AllergySeverity}" TextWrapping="Wrap"/></Grid></Border></DataTemplate></ListBox.ItemTemplate></ListBox></StackPanel><StackPanel Grid.Row="1" MinWidth="450"  Margin="10,5,10,0"><ContentControl Name="ExpandedAllergyDetails" Content="{Binding }"
                                Visibility="Visible"  Width="450"  ><ContentControl.ContentTemplate><DataTemplate><Border Name="border" BorderBrush="Gray" BorderThickness="2" Padding="5" Margin="5"><Grid Name="DetailsViewAllergyGrid" ><Grid.RowDefinitions><RowDefinition/><RowDefinition/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="Auto"/><ColumnDefinition Width="Auto"/></Grid.ColumnDefinitions><TextBlock Grid.Row="0" Grid.Column="0" Name="DetailsViewAllergenNameLabel" 
                                           Text="Allergen: " FontWeight="Bold"/><TextBlock Grid.Row="0" Grid.Column="1" Name="DetailsViewAllergenName" FontWeight="Bold"
                                           Text="{Binding Path=SelectedAllergy.Allergy}" TextWrapping="Wrap"/><TextBlock Grid.Row="1" Grid.Column="0" Name="DetailsViewAllergySeverityLabel" 
                                           Text="Severity: "/><ComboBox Grid.Row="1" Grid.Column="1" 
                                    Name="DetailsViewAllergySeverity" 
                                    ItemsSource="{Binding Path=SeverityCodes}"
                                    DisplayMemberPath="AllergenSeverityDescription"
                                    SelectedValuePath="AllergenSeverityCode"
                                    SelectedItem="{Binding Path=SeverityCode, Mode=TwoWay}" ></ComboBox></Grid></Border></DataTemplate></ContentControl.ContentTemplate></ContentControl></StackPanel></Grid>

ViewModel Properties

public ObservableCollection<PatientAllergyItem> CurrentAllergies { get; set; }
public PatientAllergyItem SelectedAllergy { get; set; }
public List<AllergySeverityCode> SeverityCodes { get; set; }

Data Classes

public class PatientAllergyItem 
{
    public string Allergy { get; set; }
    public string SeverityCode { get; set; }
}


public class AllergySeverityCode
{
    public string AllergenSeverityCode { get; set; }
    public string AllergenSeverityDescription { get; set; }
    public string Sort { get; set; }
}

Is there something obvious that needs to be set up differently?  Thanks in advance!!

 

Christine A. Piffat


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>