none
dataform里怎么得到ComboBox选中项的值? RRS feed

  • 问题

  • dataform里怎么得到ComboBox选中项的值?我在“OKButton_Click”和“Unit_SelectionChanged”事件查找ComboBox选中项的值,均出现提示:“未将对象引用设置到对象的实例”,请各位指点谜津。

     

    前台:       
    <toolkit:DataForm HorizontalAlignment="Left" Name="dataForm1" VerticalAlignment="Top" Height="161" Width="378" ContentLoaded="dataForm1_ContentLoaded">
    <toolkit:DataForm.EditTemplate>
    <DataTemplate>
    <StackPanel>
    <toolkit:DataField Label="事业部名称*">
    <ComboBox Margin="3"  Name="cbUnit" SelectionChanged="cbUnit_SelectionChanged" />
    </toolkit:DataField>
    <toolkit:DataField Label="部门名称*">
    <TextBox Margin="3" Text="{Binding DepartmentName, Mode=TwoWay,NotifyOnValidationError=True,  ValidatesOnExceptions=True }" Name="texDepartmentName" />
    </toolkit:DataField>
    </StackPanel>
    </DataTemplate>
    </toolkit:DataForm.EditTemplate>
    </toolkit:DataForm>

     

    后台:
    namespace TopXinZi.Views
    {
            public partial class Department_add : ChildWindow
            {

                   public Department cw_Department;
                    XinZiDomainContext ds = new XinZiDomainContext();
                    public Department NewDepartment { get; set; }

            public Department_add()
            {
                    InitializeComponent();
                    NewDepartment = new Department();
                    dataForm1.CurrentItem = NewDepartment;
                    dataForm1.BeginEdit();

            }

            private void OKButton_Click(object sender, RoutedEventArgs e)
            {
                   ComboBox cb = (ComboBox)(dataForm1.FindNameInContent("cbUnit"));//未将对象引用设置到对象的实例

                    NewDepartment.UnitName = (cb.SelectedItem as ComboBoxItem).Tag.ToString();
                    NewDepartment.UnitID = Convert.ToInt32((cb.SelectedItem as ComboBoxItem).Tag);
                    this.dataForm1.CommitEdit();
                    this.DialogResult = true;
    }

            private void CancelButton_Click(object sender, RoutedEventArgs e)
            {
                    NewDepartment = null;
                    dataForm1.CancelEdit();
                    this.DialogResult = false;
    }

            // ComboBox控件绑定数据源
            private void dataForm1_ContentLoaded(object sender, DataFormContentLoadEventArgs e)
            {
                    ComboBox cb = (ComboBox)(dataForm1.FindNameInContent("cbUnit"));  

                    cb.ItemsSource = ds.BusinessUnits;
                    cb.DisplayMemberPath = "UnitName";
                    cb.Tag = "UnitID";
                    ds.Load(ds.GetBusinessUnitQuery());
    }

            private void cbUnit_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {

                ComboBox cb = (ComboBox)(dataForm1.FindNameInContent("cbUnit"));/未将对象引用设置到对象的实例

                NewDepartment.UnitName = (cb.SelectedItem as ComboBoxItem).Content.ToString();
                NewDepartment.UnitID = Convert.ToInt32((cb.SelectedItem as ComboBoxItem).Tag);
            }
    }
    }

    2010年7月9日 9:18

答案