I have a model in WPF which i am binding to a window.
pageData
holds all my data and includes one field:
pageData.orders.PrefContact
On tests on the page, this is being populated by the model (integer 1->4)
I ahve this XAML:
<Label Content="Last Name" Grid.Column="2" Grid.Row="0" Margin="0,10,0,0" HorizontalAlignment="Right"/>
<TextBox x:Name="txtLastName" Text="{Binding Path = order.LastName}" Grid.Column="3" Grid.Row="0" Margin="10,10,0,0" Width="200" HorizontalAlignment="Left"
TabIndex="1" LostFocus="txtLastName_LostFocus" />
<Label Content="Contact Method" Grid.Column="2" Grid.Row="1" Margin="0,10,0,0" HorizontalAlignment="Right"/>
<ComboBox x:Name="cmbPrefContact" SelectedValue="{Binding Path = order.PrefContact}" SelectionChanged="cmbPrefContact_SelectionChanged" Grid.Column="3"
Width="200" Grid.Row="1" Margin="10,10,0,0" HorizontalAlignment="Left"
TabIndex="3" Height="37" VerticalAlignment="Top" />
In this case the textbox txtLastName is populated correctly, hopefully showing that the model is attached correctly to the XAML.
Within my C# I have a section to create the combobox:
private void bindCmbPref(int cmbpref)
{
var client = new RestClient("http://XXX.XXX.XXX:3000/comboData/contactPrefData");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
var JsonString = response.Content;
var contactPrefData = JsonConvert.DeserializeObject<contactPrefData>(JsonString);
foreach (var contactPrefs in contactPrefData.ContactPrefs)
{
cmbPrefContact.Items.Add(contactPrefs);
cmbPrefContact.SelectedValuePath = "contactPrefID";
cmbPrefContact.DisplayMemberPath = "ContactType";
}
//cmbPrefContact.SelectedValue = cmbpref;
}
I have created it this way as to be honest...I don't understand the whole model/MVVM thing yet and after hours have given up on it. (for the moment). I was hoping to create the combo this way but then set the combo using the binding like my text boxes however it is not working.
If I allow the end code to work above in the c# code I can set the SelectedValue using the variable which is taken from pageData.orders.PrefContact.
What am I doing wrong here that the binding isnt working for the combobox?
EDIT: My model is below for the contactPrefs, which is a result JSON from the API I have made.
public partial class contactPrefData
{
public bool Result { get; set; }
public List<contactPrefs> ContactPrefs { get; set; }
public partial class contactPrefs
{
public int contactPrefID { get; set; }
public string ContactType { get; set; }
}
}
My pageData comes from this model:
public partial class OrderResponse
{
public bool Result { get; set; }
public Order order { get; set; }
public partial class Order
{
public long OrderId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public int PrefContact { get; set; }
public string ItemName { get; set; }
public int ItemQuantity { get; set; }
public string Plu { get; set; }
public string ModelNumber { get; set; }
}
}
C# where the order model is set to the page dataContext:
pageData = JsonConvert.DeserializeObject<OrderResponse>(JsonString);
DataContext = pageData;
HOWEVR if I change in DB and order.PrefContact = 2:
CURRENT WORKING SITUATION: C#:
cmbPrefContact.ItemsSource = contactPrefData.ContactPrefs;
cmbPrefContact.SelectedValuePath = "contactPrefID";
cmbPrefContact.DisplayMemberPath = "ContactType";
XAML:
<ComboBox x:Name="cmbPrefContact"
SelectedValue="{Binding Path = order.PrefContact}"
SelectedValuePath ="contactPrefID"
DisplayMemberPath ="contactType"
SelectionChanged="cmbPrefContact_SelectionChanged" Grid.Column="3"
Width="200" Grid.Row="1" Margin="10,10,0,0" HorizontalAlignment="Left"
TabIndex="3" Height="37" VerticalAlignment="Top" />
So now I guess I'm not sure why I need to designate display member and value paths in the C# AND the XAML... but if i take one of them away on either side it stops working!
This is my contact prefs data being loaded into the combobox:
{
"Result": true,
"contactPrefs": [
{
"contactPrefID": 1,
"ContactType": "SMS"
},
{
"contactPrefID": 2,
"ContactType": "Phone Call"
},
{
"contactPrefID": 3,
"ContactType": "Email"
},
{
"contactPrefID": 4,
"ContactType": "None"
}
]
}
Final working code, worked out by mm8: XAML:
<ComboBox x:Name="cmbPrefContact"
SelectedValue="{Binding Path = order.PrefContact}"
SelectedValuePath ="contactPrefID"
DisplayMemberPath ="ContactType"
SelectionChanged="cmbPrefContact_SelectionChanged" Grid.Column="3"
Width="200" Grid.Row="1" Margin="10,10,0,0" HorizontalAlignment="Left"
TabIndex="3" Height="37" VerticalAlignment="Top" />
C#:
var JsonString = response.Content;
var contactPrefData = JsonConvert.DeserializeObject<contactPrefData>(JsonString);
cmbPrefContact.ItemsSource = contactPrefData.ContactPrefs;
You should bind the SelectedValue
property to the int
source property:
<ComboBox x:Name="cmbPrefContact" SelectedValue="{Binding order.PrefContact}" ... />
SelectedItem
is supposed to be bound to a contactPrefs
property.
You should also replace the foreach
loop by setting the ItemsSource
:
cmbPrefContact.ItemsSource = contactPrefData.ContactPrefs;
...and the SelectedValuePath
and DisplayMemberPath
in XAML:
<ComboBox x:Name="cmbPrefContact" ... SelectedValuePath = "contactPrefID" DisplayMemberPath = "ContactType" />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.