All the controls missing in WPF. Over 1 million downloads.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
4.1 KiB

using System;
using System.Windows;
using System.Windows.Controls;
using System.IO;
namespace Samples.Infrastructure.Controls
{
public class DemoView : ContentControl
{
#region Members
readonly string _applicationPath = String.Empty;
const string _samplesFolderName = "Samples";
#endregion //Members
#region Properties
public static readonly DependencyProperty CSharpTextProperty = DependencyProperty.Register("CSharpText", typeof(string), typeof(DemoView), new UIPropertyMetadata(null));
public string CSharpText
{
get { return (string)GetValue(CSharpTextProperty); }
set { SetValue(CSharpTextProperty, value); }
}
public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(DemoView), new UIPropertyMetadata(null));
public string Title
{
get { return (string)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}
public static readonly DependencyProperty XamlTextProperty = DependencyProperty.Register("XamlText", typeof(string), typeof(DemoView), new UIPropertyMetadata(null));
public string XamlText
{
get { return (string)GetValue(XamlTextProperty); }
set { SetValue(XamlTextProperty, value); }
}
#endregion //Properties
#region Constructors
static DemoView()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(DemoView), new FrameworkPropertyMetadata(typeof(DemoView)));
}
public DemoView()
{
_applicationPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
}
#endregion //Constructors
#region Base Class Overrides
protected override void OnContentChanged(object oldContent, object newContent)
{
base.OnContentChanged(oldContent, newContent);
//the parent of the content will be the View
ResolveContentCode((newContent as FrameworkElement).Parent);
}
#endregion //Base Class Overrides
#region Methods
private void ResolveContentCode(object newContent)
{
//get the type of the content loaded in the ContentRegion
var type = newContent.GetType();
//grab only the name of the content which will correspond to the name of the file to load
var viewName = type.FullName.Substring(type.FullName.LastIndexOf(".") + 1);
//get the module name
var moduleName = type.Module.Name.Replace(".dll", String.Empty);
SetText(viewName, moduleName);
}
private void SetText(string viewName, string moduleName)
{
SetCSharpText(viewName, moduleName);
SetXamlText(viewName, moduleName);
}
private void SetCSharpText(string viewName, string moduleName)
{
//now we need to append the file extension
string fileName = String.Format("{0}.xaml", viewName);
string filePath = GetFilePath(moduleName, fileName);
XamlText = ReadFileText(filePath);
}
private void SetXamlText(string viewName, string moduleName)
{
//now we need to append the file extension
string fileName = String.Format("{0}.xaml.cs", viewName);
string filePath = GetFilePath(moduleName, fileName);
CSharpText = ReadFileText(filePath);
}
private string GetFilePath(string moduleName, string fileName)
{
return Path.Combine(_applicationPath, _samplesFolderName, moduleName, fileName);
}
private static string ReadFileText(string filePath)
{
string text = String.Empty;
if (File.Exists(filePath))
text = File.ReadAllText(filePath);
return text;
}
#endregion //Methods
}
}