wpf c#静态组合框选择需要更新进行数据库调用的第二个组合框

时间:2013-07-25 16:51:46

标签: c# wpf

我是WPF的新手,使用DevExpress MVVM,我有一个静态组合框,当用户选择一天时,我想通过传递int日并进行数据库调用来刷新该组合框来更新第二个组合框基于当天的路线。这就是我所拥有的:

                            - >                                                                                                                 

    <Label Content="Route:" Grid.Column="2" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="9,60,0,0" Name="lblRoute" VerticalAlignment="Top" />
    <ComboBox Grid.Column="2" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="127,65,0,0" Name="cboRoute" VerticalAlignment="Top" Width="120" 
                DataContext="{Binding}" ItemsSource="{Binding Path=RouteList}"  DisplayMemberPath="{Binding RouteName}" SelectedValue="{Binding RouteID}" Grid.ColumnSpan="2" 
              IsSynchronizedWithCurrentItem="true"/>

视图模型:

 public ObservableCollection<RouteTest> BindRouteComboBox2(int day)
     {
          mgr = new SRMSRailManagerBLL();
        mgr.OpenConnection(ConfigurationManager.ConnectionStrings["SRMSSqlProvider"].ConnectionString);
        //might not want to pass dataset
        _routeDS = mgr.getRoutesForCombo(day);
        _routeDV = _routeDS.Tables[0].DefaultView;
        //cboRoute.ItemsSource = ds.Tables[0].DefaultView;
        //cboRoute.DisplayMemberPath = ds.Tables[0].Columns["RouteName"].ToString();
        //cboRoute.SelectedValuePath = ds.Tables[0].Columns["RouteID"].ToString();
        _routeList = new ObservableCollection<RouteTest>();
        foreach (DataRow row in _routeDV.Table.Rows)
        {
            RouteTest r = new RouteTest(Convert.ToInt32(row.ItemArray[0]), row.ItemArray[1].ToString());
            _routeList.Add(r);

        }


        mgr.CloseConnection();
         return _routeList;

当我点击我的第一个组合框时,我在_SelectionChanged()中填充了'Monday',我想要的是标记值'1',这样我就可以传递给我的BindRouteComboBox(day)程序来更新第二个组合框仅显示第1天的路由,而不是我的数据库表中的每个路由。这应该很简单,我必须在这里找不到简单的东西。

谢谢,

1 个答案:

答案 0 :(得分:0)

您可以使用SelectedValue而不是SelectedValuePath

string temp = (sender as ComboBox).SelectedValue.ToString();