ILScene in WindowsFormsHost - wpf

I am trying to host an ILPanel in a WindowsFormsHost Control in WPF. Here's my code:
XAML:
<Window x:Class="ILNumericsCharacteristicViewer.ILView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:forms="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
Title="ILView"
Width="300"
Height="300"
Loaded="ILView_OnLoaded">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<forms:WindowsFormsHost x:Name="WindowsFormsHost" Margin="5" />
<Button x:Name="ButtonClose"
Grid.Row="1"
HorizontalAlignment="Right"
Click="ButtonClose_OnClick"
Content="Close" />
</Grid>
Code Behind:
public partial class ILView : Window
{
private ILPanel ilPanel;
public ILView()
{
InitializeComponent();
}
private void IlPanelOnLoad(object sender, EventArgs eventArgs)
{
ILArray<float> A = ILMath.tosingle(ILMath.rand(3, 10000));
var scene = new ILScene {
new ILPlotCube(twoDMode: false) {
new ILPoints {
Positions = A,
Color = null,
Colors = A,
Size = 2,
}
}
};
var pcsm = scene.First<ILPlotCube>().ScaleModes;
pcsm.XAxisScale = AxisScale.Logarithmic;
pcsm.YAxisScale = AxisScale.Logarithmic;
pcsm.ZAxisScale = AxisScale.Logarithmic;
ilPanel.Scene = scene;
}
private void ButtonClose_OnClick(object sender, RoutedEventArgs e)
{
Close();
}
private void ILView_OnLoaded(object sender, RoutedEventArgs e)
{
ilPanel = new ILPanel();
ilPanel.Load += IlPanelOnLoad;
WindowsFormsHost.Child = ilPanel;
}
}
The line WindowsFormsHost.Child = ilPanel; throws an Argument Exception: "Parameter is not valid." Stack Trace:
at System.Drawing.Bitmap..ctor(Int32 width, Int32 height,
PixelFormat format) at
ILNumerics.Drawing.ILBackBuffer.set_Rectangle(Rectangle value) at
ILNumerics.Drawing.ILGDIDriver.set_Size(Size value) at
ILNumerics.Drawing.ILOGLControl.OnResize(EventArgs e) at
System.Windows.Forms.Control.OnSizeChanged(EventArgs e) at
System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32
width, Int32 height, Int32 clientWidth, Int32 clientHeight) at
System.Windows.Forms.Control.UpdateBounds() at
System.Windows.Forms.Control.WmWindowPosChanged(Message& m) at
System.Windows.Forms.Control.WndProc(Message& m) at
System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)

If the rendering controls of ILNumerics are not loaded from a regular application, you will have to give a hint, in order to distinguish regular rendering from design time behavior. Frameworks, which load the library dynamically at runtime (VSTO, devenv, LinqPad and obviously MEF) may cause ILNumerics controls to 'think' to be used in a designer. Hence the design time replacement ('circle') you found.
In order to make ILNumerics render the 'runtime way' instead, add the following setting to your app.config:
key="ILNIsHosted" value="true"
In the context of the app.config settings file:
<configuration>
<appSettings>
<add key="ILNIsHosted" value="true"/>
</appSettings>
</configuration>
The use of the app.config enables the application of the setting even in those scenarios, where the framework does not allow user code to be executed before the setup of any control. If your framework provides some initialization hook, you may just as well do the configuration by code:
ILNumerics.Settings.IsHosted = true;
Keep in mind, that this code needs to be executed early in the application setup. At latest before ILPanel is initialized. Otherwise, the use of app.config is recommended.

Related

WndProc not called in hosted process in wpf

Following the instructions in:
How to run an application inside wpf application?
and in the walkthrough in MSDN (https://msdn.microsoft.com/en-us/library/ms752055.aspx)
I have managed to host my console applications in wpf. (Note: there are more than 2 applications to be hosted)
In ControlHost.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;
using System.Windows.Interop;
namespace Try
{
public class ControlHost : HwndHost
{
private static List<Process> _procList = new List<Process>();
IntPtr hwndControl;
int hostHeight, hostWidth;
string filePath;
internal const int
WS_CHILD = 0x40000000,
GWL_STYLE = -16,
WS_CAPTION = 0x00C00000,
WS_THICKFRAME = 0x00040000;
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32")]
private static extern IntPtr SetParent(IntPtr hWnd, IntPtr hWndParent);
[DllImport("user32.dll", EntryPoint = "DestroyWindow", CharSet = CharSet.Unicode)]
internal static extern bool DestroyWindow(IntPtr hwnd);
public ControlHost(double height, double width, string filePathName)
{
hostHeight = (int)height;
hostWidth = (int)width;
filePath = filePathName;
}
protected override HandleRef BuildWindowCore(HandleRef hwndParent)
{
Process _process = new Process();
_process.StartInfo.FileName = filePath;
_process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
_process.Start();
_procList.Add(_process);
// The main window handle may be unavailable for a while, just wait for it
while (_process.MainWindowHandle == IntPtr.Zero)
{
Thread.Yield();
}
hwndControl = _process.MainWindowHandle;
int style = GetWindowLong(hwndControl, GWL_STYLE);
style = style & ~((int)WS_CAPTION) & ~((int)WS_THICKFRAME); // Removes Caption bar and the sizing border
style |= ((int)WS_CHILD); // Must be a child window to be hosted
SetWindowLong(hwndControl, GWL_STYLE, style);
SetParent(hwndControl, hwndParent.Handle);
this.InvalidateVisual();
HandleRef hwnd = new HandleRef(this, hwndControl);
return hwnd;
}
protected override IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
handled = false;
return IntPtr.Zero;
}
protected override void DestroyWindowCore(HandleRef hwnd)
{
DestroyWindow(hwnd.Handle);
if (_procList != null)
{
foreach (Process p in _procList)
{
if (p != null)
{
try
{
while (!p.HasExited)
{
p.Refresh();
p.CloseMainWindow();
p.Kill();
Thread.Sleep(10);
}
}
catch
{
}
}
}
}
}
public void Stop(IntPtr Hwnd)
{
HandleRef hwnd = new HandleRef(this, Hwnd);
DestroyWindow(hwnd.Handle);
if (_procList != null)
{
foreach (Process p in _procList)
{
if (p != null)
{
try
{
while (!p.HasExited)
{
p.Refresh();
p.CloseMainWindow();
p.Kill();
Thread.Sleep(10);
}
}
catch
{
}
}
}
}
}
}
}
and in MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
namespace Try
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private string[] appsList;
private List<ControlHost> ctrlHostList = new List<ControlHost>();
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ControlHost appsControl;
Border appsBorder;
foreach (string app in appsList)
{
appsBorder = new Border();
appsBorder.Height = double.NaN;
appsBorder.Width = double.NaN;
appsBorder.BorderBrush = Brushes.Silver;
appsBorder.BorderThickness = new Thickness(1);
appsControl = new ControlHost(appsBorder.ActualHeight, appsBorder.ActualWidth, app);
ctrlHostList.Add(appsControl);
appsBorder.Child = appsControl;
WP_Apps.Children.Add(appsBorder);
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
OpenFileDialog _openFileDlg = new OpenFileDialog();
_openFileDlg.Multiselect = true;
if (_openFileDlg.ShowDialog() == true)
{
appsList = _openFileDlg.FileNames;
}
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
foreach(ControlHost CH in ctrlHostList)
{
CH.Stop(CH.Handle);
}
}
}
}
and finally in MainWindow.xaml:
<Window x:Class="Try.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1502" Width="1500" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen" WindowState="Maximized" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="76*"/>
<ColumnDefinition Width="671*"/>
</Grid.ColumnDefinitions>
<Button Content="Start" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Height="20" Click="Button_Click"/>
<Button Content="GetApps" HorizontalAlignment="Left" Height="21" Margin="10,54,0,0" VerticalAlignment="Top" Width="74" Click="Button_Click_1"/>
<ScrollViewer Grid.Column="1">
<WrapPanel Name="WP_Apps" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" />
</ScrollViewer>
<Button Content="Stop" HorizontalAlignment="Left" Margin="10,99,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_2"/>
</Grid>
</Window>
First, I will get the apps by clicking the "Get Apps" buttons than click "Start"
The result will look like this:
But only one hosted application accept user inputs. (In this example, its the 1st application, circled in red who have can have user inputs)
The other 2 does not accept any user input. Nothing is triggered when clicking on the other 2 applications.
I know I have not handled a lot of situations. But that did not influence the problem that I am having at the moment, i presumed. This is a simple application (not the real application) that i wrote, which i hope that someone will be able to reproduce the same error that I have.
Is there anything that I'm doing wrong? Or have I missed something?
Any suggestions would be greatly appreciated. Thanks in advance!
I found a workaround for this problem,
that is to use WindowsFormsHost to host the console application as in this
example.
I created a System.Windows.Forms.Panel and set it as a Child of the WindowsFormsHost and once again add it into the children of a Wrap Panel.
Somehow, all the hosted applications are able to receive user inputs.
As to why, using HwndHost and Border to host console applications, do not receive user inputs, I still do not know why.
But I think its because:
I set the hosted application's window style to Child which makes it unable to receive user inputs. (I know I have read it somewhere, but forgot where)
WndProc only receives Form messages, it does not have access to/catch the messages going in consoles.
These are the 2 reasons that I can think of that are causing the hosted applications not being able to receive user inputs.
Correct me if I'm wrong.
Best Regards,
Kai

How can I "stick" a control on top edge of screen?

I have a custom control which shows some statistic data and need always be placed at the top edge of WP7 screen. but, when user inputs something on a textbox, the soft-keyboard popup. And the custom control is moved out of the screen. I want to make sure the custom control always visible, even when soft keyboard is popup. Does anyone know how to do this?
You must to use some "magic". By "magic" I mean RenderTransform.
Solution is simple - you need to move your custom control (down, when keyboard visible; up, when hidden). Check this valuable post - it's must help you.
Regards.
<phone:PhoneApplicationPage
x:Class="Test.Keyboard.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="PortraitOrLandscape"
>
<Grid x:Name="LayoutRoot" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="WINDOWS PHONE" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock Text="developer's ?" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<Grid Grid.Row="1" Margin="12,0,12,0"></Grid>
<TextBox Grid.Row="2" LostFocus="TextBoxLostFocus"/>
</Grid>
</phone:PhoneApplicationPage>
public partial class MainPage : PhoneApplicationPage
{
private const double LandscapeShift = -259d;
private const double LandscapeShiftWithBar = -328d;
private const double Epsilon = 0.00000001d;
private const double PortraitShift = -339d;
private const double PortraitShiftWithBar = -408d;
public static readonly DependencyProperty TranslateYProperty = DependencyProperty.Register("TranslateY", typeof(double), typeof(MainPage), new PropertyMetadata(0d, OnRenderXPropertyChanged));
public MainPage()
{
InitializeComponent();
Loaded += MainPageLoaded;
}
public double TranslateY
{
get { return (double)GetValue(TranslateYProperty); }
set { SetValue(TranslateYProperty, value); }
}
private static void OnRenderXPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((MainPage)d).UpdateTopMargin((double)e.NewValue);
}
private void MainPageLoaded(object sender, RoutedEventArgs e)
{
BindToKeyboardFocus();
}
private void BindToKeyboardFocus()
{
PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;
if (frame != null)
{
var group = frame.RenderTransform as TransformGroup;
if (group != null)
{
var translate = group.Children[0] as TranslateTransform;
var translateYBinding = new Binding("Y");
translateYBinding.Source = translate;
SetBinding(TranslateYProperty, translateYBinding);
}
}
}
private void UpdateTopMargin(double translateY)
{
if (IsClose(translateY, LandscapeShift) || IsClose(translateY, PortraitShift)
||IsClose(translateY, LandscapeShiftWithBar) || IsClose(translateY, PortraitShiftWithBar)
)
{
LayoutRoot.Margin = new Thickness(0, -translateY, 0, 0);
}
}
private bool IsClose(double a, double b)
{
return Math.Abs(a - b) < Epsilon;
}
private void TextBoxLostFocus(object sender, RoutedEventArgs e)
{
LayoutRoot.Margin = new Thickness();
}
}
Good Luck....

How can I place one control in a callout that orginates from another control in XAML?

Here is a setup: I have a textbox with a numberic value. According to the requirements every time anybody changes that value an accompanying comment needs to be provided. So visually there must be another textbox for the comment that should be displayed right next to the first one. Ideally the comment textbox needs to be placed in a callout that originates from the value textbox and displayed on the right from it overlaying anything what's underneath of it just like on this picture:
I know how to do easily it in CSS and HTML.
I have to do the same in Silverlight now.
Unfortunately I am not very strong in it, so what I am specifically asking about is how having 2 textboxes make one of them appear next to another (on the right overlaying whatever controls are underneath it) with as less XAML and code as possible.
Use a ToolTip, and set the Placement such that it appears to the right. in XAML, you can template your ToolTip to look however you want, even if that means mimicking the TextBox appearance.
This is the purpose of the ToolTip, and I feel strongly that you should always use the right tool for the right job. :)
I hope this helps. Let us know if you need code samples.
EDIT: Added the following code samples:
<TextBox ToolTipService.Placement="Right">
<ToolTipService.ToolTip>
<TextBox Text="{Binding CalloutText, Mode=OneWay}" IsReadOnly="True"/>
</ToolTipService.ToolTip>
</TextBox>
Ok, I ended up writing my own behaviour
namespace MyNamespace
{
public class CommentBehavior : Behavior<TextBox>
{
private readonly TimeSpan howLongWeWaitBeforePopupCloses = TimeSpan.FromMilliseconds(200);
private DispatcherTimer popupClosingTimer;
public static DependencyProperty PopupProperty = DependencyProperty.Register("Popup", typeof(Popup), typeof(CommentBehavior), new PropertyMetadata(null));
public Popup Popup
{
get { return (Popup)this.GetValue(PopupProperty); }
set { this.SetValue(PopupProperty, value); }
}
protected override void OnAttached()
{
this.popupClosingTimer = new DispatcherTimer();
this.popupClosingTimer.Stop();
this.popupClosingTimer.Interval = howLongWeWaitBeforePopupCloses;
this.popupClosingTimer.Tick += this.ClosePopup;
this.AssociatedObject.GotFocus += this.GotFocus;
this.AssociatedObject.LostFocus += this.LostFocus;
this.Popup.Child.GotFocus += PopupChild_GotFocus;
this.Popup.Child.LostFocus += PopupChild_LostFocus;
}
private void PopupChild_LostFocus(object sender, RoutedEventArgs e)
{
this.popupClosingTimer.Start();
}
private void PopupChild_GotFocus(object sender, RoutedEventArgs e)
{
this.popupClosingTimer.Stop();
}
protected override void OnDetaching()
{
this.AssociatedObject.GotFocus -= this.GotFocus;
this.AssociatedObject.LostFocus -= this.LostFocus;
this.Popup.GotFocus -= PopupChild_GotFocus;
this.popupClosingTimer.Tick -= this.ClosePopup;
this.popupClosingTimer = null;
}
private void ClosePopup(object sender, EventArgs e)
{
this.Popup.IsOpen = false;
this.popupClosingTimer.Stop();
}
protected void GotFocus(object sender, RoutedEventArgs e)
{
this.popupClosingTimer.Stop();
this.Popup.IsOpen = true;
var at = this.CalculatePopupPosition();
this.Popup.HorizontalOffset = at.X;
this.Popup.VerticalOffset = at.Y;
}
private Point CalculatePopupPosition()
{
var owner = this.AssociatedObject;
var transformation = owner.TransformToVisual(Application.Current.RootVisual);
var at = transformation.Transform(new Point(owner.ActualWidth, 0));
return at;
}
protected void LostFocus(object sender, RoutedEventArgs e)
{
this.popupClosingTimer.Start();
}
}
}
And the following XAML
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Background="Red">
<TextBox Width="200" Text="0.01">
<i:Interaction.Behaviors>
<local:CommentBehavior>
<local:CommentBehavior.Popup>
<Popup>
<TextBox Text="Comment" />
</Popup>
</local:CommentBehavior.Popup>
</local:CommentBehavior>
</i:Interaction.Behaviors>
</TextBox>
</StackPanel>
</Grid>

Where to set window property in XAML

I found some code for an attached property that will let me escape close a window called as a dialog here on SO.
But I can't figure out where to set the property.
This might be complicated by the fact that this is a RibbonWindow and not a normal Window but I don't think so.
The attached property will be ab:WindowService.EscapeClosesWindow="True". Where in the xaml should it go??
Cheers,
Berryl
ERROR
Unable to cast object of type
'Microsoft.Expression.Platform.WPF.InstanceBuilders.WindowInstance' to type
'Microsoft.Windows.Controls.Ribbon.RibbonWindow'.
at AttachedBehaviors.WindowService.OnEscapeClosesWindowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
The gist of the attached property is
private static void OnEscapeClosesWindowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
var target = (Window) d;
if (target != null)
target.PreviewKeyDown += Window_PreviewKeyDown;
}
/// <summary>Handle the PreviewKeyDown event on the window</summary>
private static void Window_PreviewKeyDown(object sender, KeyEventArgs e) {
var target = (Window) sender;
// If this is the escape key, close the window
if (e.Key == Key.Escape)
target.Close();
}
window xaml
<r:RibbonWindow x:Class="SCA.Presentation.Wpf.Views.TimeSheet.WeeklyTimeSheetView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local ="clr-namespace:SCA.Presentation.Wpf.Views.TimeSheet"
xmlns:ab="clr-namespace:SCA.Presentation.Wpf.AttachedBehaviors;assembly=Smack.Core.Presentation.Wpf"
xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
Background="{DynamicResource WaveWindowBackground}"
FontFamily="Arial" FontSize="12"
Width="900" Height="600"
ResizeMode="CanResizeWithGrip"
WindowStartupLocation="CenterScreen"
ab:WindowService.EscapeClosesWindow="True" <=== ERROR
>
<r:RibbonWindow.Resources>
<ResourceDictionary Source="TimesheetResources.xaml" />
</r:RibbonWindow.Resources>
I created a sample application to recreate that error you mentioned. And I failed to reproduce that error. It easily compiled in my app.
Can you check with a sample application and try to find any differences?
Recreation:
public static class WindowService
{
public static readonly DependencyProperty EscapeClosesWindowProperty = DependencyProperty.RegisterAttached(
"EscapeClosesWindow",
typeof(bool),
typeof(WindowService),
new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnEscapeClosesWindowChanged)));
public static bool GetEscapeClosesWindow(DependencyObject d)
{
return (bool)d.GetValue(EscapeClosesWindowProperty);
}
public static void SetEscapeClosesWindow(DependencyObject d, bool value)
{
d.SetValue(EscapeClosesWindowProperty, value);
}
private static void OnEscapeClosesWindowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Window target = (Window)d;
if (target != null)
{
target.PreviewKeyDown += new System.Windows.Input.KeyEventHandler(Window_PreviewKeyDown);
}
}
private static void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
Window target = (Window)sender;
if (e.Key == Key.Escape)
target.Close();
}
}
XAML:
<ribbon:RibbonWindow x:Class="WpfApplication1.RibbonWindow1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
xmlns:local="clr-namespace:WpfApplication1"
local:WindowService.EscapeClosesWindow="True"
Title="RibbonWindow1"
x:Name="Window"
Width="640"
Height="480">
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ribbon:Ribbon x:Name="Ribbon">
<ribbon:RibbonTab x:Name="HomeTab"
Header="Home">
<ribbon:RibbonGroup x:Name="Group1"
Header="Group1"></ribbon:RibbonGroup>
</ribbon:RibbonTab>
</ribbon:Ribbon>
</Grid>
</ribbon:RibbonWindow>

Is there a Visual Studio (or freeware) equivalent for Expression Blend's "Edit Template" feature?

In Expression Blend, you can view and edit the control template of objects in the "Objects and Timeline" panel. I'm wondering if there's an equivalent feature in Visual Studio or if there's something free (or very inexpensive) I can download that will allow me to do this.
Doing this for DataGrid results in the following:
<Style x:Key="DataGridStyle1" TargetType="{x:Type Custom:DataGrid}">
...
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Custom:DataGrid}">
...
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsGrouping" Value="True">
<Setter Property="ScrollViewer.CanContentScroll" Value="False"/>
</Trigger>
</Style.Triggers>
</Style>
(The ... is of course replaced with setters and the contents of the control template.)
This is a very useful starting point if you want to create a custom style and template for a control. It seems like you can do pretty much anything you can do in Blend in Studio, but this one is eluding me. Any ideas?
Edit
I'm also curious if this feature will be in Visual Studio 2010. Anyone know?
The SimpleStyles project
Try that I think it would be the same. It gives you the simplest template for each control, so you can use it as a jump-off point.
Code for project to get templates:
C#
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Markup;
using System.Xml;
namespace ControlTemplateBrowser
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
private System.Windows.Forms.NotifyIcon icon;
private WindowState _storedWindowState;
private bool _ballonShownOnce = false;
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Type controlType = typeof(Control);
List<Type> derivedTypes = new List<Type>();
// Search all the types in the assembly where the Control class is defined
Assembly assembly = Assembly.GetAssembly(typeof(Control));
foreach (Type type in assembly.GetTypes())
{
// Only add a type of the list if it's a Control, a concrete class, and public.
if (type.IsSubclassOf(controlType) && !type.IsAbstract && type.IsPublic)
{
derivedTypes.Add(type);
}
}
// Sort the types. The custom TypeComparer class orders types alphabetically by type name.
derivedTypes.Sort(new TypeComparer());
lstTypes.ItemsSource = derivedTypes;
SetupTrayIcon();
}
private void lstTypes_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
// Get the selected type.
Type type = (Type)lstTypes.SelectedItem;
// Instantiate the type
ConstructorInfo info = type.GetConstructor(System.Type.EmptyTypes);
Control control = (Control)info.Invoke(null);
// Add it to the grid (but keep it hidden)
control.Visibility = Visibility.Collapsed;
grid.Children.Add(control);
// Get the template.
ControlTemplate template = control.Template;
// Get the XAML for the template.
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb, settings);
XamlWriter.Save(template, writer);
// Display the template
txtTemplate.Text = sb.ToString();
grid.Children.Remove(control);
}
catch (Exception err)
{
txtTemplate.Text = "<< Error generating template: " + err.Message + ">>";
}
}
#region System tray icon code
private void SetupTrayIcon()
{
// Add system tray icon
icon = new System.Windows.Forms.NotifyIcon();
icon.Icon = new System.Drawing.Icon("appIcon.ico");
icon.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info;
icon.BalloonTipTitle = "Minimized to tray";
icon.BalloonTipText = "Click icon to maximize.";
icon.Visible = false;
icon.Click += new EventHandler(icon_Click);
}
private void icon_Click(object sender, EventArgs e)
{
Show();
WindowState = _storedWindowState;
}
private void Window_StateChanged(object sender, EventArgs e)
{
if (WindowState == WindowState.Minimized)
{
Hide();
if (icon != null)
{
if (!_ballonShownOnce)
{
icon.ShowBalloonTip(2000);
_ballonShownOnce = true;
}
}
}
else
_storedWindowState = WindowState;
}
private void Window_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (icon != null)
icon.Visible = !IsVisible;
}
private void Window_Closed(object sender, EventArgs e)
{
icon.Visible = false;
icon.Dispose();
icon = null;
}
#endregion
}
public class TypeComparer : IComparer<Type>
{
#region IComparer<Type> Members
public int Compare(Type x, Type y)
{
return String.Compare(x.Name, y.Name);
}
#endregion
}
}
WPF:
<Window x:Class="ControlTemplateBrowser.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF Control Template Browser" Height="600" Width="800" Loaded="Window_Loaded" Icon="/ControlTemplateBrowser;component/appIcon.png" Closed="Window_Closed" StateChanged="Window_StateChanged" IsVisibleChanged="Window_IsVisibleChanged">
<Grid x:Name="grid" Margin="8">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ListBox Grid.Column="0" x:Name="lstTypes" SelectionChanged="lstTypes_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" Margin="5,0" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBox Grid.Column="1" x:Name="txtTemplate" Text="Select a control to see its template." VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" />
</Grid>
</Window>
Copy paste this into your project and do the necessary namespace modifications. I think what you'll need to modify is this part:
// Search all the types in the assembly where the Control class is defined
Assembly assembly = Assembly.GetAssembly(typeof(Control));
To get the controls from a different assembly. Let if you tried it, and how it went.

Resources