Silverlight组合框SelectedItem绑定问题
我是 .NET 新手,正在开发我的第一个 Silverlight 应用程序。
我有一个 Silverlight 子窗口,用于将新客户添加到 gridview,并具有以下代码:
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>
MyCustomer 对象包含 Firstname 和 Lastname 属性。
仅出于测试目的,我想通过组合框添加姓氏,但是问题是,当我将姓氏属性绑定到组合框所选项目时,保存到姓氏属性的值是字符串 System.Windows.Controls.ComboBoxItem而不是 SelectedItem 文本值。
如何将组合框中的 selectedItem 中的文本保存到 Lastname 属性?
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 problem 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个
try this
使用 ComboBoxName.items.SelectedBoxItem
即 SelectedBoxItem 而不是 SelectedItem
Use ComboBoxName.items.SelectedBoxItem
that is SelectedBoxItem instead of SelectedItem