Newbie – Binding to Property Not Working?


Good morning, everyone.

As the title states, I’m having trouble with binding to a property. Let me knock a few things out first.

  • I’m building a WPF Desktop Application.
  • I’m using Caliburn.Micro as a MVVM framework.
  • I’m using SQLite for data storage purposes.

So, I’ve created a class for “ToolData” in my Model and am attempting to create an instance of said class in my ViewModel to fill with data pulled from my SQLite database. Then, I will present this data on my View inside some text boxes.

The Problem: When I try to bind the “ID” textbox to ToolData.ToolID, nothing is showing up. I’ve tested by prompting a message box with ToolData.ToolID as the value and the data presents without any problems, however, when the text box is presented nothing shows up. I’ve been sure to add NotifyOfPropertyChanges (Caliburn.Micro’s verson of INotify I believe?). I’m not sure what I’m doing wrong…

Here are some code samples:


<TextBox Grid.Column="1" Grid.Row="1" Width="100" FontSize="24" HorizontalContentAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Left" IsEnabled="False" Text="{Binding SelectedTool.ToolID}" Style="{DynamicResource TextBox}"/> 


 public ShellModel ShellModel = new ShellModel(); private int _viewIndex; public int ViewIndex { get { return _viewIndex; } set { _viewIndex = value; NotifyOfPropertyChange(() => ViewIndex); } } private ToolData _selectedTool; public ToolData SelectedTool { get { return _selectedTool; } set { _selectedTool = value; NotifyOfPropertyChange(() => SelectedTool); } } private string _tcoToolID; public string TCOToolID { get { return _tcoToolID; } set { _tcoToolID = value; NotifyOfPropertyChange(() => TCOToolID); } } public bool CanTCOSubmitID(string tcoToolID) { if (!string.IsNullOrWhiteSpace(tcoToolID)) { return true; } else { return false; } } public void TCOSubmitID(string tcoToolID) { SQLiteCommand DBCommand = new SQLiteCommand("SELECT id, description, manufacturer, model, serial, track_cal FROM inventory WHERE id=@toolid"); SQLiteConnection DBConnection = new SQLiteConnection(Properties.Settings.Default.db_connection_string); SQLiteDataAdapter DBDataAdapter = new SQLiteDataAdapter(DBCommand.CommandText, DBConnection); DBDataAdapter.SelectCommand.Parameters.AddWithValue("@toolid", TCOToolID); DataTable DBDataTable = new DataTable(); DBConnection.Open(); DBDataAdapter.Fill(DBDataTable); DBConnection.Close(); SelectedTool = new ToolData(); SelectedTool.ToolID = DBDataTable.Rows[0]["id"].ToString(); MessageBox.Show(SelectedTool.ToolID); ViewIndex = 1; } 


 public IObservableCollection<ToolData> ToolInventory; public class ToolData { public string ToolID { get; set; } public string ToolDescription { get; set; } } } 

submitted by /u/Jonny15T
[link] [comments]

Leave a Reply