I have a DLL with the following code
using System.Text;
using Microsoft.Win32;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace ApplicationCheck
{
public class ApCkr
{
#region .NET
public string Netframeworkavailable()
{
bool NETinstall;
RegistryKey k1 = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Client");
if (k1 == null)
{
NETinstall = false;
}
else
{
NETinstall = true;
}
return NETinstall.ToString();
}
#endregion
#region PDF
public string PDFavailable()
{
bool PDFinstall;
RegistryKey k2 = Registry.ClassesRoot.OpenSubKey(".pdf");
if (k2 == null)
{
PDFinstall = false;
}
else
{
PDFinstall = true;
}
return PDFinstall.ToString();
}
#endregion
#region IExplore
public string IEavailable()
{
bool IEversion;
string k3 = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Internet Explorer").GetValue("Version").ToString();
string z = k3.Substring(0, 1);
int a = Int32.Parse(z);
if (a < 8)
{
IEversion = false;
}
else
{
IEversion = true;
}
return IEversion.ToString();
}
#endregion
#region IIS
public string IISavailable()
{
bool IISinstall;
RegistryKey k4 = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\InetStp");
if (k4 == null)
{
IISinstall = false;
}
else
{
IISinstall = true;
}
return IISinstall.ToString();
}
#endregion
}
}
And a WPF window with the followig XAML code
<Window x:Class="WpfApplication1.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ResizeMode="CanResizeWithGrip"
WindowStyle="None" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"
Title="Window2" Height="350" Width="525">
<Grid>
<Label Content="Windows" Height="25" HorizontalAlignment="Left" Margin="12,15,0,0" Name="label1" VerticalAlignment="Top" Width="106" />
<Label Content="Edition " Height="25" HorizontalAlignment="Left" Margin="12,45,0,0" Name="label2" VerticalAlignment="Top" Width="106" />
<Label Content="Service Pack " Height="25" HorizontalAlignment="Left" Margin="12,75,0,0" Name="label3" VerticalAlignment="Top" Width="106" />
<Label Content="Version " Height="25" HorizontalAlignment="Left" Margin="12,105,0,0" Name="label4" VerticalAlignment="Top" Width="106" />
<Label Content="Processor Bits " Height="25" HorizontalAlignment="Left" Margin="12,135,0,0" Name="label5" VerticalAlignment="Top" Width="106" />
<Label Content="OS Bits " Height="25" HorizontalAlignment="Left" Margin="12,165,0,0" Name="label6" VerticalAlignment="Top" Width="106" />
<Label Content="Program Bits " Height="25" HorizontalAlignment="Left" Margin="12,195,0,0" Name="label7" VerticalAlignment="Top" Width="106" />
<TextBlock Height="21" HorizontalAlignment="Left" Margin="114,19,0,0" Name="textBlock1" Text="{Binding Path=var}" VerticalAlignment="Top" Width="249" ContextMenuOpening="textBlock1_ContextMenuOpening" />
</Grid>
</Window>
and the WPF’s c# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window2.xaml
/// </summary>
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
}
private void textBlock1_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
var NET = new ApplicationCheck.ApCkr();
textBlock1.Text = NET.Netframeworkavailable();
this.DataContext = textBlock1;
}
}
}
I researched data binding in MSDN and At stack overflow namely this – DataBinding Between a WPF GUI and a couple of ListBox/CheckBox
and others but i cannot get it right.And although stack overflow helped me utilise this in a console app.Now i have to do this in a WPF window.
Edit:I have to display the returned values from the DLL
In order to present the data from the DLL in the UI using binding, you need to have an object with public getters. In your UI DLL create a class (in mvvm design patern, this class is called ‘View Model’) with the public getters:
Then, in Window2 constructor, set ApCkrVm to be the DataContext:
Finally, add text blocks in the XML file, binding the Text to the properties:
Some other comments: