Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

how wpf mvvm new record

I have a wpf project that matches the mvvm design pattern. This project has a datagrid object in a window. How can I add a new record to the "observablecollection" object when the data is entered into the textbox object in a child form and the "TAMAM" button is clicked when a new record is requested?

namespace ptkym.Helpers
{
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    public class ObservableObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}



 namespace ptkym.Helpers
{
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    public class Sql_islemleri
    {
        //private SqlConnection _SqlConnection;
        private DataTable _YerelTablo;
        private SqlConnection _SqlConnection;
        public string SqlSorguCumle { get; set; }
        public DataTable YerelTablo()
        {
            _YerelTablo = new DataTable();
            string _connectionString = ConfigurationManager.ConnectionStrings["Sql_Baglanti"].ConnectionString;
            _SqlConnection = new SqlConnection(_connectionString);
            _SqlConnection.Open();
            SqlDataAdapter _SqlDataAdapter = new SqlDataAdapter(SqlSorguCumle, _SqlConnection);
            _SqlDataAdapter.Fill(_YerelTablo);
            _SqlDataAdapter.Dispose();
            _SqlConnection.Close();
            return _YerelTablo;
        }
    }
}



namespace ptkym.Models
{
    using ptkym.Helpers;
    public class M_PKY_Rapor_Sablon_Ek_1_Planlar : ObservableObject
    {
        private string dokuman_No;
        public string Dokuman_No
        {
            get { return dokuman_No; }
            set { dokuman_No = value; OnPropertyChanged(); }
        }
        public M_PKY_Rapor_Sablon_Ek_1_Planlar(string dokuman_No)
        {
            this.Dokuman_No = dokuman_No;
        }
    }
}



namespace ptkym.ViewModels
{
    using ptkym.Helpers;
    using ptkym.Models;
    using System;
    using System.Collections.ObjectModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Windows;
    public class VM_PKY_Rapor_Sablon_Ek_1_Planlar
    {
        Sql_islemleri sql = new Sql_islemleri();
        public RelayCommand DisplayMessageCommand { get; private set; }
        public RelayCommand NewDBRecordCommand { get; private set; }
 
        public M_PKY_Rapor_Sablon_Ek_1_Planlar Aktif_Plan { get; set; }

        private ObservableCollection<M_PKY_Rapor_Sablon_Ek_1_Planlar> sablon_Ek_1_Planlar;
        public ObservableCollection<M_PKY_Rapor_Sablon_Ek_1_Planlar> Sablon_Ek_1_Planlar
        {
            get { return sablon_Ek_1_Planlar; }
            set { sablon_Ek_1_Planlar = value; }
        }

        public VM_PKY_Rapor_Sablon_Ek_1_Planlar()
        {
            sablon_Ek_1_Planlar = new ObservableCollection<M_PKY_Rapor_Sablon_Ek_1_Planlar>();

            VerileriDoldur();
            
            DisplayMessageCommand = new RelayCommand(DisplayMessage);
            NewDBRecordCommand = new RelayCommand(DB_Yeni_Kayit);
        }

        private void VerileriDoldur()
        {
            sql.SqlSorguCumle = "SELECT * FROM pky_rapor_sablon_Ek_1_planlar";
            DataTable dataTable = new DataTable();
            dataTable = sql.YerelTablo();
            foreach (DataRow dr in dataTable.Rows)
            {
                sablon_Ek_1_Planlar.Add(new M_PKY_Rapor_Sablon_Ek_1_Planlar(dr["Dokuman_No"].ToString()));
            }
        }


        private void DB_Yeni_Kayit(object obj)
        {          
    // codes??  
        }
    }
  }


<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"
        xmlns:local="clr-namespace:ptkym.Views"
        xmlns:lokal="clr-namespace:ptkym.ViewModels"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" 
        x:Class="ptkym.Views.PKY_Sablon_Ek_1_Planlar"
        mc:Ignorable="d"
        Title="PKY_Sablon_Ek_1_Planlar" Height="512.534" Width="1196.049">

    <Window.DataContext>
        <lokal:VM_PKY_Rapor_Sablon_Ek_1_Planlar/>
    </Window.DataContext>



    <Grid Margin="0,0,-2,0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="0*"/>
        </Grid.ColumnDefinitions>
        <DataGrid x:Name="grd" CanUserAddRows="True" ItemsSource="{Binding Sablon_Ek_1_Planlar}" 
        AutoGenerateColumns="False" HorizontalAlignment="Left" Height="369" Margin="79,42,0,0" 
          VerticalAlignment="Top" Width="1053">
            <DataGrid.Columns>
                <!--DOKüMAN NO-->
                <DataGridTemplateColumn Header="NO">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Dokuman_No, Mode=TwoWay, 
       UpdateSourceTrigger=PropertyChanged}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>

        </DataGrid>
        <xctk:ChildWindow  WindowState="Open" Width="300" Height="400" DesignerWindowState="Open">
            <Grid>
                <StackPanel Orientation="Vertical">
                    <TextBox ></TextBox>
                </StackPanel>
                <StackPanel Orientation="Horizontal" Margin="0,300,0,0">
                    <Button Content="TAMAM" Command="{Binding NewDBRecordCommand}" Width="150" 
              Margin="0,20,0,0"></Button>
                    <Button Content="?PTAL" Width="150" Margin="0,20,0,0"></Button>
                </StackPanel>
            </Grid>
        </xctk:ChildWindow>
    </Grid>
</Window>
question from:https://stackoverflow.com/questions/65916411/how-wpf-mvvm-new-record

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...