Fairly new to the world of WPF. I've got data binding to a view model in the past just fine when it's a basic tree of elements under the root .
Like this works fine:
<TextBox x:Name="emailTxt" Grid.Row="6" Grid.Column="2" Text="{Binding Path=Email}">
My Email string object binds and displays in the above textbox appropriately.
However, when using a DataTemplate nothing seems to work. I'm not sure if its a combination of not implementing the DataTemplate correctly or something with the TabControl. I've tried switching around the logic a bit based on other sources (setting DataType on the template, etc), but that's been no good.
Here's the example of kind of what I have currently (sorry the template is long do the needing a large grid of 3x40 cells): this template exists under Windows.Resources.
<DataTemplate x:Key="commGridTemplate" DataType="{x:Type ViewModels:CommViewModel}">
<ScrollViewer Margin="10,30,60,30">
<Grid Name="CommGrid" IsSharedSizeScope="True"> <!-- DataContext="{Binding}" -->
<Grid.ColumnDefinitions>
<ColumnDefinition Name="RowBoxCol" Width="0.2*"></ColumnDefinition>
<ColumnDefinition Name="ChannelCol" Width="1*"></ColumnDefinition>
<ColumnDefinition Name="FrequencyCol" Width="1*"></ColumnDefinition>
<ColumnDefinition Name="DescriptionCol" Width="2*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<!--#region Row Definitions-->
<RowDefinition Height="20" SharedSizeGroup="CommRow"></RowDefinition> <!-- Header Row-->
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<RowDefinition SharedSizeGroup="CommRow"></RowDefinition>
<!--#endregion Row Definitions-->
</Grid.RowDefinitions>
<!-- Header boxes-->
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="0" IsReadOnly="True"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="1" Grid.Row="0" HorizontalContentAlignment="Center" IsReadOnly="True" Text="Channel"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="2" Grid.Row="0" HorizontalContentAlignment="Center" IsReadOnly="True" Text="Frequency"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="3" Grid.Row="0" HorizontalContentAlignment="Center" IsReadOnly="True" Text="Description"></TextBox>
<!--#region Content boxes-->
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="1" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox x:Name="ExampleBox" Text="{Binding Path=ExampleText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Grid.Row="1" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="1" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="1" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="2" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="2" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="2" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="2" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="3" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="3" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="3" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="3" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="4" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="4" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="4" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="4" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="5" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="5" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="5" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="5" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="6" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="6" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="6" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="6" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="7" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="7" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="7" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="7" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="8" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="8" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="8" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="8" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="9" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="9" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="9" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="9" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="10" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="10" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="10" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="10" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="11" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="11" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="11" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="11" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="12" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="12" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="12" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="12" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="13" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="13" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="13" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="13" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="14" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="14" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="14" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="14" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="15" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="15" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="15" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="15" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="16" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="16" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="16" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="16" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="17" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="17" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="17" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="17" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="18" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="18" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="18" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="18" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="19" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="19" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="19" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="19" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="20" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="20" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="20" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="20" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="21" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="21" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="21" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="21" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="22" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="22" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="22" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="22" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="23" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="23" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="23" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="23" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="24" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="24" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="24" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="24" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="25" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="25" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="25" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="25" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="26" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="26" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="26" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="26" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="27" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="27" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="27" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="27" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="28" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="28" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="28" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="28" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="29" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="29" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="29" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="29" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="30" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="30" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="30" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="30" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="31" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="31" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="31" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="31" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="32" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="32" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="32" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="32" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="33" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="33" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="33" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="33" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="34" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="34" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="34" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="34" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="35" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="35" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="35" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="35" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="36" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="36" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="36" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="36" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="37" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="37" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="37" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="37" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="38" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="38" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="38" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="38" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="39" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="39" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="39" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="39" HorizontalContentAlignment="Center"></TextBox>
<TextBox Style="{StaticResource HeaderFiller}" Grid.Column="0" Grid.Row="40" HorizontalContentAlignment="Center" IsReadOnly="True"></TextBox>
<TextBox Grid.Column="1" Grid.Row="40" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="2" Grid.Row="40" HorizontalContentAlignment="Center"></TextBox>
<TextBox Grid.Column="3" Grid.Row="40" HorizontalContentAlignment="Center"></TextBox>
<!--#endregion-->
<ContentPresenter Content="{Binding}"/>
</Grid>
</ScrollViewer>
</DataTemplate>
<Grid Name="MainWindowGrid">
<GroupBox Style="{DynamicResource GroupBoxStyleNoHeader}" BorderThickness="1" Margin="10,25,10,10">
<Grid Name="GroupBoxGrid">
<Grid.RowDefinitions>
<RowDefinition x:Name="tabControlRow" Height="14*"></RowDefinition>
<RowDefinition x:Name="formButtonsRow" SharedSizeGroup="A"></RowDefinition>
</Grid.RowDefinitions>
<TabControl Name="CommTabs" Grid.Row="0" Margin="5,10,5,0" ContentTemplate="{StaticResource commGridTemplate}">
<TabItem Name="UHFTab" Header="UHF" Padding="15,2,15,2"/>
<TabItem Name="VHFTab" Header="VHF" Padding="15,2,15,2"/>
</TabControl>
Right now I'm just trying to get the ExampleBox at Grid 1,1 to bind to the ExampleText object that exists within my CommViewModel. I set a namespace at the top for xmlns:ViewModels="clr-namespace:CommGUI.ViewModels" so hopefully it's accessible. I also set ViewModel as the DataContext of the main window within MainWindow.xaml.cs
ViewModels.CommViewModel viewModel = new ViewModels.CommViewModel(Convert.ToString(id));
this.DataContext = viewModel;
Any input would be much appreciated.
**EDIT: ** In response to a comment: my grid is a 3x40 consisting of all textboxes. 3 columns, 40 rows. The top row is made up of disabled boxes to act as the header for each column. The columns are Channel, Frequency, and Description. I imagine each row will contain data from a bound object from CommViewModel.
CodePudding user response:
I think you are going about it the wrong way. What you probably need is to create a List that has the 3 parts you need... Frequency, description, and any other things you want. Make the List<> public via a getter/setter.
In your form, create a DataGrid control and set its items source to the List<> and you are almost done.
I have created a sample project called Help1, so change the context, or copy paste into your project to see it work. Below is the MainWindow.xaml showing the use of a datagrid.
<Window
x:Class="Help1.MainWindow"
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"
xmlns:local="clr-namespace:Help1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<DataGrid ItemsSource="{Binding AllMyChannels}"
SelectedItem="{Binding JustTheOneChosenInTheGrid}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Channel" Width="70"
CanUserSort="True" Binding="{Binding Channel}" />
<DataGridTextColumn Header="Frequency" Width="100"
CanUserSort="True" Binding="{Binding Frequency}" />
<DataGridTextColumn Header="Description" Width="200"
CanUserSort="True" Binding="{Binding Description}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
Now, the codebehind via MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Windows;
namespace Help1
{
public partial class MainWindow
{
public MainWindow()
{
prepareChannels();
DataContext = this;
InitializeComponent();
}
public List<YourStuff> AllMyChannels { get; set; }
private void prepareChannels()
{
AllMyChannels = new List<YourStuff>();
AllMyChannels.Add(new YourStuff { Channel = "x1", Frequency = 98.7m, Description = "Some radio station" });
AllMyChannels.Add(new YourStuff { Channel = "y2", Frequency = 16.7m, Description = "Another Thing" });
AllMyChannels.Add(new YourStuff { Channel = "z9", Frequency = 2320.3m, Description = "who knows" });
// continue with all other frequency / items you have, or pull from some database somewhere.
}
// a public object reference to hold whatever the current selected item would be wihtin a datagrid
private YourStuff _justTheOneChosenInTheGrid = null;
public YourStuff JustTheOneChosenInTheGrid
{
get { return _justTheOneChosenInTheGrid; }
set { _justTheOneChosenInTheGrid = value;
// anything you might want to do with the record the user just changed to
MessageBox.Show($"Just selected Channel: {_justTheOneChosenInTheGrid.Channel}");
}
}
}
// define your own class listing the things you want to present to the user.
public class YourStuff
{
public string Channel { get; set; } = "";
public Decimal Frequency{ get; set; } = 0;
public string Description { get; set; } = "";
}
}