基于组合框选择WPF更改数据网格

时间:2018-03-05 21:41:15

标签: wpf combobox

我试图根据组合框中的选择来填充数据网格,我是wpf的新手,所以我不确定我的问题是数据绑定还是什么。这是我的VM:

namespace WasteTracker
{
public class MenuItemViewModel : INotifyPropertyChanged
{

    DataSource data = new DataSource();

    private ObservableCollection<StationList> stationlistcb = new 
        ObservableCollection<StationList>();
    private ObservableCollection<MenuItem> menuitemdg = new 
        ObservableCollection<MenuItem>();

    //methods to populate combobox of stations and datagrid of menu items 
               for that station
    #region Load Stations and Menu Methods
    public void LoadStationList()
    {
        stationlistcb = new ObservableCollection<StationList>
                            (data.Stations());
    }

    public void LoadMenuItems(int stationid)
    {
        menuitemdg = new ObservableCollection<MenuItem>
            (from onemenuitem in data.MenuItems()
             where onemenuitem.StationId.Equals(stationid)
             select onemenuitem);

        OnPropertyChanged("MenuItemData");
    }
    #endregion

    public ObservableCollection<StationList> StationList
    {
        get
        {
            return stationlistcb;
        }
    }

    public ObservableCollection<MenuItem> MenuItemList
    {
        get
        {
            return menuitemdg;
        }
    }

    /// <summary>
    /// Called when [property changed].
    /// </summary>
    /// <param name="PropertyName">Name of the property.</param>
    protected void OnPropertyChanged(string PropertyName)
    {
        VerifyPropertyName(PropertyName);

        if (PropertyChanged != null)
        {
            PropertyChanged(this, new 
                                    PropertyChangedEventArgs(PropertyName));
        }
    }

    #region Propertychange Methods
    /// <summary>
    /// Warns the developer if this object does not have
    /// a public property with the specified name. This 
    /// method does not exist in a Release build.
    /// </summary>
    [Conditional("DEBUG")]
    [DebuggerStepThrough]
    private void VerifyPropertyName(string propertyName)
    {
        // Verify that the property name matches a real,  
        // public, instance property on this object.
        if (TypeDescriptor.GetProperties(this)[propertyName] == null)
        {
            string msg = "Invalid property name: " + propertyName;
            throw new Exception(msg);
        }
    }
    #endregion

    public event PropertyChangedEventHandler PropertyChanged;
   }
}

这是页面的XAML:

    <TextBlock Grid.Row="0" Grid.ColumnSpan="3" FontSize="35" 
      HorizontalAlignment="Center" VerticalAlignment="Top" Text="Microsoft 
      SVMT Waste Tracker" Height="50"  Width="480"  Margin="44,0" />
    <TextBlock Grid.Row="1" Grid.ColumnSpan="3" FontSize="30" 
       HorizontalAlignment="Center" VerticalAlignment="Top" Text="Waste 
       Entry"  Height="50" Width="166"  Margin="10,0,44,0" />

    <TextBlock Grid.Row="2" Grid.Column="0" Text="Station" FontSize="30"  
      HorizontalAlignment="Center" VerticalAlignment="top" />

    <TextBlock Grid.Row="2" Grid.Column="3" Text="Date" FontSize="30" 
      HorizontalAlignment="Center" VerticalAlignment="top" />
    <DatePicker Grid.Row="3" Grid.Column="2" Grid.RowSpan="2" FontSize="30" 
                HorizontalAlignment="Center" VerticalAlignment="Center" 
                Width="290" Height="42" Margin="0,19,10,22" />

    <ComboBox  Name="StationBox" Grid.Row="3" Grid.Column="0" 
     Grid.RowSpan="2" FontSize="30" ItemsSource="{Binding StationList}" 
               DisplayMemberPath="StationName" HorizontalAlignment="Center" 
          VerticalAlignment="Center" Width="300" Height="42" 
          Margin="0,19,0,23" 
               SelectionChanged="Station_SelectionChanged" />

    <DataGrid ItemsSource="{Binding MenuItemList}" 
       DisplayMemberPath="MenuItemText" AutoGenerateColumns="False" 
       Grid.Row="5" Grid.ColumnSpan="3" CanUserAddRows="True" 
      AlternatingRowBackground="AliceBlue" 
      IsSynchronizedWithCurrentItem="True" >

        <DataGrid.Columns>
            <DataGridTextColumn Header="Menu Item" Binding="{Binding 
              MenuItemText}" HeaderStyle="{StaticResource CenterGridHeader}" 
              CellStyle="{StaticResource CenterCellText}" 
                                IsReadOnly="True" CanUserResize="False" 
              FontSize="36" Width="*"/>
            <DataGridTextColumn Header="UoM" Binding="{Binding UoM}" 
             HeaderStyle="{StaticResource CenterGridHeader}" CellStyle="
             {StaticResource CenterCellText}" 
                                IsReadOnly="True" FontSize="36" 
            CanUserResize="False" Width="*" />
            <DataGridTextColumn Header="Left Over" Binding="{Binding 
               UserInput}" HeaderStyle="{StaticResource CenterGridHeader}" 
               CellStyle="{StaticResource CenterCellText}" 
                                FontSize="36" IsReadOnly="False" 
               CanUserResize="False" Width="*"/>
            <DataGridTextColumn Header="UoM" Binding="{Binding UoM}" 
              HeaderStyle="{StaticResource CenterGridHeader}" CellStyle="
              {StaticResource CenterCellText}" 
                                IsReadOnly="True" FontSize="36" 
             CanUserResize="False" Width="*" />
            <DataGridTextColumn Header="Par" Binding="{Binding Par}" 
             HeaderStyle="{StaticResource CenterGridHeader}" CellStyle="
             {StaticResource CenterCellText}" 
                                IsReadOnly="True" FontSize="36" 
             CanUserResize="False" Width="*"/>
            <DataGridTextColumn Header="UoM" Binding="{Binding UoM}" 
              HeaderStyle="{StaticResource CenterGridHeader}" CellStyle="
             {StaticResource CenterCellText}" 
                                IsReadOnly="True" FontSize="36" 
         CanUserResize="False" Width="*" />
        </DataGrid.Columns>

    </DataGrid>


</Grid>

这是xaml.cs:

public MenuItemViewModel _objViewModel = new MenuItemViewModel();

    public EnterWaste()
    {
        InitializeComponent();
        SourceContext();
        InitialOperations();
    }

   private void Station_SelectionChanged(object sender, 
    SelectionChangedEventArgs e)
    {
        _objViewModel.LoadMenuItems((StationBox.SelectedItem as 
     MenuItem).StationId);

    }

目前使用此代码组合框当前填充了正确的信息,但是当我从组合框中进行选择时,数据网格将不会显示正确的信息。作为wpf的新手我已经做了很多谷歌搜索,但是在级联组合框中我能找到最接近的东西,而这个解决方案确实无济于事。任何建议将不胜感激。

0 个答案:

没有答案
相关问题