{"id":37777,"date":"2024-11-01T10:00:21","date_gmt":"2024-11-01T10:00:21","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=37777"},"modified":"2024-11-01T11:03:46","modified_gmt":"2024-11-01T11:03:46","slug":"wpf-course-connecting-to-database-and-data-binding-in-wpf","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/37777\/","title":{"rendered":"WPF Course, Connecting to Database and Data Binding in WPF"},"content":{"rendered":"<h2>WPF Course: Connecting to a Database and Data Binding in WPF<\/h2>\n<p>\n    WPF (Windows Presentation Foundation) is a UI framework provided by Microsoft that helps to develop rich and diverse user interfaces. WPF offers great advantages in creating efficient and maintainable applications by leveraging features like data binding, styling, templates, and animations. In this course, we will delve into how to connect to a database in WPF and bind data to UI elements.\n<\/p>\n<h3>1. Understanding Data Binding in WPF<\/h3>\n<p>\n    Data binding is one of the key features of WPF that allows easy connection between application data and UI. With this feature, changes in the data model are automatically reflected in the UI, and conversely, data entered in the UI is immediately reflected in the model. This process is implemented utilizing the MVVM (Model-View-ViewModel) pattern.\n<\/p>\n<h4>1.1 Introduction to the MVVM Pattern<\/h4>\n<p>\n    The MVVM pattern is a design pattern that efficiently manages data binding in WPF applications. It consists of three components: Model, View, and ViewModel. The Model defines the data structure of the application, the View defines the UI elements displayed to the user, and the ViewModel connects the View and the Model.\n<\/p>\n<h4>1.2 Types of Data Binding<\/h4>\n<p>\n    The data binding features provided by WPF can be broadly categorized into the following types:\n<\/p>\n<ul>\n<li><strong>One-way Binding:<\/strong> Changes in the data source are reflected in the UI, but changes in the UI do not affect the data source.<\/li>\n<li><strong>Two-way Binding:<\/strong> This allows bidirectional data transfer between the data source and the UI, so changes on both sides are reflected in each other.<\/li>\n<li><strong>One-way to Source Binding:<\/strong> Changes made in the UI are reflected in the data source, but changes in the data source are not reflected in the UI.<\/li>\n<\/ul>\n<h3>2. Connecting to a Database in WPF<\/h3>\n<p>\n    To connect to a database in WPF, data access technologies like ADO.NET can be used. ADO.NET is provided in the .NET framework, enabling connections to databases, data retrieval, and manipulation. To connect to a database from the outset, you must first install the necessary NuGet packages.\n<\/p>\n<h4>2.1 Installing Required Packages<\/h4>\n<p>\n    You can install the necessary database connection libraries via the NuGet Package Manager. For example, to connect to SQL Server, you can install it using the following command:\n<\/p>\n<pre><code>Install-Package System.Data.SqlClient<\/code><\/pre>\n<h4>2.2 Setting Up Database Connection<\/h4>\n<p>\n    To connect to a database, you must set up a connection string. This is usually stored in the <code>App.config<\/code> file. For example, the connection string for SQL Server looks like this:\n<\/p>\n<pre><code>\n&lt;configuration&gt;\n    &lt;connectionStrings&gt;\n        &lt;add name=\"MyDatabase\"\n            connectionString=\"Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;\"\n            providerName=\"System.Data.SqlClient\" \/&gt;\n    &lt;\/connectionStrings&gt;\n&lt;\/configuration&gt;\n<\/code><\/pre>\n<h3>3. Retrieving Data Using ADO.NET<\/h3>\n<p>\n    Once connected to the database, you can use SQL queries to retrieve data. Below is an example of how to read data from a database using ADO.NET.\n<\/p>\n<pre><code>\nusing System.Data;\nusing System.Data.SqlClient;\n\npublic class DatabaseHelper\n{\n    private string connectionString = ConfigurationManager.ConnectionStrings[\"MyDatabase\"].ConnectionString;\n\n    public DataTable GetData()\n    {\n        DataTable dataTable = new DataTable();\n        using (SqlConnection connection = new SqlConnection(connectionString))\n        {\n            connection.Open();\n            SqlCommand command = new SqlCommand(\"SELECT * FROM MyTable\", connection);\n            SqlDataAdapter adapter = new SqlDataAdapter(command);\n            adapter.Fill(dataTable);\n        }\n        return dataTable;\n    }\n}\n<\/code><\/pre>\n<h3>4. Applying Data Binding<\/h3>\n<p>\n    Now we are ready to bind the data retrieved from the database to the WPF UI. Let&#8217;s look at how to display data in UI elements using data binding.\n<\/p>\n<h4>4.1 Setting Up Data Binding in XAML<\/h4>\n<p>\n    To set up data binding in XAML, you use the <code>ItemsSource<\/code> property to pass data. Below is an example of binding data to a ListBox:\n<\/p>\n<pre><code>\n&lt;ListBox Name=\"myListBox\" ItemsSource=\"{Binding}\" \/&gt;\n<\/code><\/pre>\n<h4>4.2 Creating a ViewModel<\/h4>\n<p>\n    To effectively utilize the MVVM pattern, you need to create a ViewModel. The ViewModel wraps the data and should implement the <code>INotifyPropertyChanged<\/code> interface for change notifications.\n<\/p>\n<pre><code>\npublic class MyViewModel : INotifyPropertyChanged\n{\n    private DataTable _data;\n\n    public DataTable Data\n    {\n        get { return _data; }\n        set\n        {\n            _data = value;\n            OnPropertyChanged(\"Data\");\n        }\n    }\n\n    public MyViewModel()\n    {\n        DatabaseHelper dbHelper = new DatabaseHelper();\n        Data = dbHelper.GetData();\n    }\n\n    public event PropertyChangedEventHandler PropertyChanged;\n\n    protected void OnPropertyChanged(string propertyName)\n    {\n        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));\n    }\n}\n<\/code><\/pre>\n<h4>4.3 Connecting the ViewModel to the View<\/h4>\n<p>\n    To connect the ViewModel to the View, set the DataContext property like this:\n<\/p>\n<pre><code>\nthis.DataContext = new MyViewModel();\n<\/code><\/pre>\n<h3>5. Modifying and Saving Data<\/h3>\n<p>\n    Handling data modification or addition in the UI and reflecting these changes in the database is also very important. For instance, you can allow users to modify and save the selected item from the ListBox.\n<\/p>\n<h4>5.1 Binding Modified Data<\/h4>\n<p>\n    The modified data from the UI should be bound to the ViewModel&#8217;s properties, and you need to implement logic to save this data to the database. You can add a TextBox that allows the user to select and edit an item from the list:\n<\/p>\n<pre><code>\n&lt;TextBox Text=\"{Binding SelectedItem.Property, UpdateSourceTrigger=PropertyChanged}\" \/&gt;\n<\/code><\/pre>\n<h4>5.2 Saving to the Database<\/h4>\n<p>\n    After the user modifies the data, you need to create logic to update the corresponding data in the database when they click the &#8220;Save&#8221; button. You can write the additional method in the ViewModel for this:\n<\/p>\n<pre><code>\npublic void UpdateData()\n{\n    \/\/ Logic to save the updated data to the database\n}\n<\/code><\/pre>\n<h3>6. Flexible Interaction Between Data Binding and UI<\/h3>\n<p>\n    The data binding feature in WPF facilitates flexible interaction between the UI and the model. When a user manipulates data through the UI, it is updated through the ViewModel and then reflected back in the UI. This process helps provide users with a better experience.\n<\/p>\n<h3>7. Summary<\/h3>\n<p>\n    In this course, we examined how to connect to a database in WPF and bind data to the UI. We explored how to efficiently manage data binding using the MVVM pattern and how to implement data modification and saving features. These functionalities are very useful when developing data-driven applications, creating an environment where users can smoothly manipulate data.\n<\/p>\n<p>\n    Make active use of WPF&#8217;s powerful data binding features to develop applications that provide rich and dynamic user experiences.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>WPF Course: Connecting to a Database and Data Binding in WPF WPF (Windows Presentation Foundation) is a UI framework provided by Microsoft that helps to develop rich and diverse user interfaces. WPF offers great advantages in creating efficient and maintainable applications by leveraging features like data binding, styling, templates, and animations. In this course, we &hellip; <a href=\"https:\/\/atmokpo.com\/w\/37777\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;WPF Course, Connecting to Database and Data Binding in WPF&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[117],"tags":[],"class_list":["post-37777","post","type-post","status-publish","format-standard","hentry","category-wpf-programming"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>WPF Course, Connecting to Database and Data Binding in WPF - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/37777\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"WPF Course, Connecting to Database and Data Binding in WPF - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"WPF Course: Connecting to a Database and Data Binding in WPF WPF (Windows Presentation Foundation) is a UI framework provided by Microsoft that helps to develop rich and diverse user interfaces. WPF offers great advantages in creating efficient and maintainable applications by leveraging features like data binding, styling, templates, and animations. In this course, we &hellip; \ub354 \ubcf4\uae30 &quot;WPF Course, Connecting to Database and Data Binding in WPF&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/37777\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T10:00:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:03:46+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"5\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/37777\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37777\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"WPF Course, Connecting to Database and Data Binding in WPF\",\"datePublished\":\"2024-11-01T10:00:21+00:00\",\"dateModified\":\"2024-11-01T11:03:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37777\/\"},\"wordCount\":812,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"WPF Programming\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/37777\/\",\"url\":\"https:\/\/atmokpo.com\/w\/37777\/\",\"name\":\"WPF Course, Connecting to Database and Data Binding in WPF - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T10:00:21+00:00\",\"dateModified\":\"2024-11-01T11:03:46+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37777\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/37777\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/37777\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"WPF Course, Connecting to Database and Data Binding in WPF\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"WPF Course, Connecting to Database and Data Binding in WPF - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/37777\/","og_locale":"ko_KR","og_type":"article","og_title":"WPF Course, Connecting to Database and Data Binding in WPF - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"WPF Course: Connecting to a Database and Data Binding in WPF WPF (Windows Presentation Foundation) is a UI framework provided by Microsoft that helps to develop rich and diverse user interfaces. WPF offers great advantages in creating efficient and maintainable applications by leveraging features like data binding, styling, templates, and animations. In this course, we &hellip; \ub354 \ubcf4\uae30 \"WPF Course, Connecting to Database and Data Binding in WPF\"","og_url":"https:\/\/atmokpo.com\/w\/37777\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T10:00:21+00:00","article_modified_time":"2024-11-01T11:03:46+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"5\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/37777\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/37777\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"WPF Course, Connecting to Database and Data Binding in WPF","datePublished":"2024-11-01T10:00:21+00:00","dateModified":"2024-11-01T11:03:46+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/37777\/"},"wordCount":812,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["WPF Programming"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/37777\/","url":"https:\/\/atmokpo.com\/w\/37777\/","name":"WPF Course, Connecting to Database and Data Binding in WPF - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T10:00:21+00:00","dateModified":"2024-11-01T11:03:46+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/37777\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/37777\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/37777\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"WPF Course, Connecting to Database and Data Binding in WPF"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37777","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=37777"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37777\/revisions"}],"predecessor-version":[{"id":37778,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37777\/revisions\/37778"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=37777"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=37777"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=37777"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}