0 Replies - 4871 Views - Last Post: 20 April 2012 - 06:25 AM Rate Topic: -----

#1 thewisegod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-April 12

How to bind a ComboBox to a Dictionary in WPF.

Posted 20 April 2012 - 06:25 AM

<ComboBox x:Name="cboInsCarrier" Height="24" Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="3"  Margin="0,0,0,0" ItemsSource="{Binding InsuranceCompanies}" SelectedValuePath="Key" DisplayMemberPath="Value" SelectedValue="{Binding InsuranceCompany, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<Label Grid.Row="1" Grid.Column="0" Margin="0,0,0,0">Contacts Filter:</Label>
<ComboBox x:Name="cboInsCoContactsFilter" Height="24" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="3"  Margin="0,0,23,0" ItemsSource="{Binding InsuranceCompanyContacts}" SelectedValuePath="Key" DisplayMemberPath="Value" SelectedValue="{Binding InsuranceCompanyContact, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<Button x:Name="btnApplyInsCoContactsFilter" Grid.Row="1" Grid.Column="3" Margin="49,6,0,7">
  <Image Source="{StaticResource button1.BackgroundImage}" />
</Button>
<Label Foreground="Red" Grid.Row="2" Height="24" Grid.Column="0" Grid.ColumnSpan="1" >*Customer:</Label>
<Button x:Name="btnCompanyNotes" Height="24" Visibility="Hidden"  Grid.Row="2" Margin="75,0,0,2" />
<ComboBox x:Name="cboInsCust" Grid.Row="2" Grid.Column="1" Height="24" Grid.ColumnSpan="3" Margin="0,0,0,0"  ItemsSource="{Binding InsuranceCustomers}" SelectedValuePath="Key" DisplayMemberPath="Value" SelectedValue="{Binding InsuranceCustomer, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<Label Grid.Row="3" Margin="0,0,0,0" Grid.Column="0">City:</Label>
<TextBox x:Name="txtInsCoCity" Height="24" Grid.Row="3" IsReadOnly="True" Grid.Column="1" Grid.ColumnSpan="3" Margin="0,0,0,0" />
<Label Grid.Row="4" Margin="0,0,0,0" Grid.Column="0">Zip:</Label>
<TextBox x:Name="txtInsCoZipCode" Height="24" Grid.Row="4" IsReadOnly="True" Grid.Column="1" Grid.ColumnSpan="2" Margin="0,0,41,0" />
<Label Foreground="Red" Grid.Row="5" Grid.Column="0" Margin="0,0,0,0">*Staff Adjuster:</Label>
<ComboBox x:Name="cboInsCoAdjuster" Height="24" Grid.Row="5" Grid.Column="1" Grid.ColumnSpan="3" Margin="0,0,0,0"  ItemsSource="{Binding StaffAdjusters}" SelectedValuePath="Key" DisplayMemberPath="Value" SelectedValue="{Binding StaffAdjusterID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

public class test
{
    /// <summary>
    /// Runs when window is loaded
    /// </summary>
    /// <param name="param"></param>
    public void LoadCollections(object param)
    {
        using (var context = new EnterpriseEntities())
        {

            LoadTheInsuranceCarriersDictionary(context);
        }
    }

    /// <summary>
    /// Runs when I tab off CNUM textbox
    /// </summary>
    /// <param name="param"></param>
    public void TabOffCnum(object param)
    {
        using (var context = new EnterpriseEntities())
        {
            var assessment = context.Assessments.Where(a => a.ControlNumber == param).FirstOrDefault();
            if (assessment != null)
            {

                LoadStaffAdjusters(context, assessment.Claim.CustomerID, false);

                StaffAdjusterID = (int)assessment.AdjusterID;
            }
        }
    }


    public Dictionary<int, string> StaffAdjusters
    {
        get { return _staffAdjusters; }
        set
        {
            _staffAdjusters = value;
            onpropertychanged("StaffAdjusters");
        }
    }

    public int StaffAdjusterID
    {
        get { return _staffAdjusterID; }
        set
        {
            _staffAdjusterID = value;
            onpropertychanged("StaffAdjusterID");
        }
    }

    public Dictionary<int, string> InsuranceCompanies
    {
        get { return _insuranceCompanies; }
        set
        {
            _insuranceCompanies = value;
            onpropertychanged("InsuranceCompanies");
        }
    }

    public int InsuranceCompany
    {
        get { return _insuranceCompany; }
        set
        {
            _insuranceCompany = value;
            onpropertychanged("InsuranceCompany");
        }
    }



    private void LoadTheInsuranceCarriersDictionary(EnterpriseEntities context)
    {
        context.CompanyMasters.OrderBy
        (
            cm => cm.Name
        ).ToList().ForEach
        (
            cm =>
            {
                _insuranceCompanies.Add(cm.CompanyMasterID, cm.Name);
            }
        );
    }

    private void LoadStaffAdjusters(EnterpriseEntities context, int? companyID, bool? includeInactive)
    {
        AddUnknownAdjuster(-1, "OE unknown");
        AddUnknownAdjuster(-2, "AR unknown");
        AddUnknownAdjuster(-3, "CS unknown");

        (from a in context.Contacts
         join b in context.Company_SHP__Contact
         on a.ContactID equals b.ContactID
         where b.CompanyID == companyID
         && b.Active == true
         || includeInactive == true
         select new
         {
             ContactID = b.Company_SHP__ContactID,
             DisplayName = a.LastName + ", " + a.FirstName,
             Active = b.Active
         }).OrderBy
         (
             a => a.DisplayName
         ).ToList().ForEach
         (
             sa =>
             {
                 int id = sa.ContactID;
                 System.Diagnostics.Debug.WriteLine(id.GetType().Name);
                 _staffAdjusters.Add(id, sa.DisplayName);
             }
         );
    }

    private void AddUnknownAdjuster(int id, string displayText)
    {
        if (!_staffAdjusters.ContainsKey(id))
            _staffAdjusters.Add(id, string.Format("(({0})), (({0}))", displayText));
    }

}


When using WPF with MVVM and a ViewModel, I can bind a combobox to a dictionary by setting the ItemsSource, the SelectedValuePath, DisplayMemberPath and then the SelectedValue. I am able to achieve this for instance, if I fill the dictionary when the window loads and then perform a SelectedValue change by tabbing off of a TextBox. However, on certain occasions I need my dictionaries to be filtered. So I tried just filling the dictionary when tabbing off of the TextBox and setting the SelectedValue, but it does not work. The list is filtered, but there is no item selected.

Any help would be greatly appreciated. Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1