无法将用户输入数据绑定到数据网格

时间:2021-03-26 23:16:33

标签: c# wpf mvvm data-binding

我在将数据从用户输入绑定到数据网格时遇到问题。一旦我进入 ShowEntryWindow 函数,它就不会给我任何绑定。 Observable 继承自 INotfiyPropertyChanged。到目前为止,我只得到 ID 为 0、名称为 null 和位置枚举。我不明白为什么它没有得到用户输入。

模型 - SampleOrder

    public class SampleOrder : Observable
    {
       private long _orderID;
       private string _orderName;
       private Locations _orderLocation;
       public long OrderID
       {
          get { return _orderID; }
          set { _orderID = value; OnPropertyChanged(null); }
       }
       public string OrderName
       {
          get { return _orderName; }
          set { _orderName = value; OnPropertyChanged(null); }
       }
       public Locations OrderLocation
       {
          get { return _orderLocation; }
          set { _orderLocation = value; OnPropertyChanged(null); }
       }
     }

SourcePage.xaml

    <Grid Grid.Row="1">
        <DataGrid
            AutoGenerateColumns="False"
            GridLinesVisibility="Horizontal"
            CanUserAddRows="True"
            Margin="10,10,10,10"
            KeyboardNavigation.TabNavigation="Once">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding OrderID}" Header="ID" />
                <DataGridTextColumn Binding="{Binding OrderName}" Header="Name" />
                <DataGridTextColumn Binding="{Binding OrderLocation}" Header="Location" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
    

SourceViewModel

    public class SourceViewModel : Observable
    {
        public ICommand _showAddEntryCommand;
        public ICommand ShowAddEntryCommand => _showAddEntryCommand ?? (_showAddEntryCommand = new RelayCommand(ShowEntryWindow));

        public ObservableCollection<SampleOrder> Source { get; } = new ObservableCollection<SampleOrder>();

        public SourceViewModel(){}

        public async void ShowEntryWindow()
        {
            var activeWindow = Application.Current.Windows.OfType<MetroWindow>().FirstOrDefault(x => x.IsActive);
            if (!(activeWindow is null))
            {
                var dialog = new AddInformation();
                var result = await activeWindow.ShowChildWindowAsync<CloseReason>(dialog);
                if(result == CloseReason.Apply)
                {
                    Source.Add(new SampleOrder
                    {
                        OrderID = dialog.SourceID,
                        OrderName = dialog.SourceName,
                        OrderLocation = dialog.SourceLocation
                    });
                }
            }
        }
    }

AddInformation.xaml(获取用户输入的对话框)

     <StackPanel Grid.Row="0">
        <TextBlock Text="Order ID:"/>
        <TextBox Text="{Binding SourceID}"
                 Margin="0 5 0 8"/>
        <TextBlock Text="Order Name:" 
                   Padding="0 5 0 0"/>
        <TextBox Text="{Binding SourceName}" 
                 TextWrapping="Wrap" 
                 Height="60"
                 Margin="0 5 0 8" />
        <TextBlock Text="Location" Padding="0 5 0 0"/>
        <ComboBox ItemsSource="{Binding Source={ext:EnumBindingSource {x:Type m:Locations}}}" 
                  Margin="0 5 0 8"/>
    </StackPanel>

AddInformation.xaml.cs

     public partial class AddInformation : ChildWindow
     {
        public SampleOrder Source { get; set; }

        public string SourceName
        {
            get { return Source.OrderName; }
            set { Source.OrderName = value; }
        }
        public long SourceID
        {
            get { return Source.OrderID; }
            set { Source.OrderID = value; }
        }
        public Locations SourceLocation
        {
            get { return Source.OrderLocation; }
            set { Source.OrderLocation= value; }
        }
    
        public AddInformation()
        {
          Source = new SampleOrder();
          InitializeComponent();
        }
      }

0 个答案:

没有答案
相关问题