Select to view content in your preferred language

Define a Combobox Selection via the code behind?

4980
9
02-01-2012 11:42 AM
PaulVepraskas
Frequent Contributor
I have a combobox that I am trying to populate when a user selects a point.  I can get Textbox's and Datepicker's to populate but I can't get a combo

The xaml
                        <ComboBox x:Name="Selection"  Width="200">
                            <ComboBoxItem Tag="1" Content="1"/>
                            <ComboBoxItem Tag="2" Content="2"/>
                            <ComboBoxItem Tag="3" Content="3"/>                            
                        </ComboBox>



In the CS is have been able to isolate the value I need, I just can't force the combobox to show the selection

The cs

string ss = Convert.ToString(g.Attributes["status"]);




Any help would be appreciated.  Thanks!
0 Kudos
9 Replies
JenniferNery
Esri Regular Contributor
You can try the following:
XAML:
     <ComboBox x:Name="Selection">
      <ComboBox.ItemTemplate>
       <DataTemplate>
        <TextBlock Text="{Binding}"/>
       </DataTemplate>
      </ComboBox.ItemTemplate>
     </ComboBox>

Code-behind:
Selection.ItemsSource = new int[] { 1, 2, 3 };
Selection.SelectedItem = (int)g.Attributes["status"];
0 Kudos
PaulVepraskas
Frequent Contributor
Thank you that worked, with a modification! 

All I had to add was Slection.UpdateLayout() near the end of that function otherwise the next selection was messed up.

It works thank you!

One additional large Question, is it possible to populate this combobox with the domain of a field in my geodatabase, and how? The editing featuredataform does this by defualt how can I program a box to do this?

Thanks again for your help!
0 Kudos
JenniferNery
Esri Regular Contributor
Here's an example:

  <ComboBox x:Name="choices" VerticalAlignment="Top" HorizontalAlignment="Center">
   <ComboBox.ItemTemplate>
    <DataTemplate>
     <TextBlock Text="{Binding Value}"/>
    </DataTemplate>
   </ComboBox.ItemTemplate>
  </ComboBox>


When the layer has initialized, you have access to its LayerInfo. LayerInfo.Fields will give you domain information. Domain.CodedValues is a dictionary, so the Binding statement has been changed to show Value.
   var l = new FeatureLayer() { Url = "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/HomelandSecurity/operations/FeatureServer/0", Where = "1=1" };
   l.OutFields.Add("*");
   l.Initialized += (s, e) => 
    {
     foreach (var f in l.LayerInfo.Fields)
     {
      if (f.Domain is CodedValueDomain)
      {
       var cvd = f.Domain as CodedValueDomain;
       choices.ItemsSource = cvd.CodedValues;
       break;
      }
     }
    };
   l.Initialize();
0 Kudos
PaulVepraskas
Frequent Contributor
Thanks for the help that code looks like it will work but I have been unable to test it due to a missing referance.  Can you show me what referance I need to have so that "CodedValueDomain" will recognize itself?

Scratch that I found it...

using ESRI.ArcGIS.Client.FeatureService;

Tested it...

That code worked great, I now get the following [1, NEW] to appear in the dropdown.  What do I tweak to just get NEW?
0 Kudos
JenniferNery
Esri Regular Contributor
You can check what type cvs.CodedValues is. So you can change the Binding statement appropriately. This assumes CodedValues to be a Dictionary and TextBlock should show just the Value part. So when you iterate through cvs.CodedValues, do you see [1, NEW] as value?
<TextBlock Text="{Binding Value}"/>
0 Kudos
PaulVepraskas
Frequent Contributor
It was my mistake I had missed the value binding.  Thanks for your help!
0 Kudos
PaulVepraskas
Frequent Contributor
For testing purposes I was using a sample site where their was only one field that had a domain.  When I use the following code for a layer that had multiple domains it defaults to the first field that has a domain?

var l = new FeatureLayer() { Url = "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/HomelandSecurity/operations/FeatureServer/0", Where = "1=1" };
   l.OutFields.Add("*");
   l.Initialized += (s, e) => 
    {
     foreach (var f in l.LayerInfo.Fields)
     {
      if (f.Domain is CodedValueDomain)
      {
       var cvd = f.Domain as CodedValueDomain;
       choices.ItemsSource = cvd.CodedValues;
       break;
      }
     }
    };
   l.Initialize();.


I have tried changing the "l.OutFields.Add("*");" to specify my field but it continues to bring back the first field that has a domain not say the specifeid fifth field? 

Any thoughts?
0 Kudos
PaulVepraskas
Frequent Contributor
Jennifer thanks for all your help. Im almost there I was able to solve the issue of it only using the first domain by adding an IF statement inside the following code highligted in red...

l.Initialized += (s, b) =>
            {
                foreach (var f in l.LayerInfo.Fields)
                {
                    if (f.Domain is CodedValueDomain)
                    {
                        if (f.Name == "SITE_STATUS")
                        {                            
                            var cvd1 = f.Domain as CodedValueDomain;
                            SITE_STATUSCB.ItemsSource = cvd1.CodedValues;
                            break;
                        }                    
                      }
                }
            };
            l.Initialize();


My Latest issue is that since we are no longer using the following to populate the combobox, Im afraind im back to my original question?

Since we are no longer using the following...
        Selection.ItemsSource = new int[] { 1, 2, 3 };


Now following no longer works to make the selection... I have tried a number of things without any results?

        Selection.SelectedItem = (int)g.Attributes["status"];



Any thoughts would be greatly appreciated?
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Now following no longer works to make the selection... I have tried a number of things without any results?

Code:

Selection.SelectedItem = (int)g.Attributes["status"];
Any thoughts would be greatly appreciated?


Try by using the SelectedValue:
- Add SelectedValuePath="Key" in the XAML that creates your combobox.
- Change the selection by code like : Selection.SelectedValue = (int)g.Attributes["status"];
0 Kudos