So I think the title is a little confusing, but I couldn't think of anything better. Here's what I have:
CueSheet.cs
class CueSheet : INotifyPropertyChanged
{
private ObservableCollection<Track> _tracks;
public ObservableCollection<Track> Tracks
{
get => _tracks;
set
{
if (_tracks != value)
_tracks = value;
NotifyPropertyChanged();
}
}
private string _path;
public string Path
{
get => _path;
private set
{
if (_path != value)
_path = value;
NotifyPropertyChanged();
}
}
... other code...
}
public class Track : INotifyPropertyChanged
{
... INotifyPropertyChanged event handler ...
private string _title;
public string Title
{
get => _title;
set
{
_title = value;
NotifyPropertyChanged();
}
}
... 2 more properties (Frame and Index) coded as above ...
}
MainWindow.xaml.cs
public partial class MainWindow : INotifyPropertyChanged
{
CueSheet cueSheet;
public MainWindow()
{
InitializeComponent();
cueSheet = new CueSheet();
this.DataContext = this;
// DataContext Bindings
txtCueFilePath.DataContext = cueSheet;
dgCueTracks.DataContext = cueSheet;
}
...Other Code...
}
cueSheet is populated later in the code by reading from a file.
MainWindow.xaml
... other code ...
<TextBox Name="txtCueFilePath"
Grid.Row="1" Grid.Column="1"
IsReadOnly="False"
Background="LightGray"
Text="{Binding Path=Path, Mode=OneWay}"/>
... other code ...
<DataGrid Grid.Column="2" Name="dgCueTracks2"
BorderBrush="Black" BorderThickness="1"
Margin="0 5 5 5"
AutoGenerateColumns="False"
ItemsSource="{Binding Path=Tracks}">
<DataGrid.Columns>
<DataGridTextColumn Header="Title" Binding="{Binding Title}"/>
<DataGridTextColumn Header="Index" Binding="{Binding Index}"/>
<DataGridTextColumn Header="Frame" Binding="{Binding Frame}"/>
</DataGrid.Columns>
</DataGrid>
... other code ...
Ok, so now that you see what I'm working with, I will say that this all works just fine. However I want to have all the binding taken care of in one place, and id like that place to be the XAML file. But I can't work out how to set the DataContext from inside the MainWindow.xaml. I see there is a Datacontext property for DataGrid and TextBox, but I can't figure out how to use them.