Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Two way databinding with Linq to SQL in a WPF textbox or other control in C#

$
0
0

I'm new to both Linq and WPF and I'm looking for a good resource to understand the concepts better (XAML Databinding and INotifyPropertyChanged specifically when using Linq).  Ultimately I want to use an opensource gauge that automatically registers a new value whenever a row is added to the table.  I'm using VS 2012.  I created a test application but it doesn't work and I can't figure out why.  I get the intial value from my table but when I add a row and generate a new ProcessID nothing updates.  Here is my code:

<Window x:Class="WpfApplication2Test6.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"><Grid><TextBox x:Name="tbTest" BorderBrush="#FFCB5D5D" Width="50" Height="25" Text="{Binding Source=valueP, Path=ProcessID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox></Grid></Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.CompilerServices;
using System.ComponentModel;

namespace WpfApplication2Test6
{

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Window1_Loaded);  
        }

        void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            BindingList<ProcessUpdate> processList = new BindingList<ProcessUpdate>();
            var processID = ProcessUpdate.GetNewProcessValue().ProcessID;
            tbTest.Text = processID.ToString();
        }
    }

    public class ProcessUpdate : INotifyPropertyChanged
    {
        private int pid;

        public event PropertyChangedEventHandler PropertyChanged;

         private ProcessUpdate()
        {
            WpfApplication2Test6.MonthlyBudgetEntities entity = new MonthlyBudgetEntities();

            var valueP = (from p in entity.WPFTestTables
                          select new { p.ProcessID }).ToList().Last();

            pid = valueP.ProcessID;

        }

        public static ProcessUpdate GetNewProcessValue()
        {
            return new ProcessUpdate();
        }

        public int ProcessID
        {
            get { return pid; }
            set
            {
                pid = value;
                OnPropertyChanged("ProcessID");
            }
        }

        public void OnPropertyChanged(string pid)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(pid.ToString()));
            }
        }
    }



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>