Silverlight组合框SelectedItem绑定问题

发布于 2024-11-17 02:30:48 字数 2264 浏览 0 评论 0原文

我是 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盛夏尉蓝 2024-11-24 02:30:48

试试这个

<ComboBox x:Name="cb1" SelectedValue="{Binding Lastname, Mode=TwoWay}" SelectedValuePath="Content">

try this

<ComboBox x:Name="cb1" SelectedValue="{Binding Lastname, Mode=TwoWay}" SelectedValuePath="Content">
樱娆 2024-11-24 02:30:48

使用 ComboBoxName.items.SelectedBoxItem

即 SelectedBoxItem 而不是 SelectedItem

Use ComboBoxName.items.SelectedBoxItem

that is SelectedBoxItem instead of SelectedItem

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文