Value Converters in WPF C# We often use data binding to display primitive data (
ID: 3841302 • Letter: V
Question
Value Converters in WPF C#
We often use data binding to display primitive data (ints, strings, even bools) directly to the UI. However, what if we want to bind a value to a dependency property of an incompatible type? Or what if the data value needs to be translated before it is displayed? WPF gives us the value converters in order to meet this need.
Let’s build a color hex value translator.
Requirements
You will build a UI with the following qualities:
There will be eight text boxes. Four of them will represent the decimal values of the current color (each between 0 and 255 inclusive). The remaining four will be the hexadecimal values of the current color (each between 00 and FF).
You must build a custom value converter to convert between the decimal and hexadecimal values of each box. Changing the hex Red box should update the decimal Red box and vice versa.
There will be a checkbox for the option “Display Preview With Sliders”. Clicking the checkbox toggles the visibility of a color control with 4 sliders and a preview panel.
While you will need a value converter for this, there is no need to build a custom one. You will use WPF’s built-in value converter class BooleanToVisibilityConverter.
The preview panel will be bound to its sliders in order to display the currently selected color in real time.
You will create a custom multi-converter and use it in a MultiBinding in order to accomplish this.
The sliders themselves should be clearly labeled, slide only in whole number values, span a range of values from 0 to 255 inclusive, and have a non-editable label displaying the its current value.
Finally, you will bind the sliders of the preview control to the original decimal text boxes using two-way binding.
Explanation / Answer
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace std
{
private String ConnString;
private DataManager dView;
private DataSet dp;
public SimpleData()
{
InitializeComponent();
ConnString = "data source=xeon;uid=sa;password=manager;database=northwind";
SqlConnection cn = new SqlConnection(ConnectionString);
dp = new DataSet("CustOrders");
SqlDataAdapter da1 = new SqlDataAdapter("SELECT * FROM Customers",cn);
da1.TableMappings.Add("Table","Customers");
da1.Fill(dp);
SqlDataAdapter da2 = new SqlDataAdapter("SELECT * FROM Orders",cn);
da2.TableMappings.Add("Table","Orders");
da2.Fill(dp);
SqlDataAdapter da3 = new SqlDataAdapter("SELECT * FROM [Order Details]",cn);
da3.TableMappings.Add("Table","OrderDetails");
da3.Fill(dp);
string myMessage = "Table Mappings: ";
for(int k=0; k < dp.Tables.Count; k++)
{
myMessage += k.ToString() + " "
+ dp.Tables[k].ToString() + " ";
}
System.Data.DataRelation relCustOrd;
System.Data.DataColumn colMaster1;
System.Data.DataColumn colDetail1;
colMaster1 = ds.Tables["Customers"].Columns["CustomerID"];
colDetail1 = ds.Tables["Orders"].Columns["CustomerID"];
relCustOrd = new System.Data.DataRelation("RelCustOrd",colMaster1,colDetail1);
dp.Relations.Add(relCustOrd);
System.Data.DataRelation relOrdDet;
System.Data.DataColumn colMaster2;
System.Data.DataColumn colDetail2;
colMaster2 = ds.Tables["Orders"].Columns["OrderID"];
colDetail2 = ds.Tables["OrderDetails"].Columns["OrderID"];
relOrdDet = new System.Data.DataRelation("RelOrdDet",colMaster2,colDetail2);
dp.Relations.Add(relOrdDet);
myMessage += "Relation Mappings: ";
for(int k=0; k < dp.Relations.Count; k++)
{
myMessage += k.ToString() + " "
+ ds.Relations[k].ToString() + " ";
}
txtMessage.Text = myMessage;
dpView = dp.DefaultViewManager;
grdOrders.DataSource = dpView;
grdOrders.DataMember = "Customers.RelCustOrd";
grdOrderDetails.DataSource = dpView;
grdOrderDetails.DataMember = "Customers.RelCustOrd.RelOrdDet";
cbCust.DataSource = dsView;
cbCust.DisplayMember = "Customers.CompanyName";
cbCust.ValueMember = "Customers.CustomerID";
txtContact.DataBindings.Add("Text",dsView,"Customers.ContactName");
txtPhoneNo.DataBindings.Add("Text",dsView,"Customers.Phone");
txtFaxNo.DataBindings.Add("Text",dsView,"Customers.Fax");
}
private void btnPrev_Click(object sender, System.EventArgs e)
{
if (this.BindingContext[dsView,"Customers"].Position > 0)
{
this.BindingContext[dsView,"Customers"].Position--;
}
}
private void btnNext_Click(object sender, System.EventArgs e)
{
CurrencyManager cm = (CurrencyManager)this.BindingContext[dsView,"Customers"];
if (cm.Position < cm.Count - 1)
{
cm.Position++;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.