Доброго времени суток, никак не могу понять почему нет привязки, буду благодарен за любую критику и наставления, спасибо.
Model:
public class Data : ViewModelBase
public DateTime BeginDate { get; set; }
public DateTime EndDate { get; set; }
public bool ServerState { get; set; }
public TimeSpan ServerTimeSpan { get; set; }
public ObservableCollection<Terminal> Terminals { get; set; }
public class Terminal : ViewModelBase
public string Protocol { get; set; }
public string SerialId { get; set; }
public string SimNumber { get; set; }
public string ConnectionTime { get; set; }
public ObservableCollection<Sensor> Sensors;
public class Sensor : ViewModelBase
public string Type { get; set; }
public dynamic Value { get; set; }
ViewModel:
public class ViewModel : ViewModelBase
{
private Data Data;
public Data _data
{
get { return Data; }
set
{
if (Data != value)
{
Data = value;
NotifyPropertyChanged("Data");
}
}
}
public DelegateCommand OpenFileCommand { get; set; }
public ViewModel()
{
OpenFileCommand = new DelegateCommand(obj => OpenFileExecute(), obj => true);
}
private async void OpenFileExecute()
{
var openFileDialog = new OpenFileDialog();
var dialogResult = openFileDialog.ShowDialog();
if (!dialogResult.HasValue || !dialogResult.Value)
return;
var path = openFileDialog.FileName;
var xmlParser = new XmlParser();
var data = await xmlParser.ParseFile(path);
_data = data;
}
View:
<Button Content="Button" HorizontalAlignment="Left" Margin="269,347,0,0" VerticalAlignment="Top" Width="75" Command="{Binding OpenFileCommand}"/>
<DataGrid Name="Terminals" AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="10,158,0,0" VerticalAlignment="Top" Height="243" Width="254" SelectedItem="{Binding _data.SelectedTerminal}" ItemsSource="{Binding _data.Terminals}"/>
<DataGrid Name="Sensors" AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="349,222,0,0" VerticalAlignment="Top" Height="158" Width="169" RenderTransformOrigin="0.311,0.278" ItemsSource="{Binding _data.SelectedTerminal.Sensors}"/>
Тут привязываю контекст:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new ViewModel.ViewModel();
}
}
Кстати, Output не ругается ни на что.
Data
SelectedTerminal
?INotifyPropertyChanged
, то изменения свойства будут подхватываться, иначе — нет.