I've been breaking my head on best approach to this for few weeks now, and I need some doubts cleared for me, since there are many places I'm unsure as to what's the correct method of binding.
I'm working on a small app for autocad (using its api), and to speed up debuging, I'm simulating it with cmd;
the app creates an instance of my model, and the model instantiates my_window.
in the model, I have ObservableCollection "window_display_data" thats supposed to populate a datagrid in WPF.
I'm aware I can use codebehind, to set datacontext, but I'm also aware that to use the "property panel preview" , its best to set datacontext from "markup code", so thats what I'm trying to do.
I was able to "by accident" populate the table, but in that case, I was setting my observable collection to "static", which I dont see recomended anywhere... so please, someone give me a review of the code, and what would be prefered path to binding my grid.
my "acad simulator"
public static void Main()
{
MyCommands start = new MyCommands();
start.model();
}
public partial class MyCommands
{
public ObservableCollection<string> window_display_data { get; set; }
public void model()
{
.....test values initialization...
MainWindow _window = new MainWindow(this);
var application = new System.Windows.Application();
application.Run(_window);
}
}
public partial class MainWindow : Window
{
public static MyCommands myModel { get; set; }
public MainWindow(MyCommands model)
{
myModel = new MyCommands();
myModel= model;
InitializeComponent();
//this.DataContext = myModel;
}
}
my "window"
<Window
x:Name="MainWindowName"
x:Class="_2017_test_binding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="local"
xmlns:local1="clr-namespace:_2017_test_binding"
xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2"
xmlns:System="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="220" Width="200" WindowStartupLocation="CenterOwner"
ResizeMode="CanResizeWithGrip" MouseDown="Window_MouseDown" Topmost="True"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Width="192"
GridLinesVisibility="Horizontal" AutoGenerateColumns="False"
ItemsSource="{Binding ElementName=MainWindowName, Path=myDictionary}">
<DataGrid.Columns >
<DataGridTextColumn Header="Command" Binding="{Binding Mode=OneWay, Path=window_display_data}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
I guess I should set the dataContext in the window, and let it trinkle down to dataGrid; But without debuging, I never know what ends up down there.
also, could someone clear up for me, why the "dataContext window in property panel in VS" shows links to "objects" instead of instances? that confuses me completely, since what use do I have from that link, if my instance is doing all the work.