Silverlight combobox SelectedItem binding issue
I am new to .NET, and is working on my first Silverlight application.
I Have a Silverlight Child window which is used to add new Customers to a gridview, and has the following code:
namespace FrontEnd.Views
{
public partial class NewCustomer : ChildWindow
{
public DataTransfer.Customer MyCustomer { get; set; }
public NewCustomer()
{
InitializeComponent();
MyCustomer = new DataTransfer.Customer();
NewCustomerForm.CurrentItem = MyCustomer;
NewCustomerForm.BeginEdit();
}
private void OKButton_Click(object sender, RoutedEventArgs e)
{
NewCustomerForm.CommitEdit();
this.DialogResult = true;
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
MyCustomer = null;
NewCustomerForm.CancelEdit();
this.DialogResult = false;
}
}
}
<dataform:DataForm x:Name="NewCustomerForm" Header="New Customer" AutoGenerateFields="False" Margin="0,12,0,0" MinWidth="290" HorizontalAlignment="Left">
<dataform:DataForm.EditTemplate>
<DataTemplate>
<StackPanel>
<dataform:DataField Label="First name">
<TextBox Text="{Binding Firstname, Mode=TwoWay}"/>
</dataform:DataField>
<dataform:DataField Label="Last name">
<ComboBox x:Name="cb1" SelectedItem="{Binding Lastname, Mode=TwoWay}">
<ComboBoxItem Content="NameOne"></ComboBoxItem>
<ComboBoxItem Content="NameTwo"></ComboBoxItem>
</ComboBox>
</dataform:DataField>
</StackPanel>
</DataTemplate>
</dataform:DataForm.EditTemplate>
</dataform:DataForm>
The MyCustomer object contains a Firstname and a Lastname property.
Just for testing purposes i want to add the Lastname through a combobox, the probl开发者_如何学Pythonem is however, when i bind the Lastname property to the combobox selected item, the value that gets saved to the Lastname property is the string System.Windows.Controls.ComboBoxItem instead of the SelectedItem text value.
How can i save the text from the selectedItem in the combobox to the Lastname property?
try this
<ComboBox x:Name="cb1" SelectedValue="{Binding Lastname, Mode=TwoWay}" SelectedValuePath="Content">
Use ComboBoxName.items.SelectedBoxItem
that is SelectedBoxItem instead of SelectedItem
精彩评论