Posts Tagged ‘Xbap’
Xceed DataGrid WPF Mouse Single Click Event
Have you been trying to find the event that fires when you select a single record from the Xceed DataGrid? It doesn’t exist! You can play around with the MouseDown event a bit, but it doesn’t quite do the trick since this fires when the scrollbar is pressed and has some other weird side effects. Other Events are worse.
Our developers (including myself) were banging our head up against the wall with this problem until we found a pretty reliable way of capturing when the user single clicks on a record on the DataGrid. I cannot answer why most WPF controls don’t have the Mouse Single Click Event, but this functionality should have been better exposed since it has so many useful applications. This one is a gem, so please comment if you found this article useful in your project.
Enjoy!
Private Sub myDataGridControl_PropertyChanged _
(ByVal sender As Object, _
ByVal e As System.ComponentModel.PropertyChangedEventArgs)
Handles myDataGridControl.PropertyChanged
If e.PropertyName = "GlobalSelectedItems" Then
'myDataGridControl.SelectedItem was clicked!
End If
End Sub
Xceed DataGrid WPF Object Data Binding
Many of the examples that are provided for the Xceed DataGrid involve binding directly to a database table source, so I wanted to give an example of how easy it is to bind to an Object. Xceed DataGrid makes excellent use of the WPF binding and thus only needs it’s ItemsSource property to be set to a data source that implements IEnumerable.
Guess what? By default, all Generic Lists in the .Net framework are compatible data sources. To show how easy it is to bind the Xceed datagrid to a Generic List of Objects, I have created a sample project (which you can download) that does essentially 3 things:
1. Declares a class named: MyRecord with 4 public properties (First, Last, Title and Phone).
2. Defines a function that creates a Generic List of MyRecord objects, sets temp data for each object and returns the new Generic List of MyRecord objects.
3. In the New() method of the Window that contains the DataGrid, we simply set the ItemsSource to the Generic List of MyRecord objects.
Enjoy!
Class Window1
Public Sub New()
InitializeComponent()
DataGridControl1.ItemsSource = GetMyRecords()
End Sub
Private Function GetMyRecords() As List(Of MyRecord)
Dim MyRecords As New List(Of MyRecord)
Dim _myRecord As MyRecord
_myRecord = New MyRecord
_myRecord.First = "Carlos"
_myRecord.Last = "Gonzalez"
_myRecord.Title = "Managing Director"
_myRecord.Phone = "213-555-1212"
MyRecords.Add(_myRecord)
_myRecord = New MyRecord
_myRecord.First = "Vivien"
_myRecord.Last = "Dracon"
_myRecord.Title = "Managing Director"
_myRecord.Phone = "213-777-1212"
MyRecords.Add(_myRecord)
_myRecord = New MyRecord
_myRecord.First = "John"
_myRecord.Last = "Doe"
_myRecord.Title = "Developer"
_myRecord.Phone = "213-111-1212"
MyRecords.Add(_myRecord)
_myRecord = New MyRecord
_myRecord.First = "Jane"
_myRecord.Last = "Doe"
_myRecord.Title = "Sales"
_myRecord.Phone = "213-999-1212"
MyRecords.Add(_myRecord)
_myRecord = New MyRecord
_myRecord.First = "Bill"
_myRecord.Last = "Gates"
_myRecord.Title = "Retired"
_myRecord.Phone = "213-222-1212"
MyRecords.Add(_myRecord)
Return MyRecords
End Function
Private Class MyRecord
Private _first As String
Private _last As String
Private _title As String
Private _phone As String
Public Property First()
Get
Return _first
End Get
Set(ByVal value)
_first = value
End Set
End Property
Public Property Last()
Get
Return _last
End Get
Set(ByVal value)
_last = value
End Set
End Property
Public Property Title()
Get
Return _title
End Get
Set(ByVal value)
_title = value
End Set
End Property
Public Property Phone()
Get
Return _phone
End Get
Set(ByVal value)
_phone = value
End Set
End Property
End Class
End Class
Automate Certificate Install for XBAP Applications
An XBAP application is a WPF application that is targeted to work under the security sandbox within a browser. Because of these restrictions, there is very little you can do to interact with the local computer running the code or even access other web services outside of your main server. XBAP gets around this by allowing you to mark your application as full or partial trust. The following post gives a pretty good overview of how to set up a project to do this:
Setup XBAP Project Full Trust
Although this all works, the part of setting up the user’s browser to accept the trust needed by your application is a bit complicated for the average user to follow. For this reason, I wanted to provide a bit of source code that you can use to create an automated program to install the required certificate on the user’s computer.
All that you have to do is place the pfx file you created via Visual Studio on a server that your users will have access to. The user will only need to download a zip file that contains an executable and run it. This executable will automate the work of installing your certificate on their computer which should make your application richer with full trust and make the barrier of entry for a novice user to start using your XBAP application a lot lower.
Enjoy!
using System;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Windows.Forms;
namespace CertInstall
{
class Program
{
static void Main(string[] args)
{
try
{
//we will use the web client to download the pfx file
//from the server for installation on the local machine
WebClient webClient = new WebClient();
// if you need to pass credentials, this is how you do that
//webClient.Credentials =
new System.Net.NetworkCredential("username", "password");
byte[] certBytes =
webClient.DownloadData("http://HostName/TheCertKey.pfx");
if (certBytes.Length > 0)
{
Console.WriteLine("Begin ....");
createCert(StoreName.TrustedPublisher, certBytes);
createCert(StoreName.Root, certBytes);
createCert(StoreName.My, certBytes);
Console.WriteLine("Finished ....");
}
else
{
Console.WriteLine("error.");
Console.ReadLine();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + Environment.NewLine +
ex.StackTrace);
}
}
private static void createCert(StoreName storageName,
byte[] certBytes)
{
// this is the password you used for the pfx file
//when you created it
X509Certificate2 cert =
new X509Certificate2(certBytes, "pfxpassword");
X509Store store =
new X509Store(storageName, StoreLocation.CurrentUser);
store.Open(OpenFlags.MaxAllowed);
store.Add(cert);
store.Close();
}
}
}