This is my first day with WPF. Fun!
I currently have a WPF application that opens/returns a file with OpenFileDialog. I have an existing script that deconstructs and extracts that data already. What I need is to be able to access that data (I guess bind it to) and display it in an element in XAML. I haven’t been able to figure out how to do this.
So this is my setup.
MainWindow.xaml.cs:
namespace GridView
{
public partial class MainWindow
{
private GridSet<byte> _grids;
public MainWindow()
{
InitializeComponent();
}
private void Open_OnClick(object sender, RoutedEventArgs e)
{
var openDialog = new OpenFileDialog();
if (openDialog.ShowDialog().Value)
{
//Populate _grids with data
}
}
}
}
MainWindow.xaml:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" x:Class="GridView.MainWindow"
Title="Grid View" Width="800" Height="600" WindowStartupLocation="CenterScreen">
<DockPanel>
...
"Open_OnClick" here
...
<TextBox/>
</DockPanel>
</Window>
As you can see, the .cs file is simply the interaction logic for the MainWindow. So why on earth can’t I figure out how to configure my TextBox to display data in _grids?
I have fiddled with x:Name, x:Reference, Window.Resources, DataContext, Binding, and so on, but I haven’t found a guide yet that has applied to this. And it’s probably because I still don’t understand every facet of WPF. But what am I doing wrong here?
(I would alternatively be satisfied with a WPF tutorial that doesn’t make my brain ooze out my ears.)
You have declared
_gridsin your C#, but it’s not added to the visual tree.The simplest way to do this is to declare the object in the XAML (though I’m not 100% sure what a
GridSetis – I can’t find any documentation on that). So if it was a simpleGridyou’d have:By naming it you can access it in the code:
To use binding you must set the
DataContextof the window. The simplest way is to have:in you constructor.