Why doesn't my ControlTemplate get applied in the ItemContainerStyle?
Took me ages until I found this obscure reference:
"The container for ItemsControl is normally a ContentPresenter, but if the child is a UIElement then it won't use a container. In this case, all of the children are Controls, so the ItemContainerStyle will apply to them directly. If you added an item other than a UIElement, that setter would set the Control.Template property on the ContentPresenter, which would succeed but have no effect."
http://stackoverflow.com/questions/3542381/specify-controltemplate-for-itemscontrol-itemcontainerstyle/3542399#3542399
How to call Dispatcher.Invoke
Making Xceed excel export use foreign key lookups
There is a workaround by rolling your own excel export class that extends Xceed's ExcelExporter base class though.
Here is it: <download>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xceed.Wpf.DataGrid;
using Xceed.Wpf.DataGrid.Export;
namespace sfc.Controls.Custom
{
public class CustomExcelExporter : ExcelExporter
{
private DataGridControl _dgc;
public CustomExcelExporter(DataGridControl dgc)
: base(dgc)
{
_dgc = dgc;
}
protected override void StartDataItemField(ExportContext exportContext, object fieldValue)
{
// Attempt to find the export column in the master section of the DataGrid
var column = (Column)_dgc.Columns[exportContext.Column.FieldName];
// If the column cannot be found in the master section, search the columns of the detail configurations
if (column == null)
{
foreach (DetailConfiguration detailConfiguration in _dgc.DetailConfigurations)
{
if ((column = (Column)detailConfiguration.Columns[exportContext.Column.FieldName]) != null)
{
break;
}
}
}
if (column != null)
{
ForeignKeyConfiguration fkc = column.ForeignKeyConfiguration;
// Check if the column being exported has a ForeignKeyConfiguration and export the mapped value if so
if (fkc != null && fkc.ForeignKeyConverter != null)
{
fieldValue = fkc.ForeignKeyConverter.GetValueFromKey(fieldValue, fkc);
}
}
base.StartDataItemField(exportContext, fieldValue);
}
}
}
One gotacha I found when writing this is that if the foreign key lookup returns a null its going to corrupt your excel document.
How to know when the sort is changed on the Xceed WPF grid
Visual Studio Hanging
You can see the GDI Object count in Process Explorer or if Task Manager - by clicking on the view -> select columns menu and ticking GDI Objects.
You can't fix this as far as I know but you can delay the inevitable by increasing the limit.
See this link for information on how to change the GDIProcessHandleQuota limit in the registry: http://msdn.microsoft.com/en-us/library/ms724291(VS.85).aspx
I set the limit to 15,000 on my windows 7 64 bit desktop with 4 gig of ram and haven't had any issues.
For more detailed information on GDI Objects see this page: http://blogs.technet.com/markrussinovich/archive/2010/03/31/3322423.aspx
A better way to declare your ValueConverters in XAML
This is OK but it’s fairly verbose and leads to converters being defined all over the place. An alternative is to use a MarkupExtension to minimize the amount of XAML code required. E.g.:
This approach has an added advantage of ensuring that all your converters are singletons.
This article explains a generic markup extension class that hides the MarkupExtension implementation details and makes the process above even easier.
You can then modify your classes to inherit from this class and they nolonger need to worry about implementing MarkupExtension E.g.:
WPF Xceed Grid: How to prevent/disable validation for objects that implement IDataErrorInfo
How to redirect Console and Debug output to your UI
Why doesn't my keyboard navigation work on templated ComboBoxes/ItemControls
Example:
How to iterate over the rows and cells in an Xceed WPF DataGrid
How do you use a converter on a trigger
ConverterParameter=50}"
How to make Tooltips display on disabled WPF controls
E.g.
<Button Content="Example Button"
ToolTipService.ShowOnDisabled="True"
ToolTip="I’m visible even when the control is not enabled"/>
How to obtain access to nested types through XAML
For example I want to access the View member on the Shipping type which is nested inside the Setup type e.g.
public struct Setup
{
public struct Shipping
{
public const string View = "This is the value I want to access";
}
}
The syntax would be CommandParameter="{x:Static demoAlias:Setup+Shipping.View}"
PropertyChangedEventManager is not marked as serializable
The solution is to mark your PropertyChangedEventManager as NonSerialized.
#region INotifyPropertyChanged Members
[field: NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, e);
}
}
#endregion
Binding to a Settings file
<ToggleButton x:Name="btnNetworkSpeed" Margin="5,0,0,0" FlowDirection="LeftToRight" IsChecked="{Binding Source={x:Static InfrastructureProperties:Settings.Default}, Path=IsSlowConnectionSpeed, Mode=TwoWay}" FontSize="9"/>
The trick to making this work is to change the settings file access modifier to Public. To do this go into the settings file and at the top of the designer there is a drop down. What this does under the covers is change the compile tool from 'SettingsSingleFileGenerator' to 'PublicSettingsSingleFileGenerator'
If you want the setting to be persisted on the closing event of the application add this line and you're done.
protected override void OnExit(ExitEventArgs e)
{
Modules.Infrastructure.Properties.Settings.Default.Save();
...
}
Bug in ItemsControl with nested bindings
After a lot of searching I found a few other people with the same problem but not many solutions. One workaround that seems to have been successful is to move the template into a resource. This didn't work for me but might be worth a try if you come across the same thing.
Workaround for bug in WPF Splash Screen
System.ComponentModel.Win32Exception was unhandled
Message="The operation completed successfully"
Source="WindowsBase"
ErrorCode=-2147467259
NativeErrorCode=0
StackTrace:
at MS.Win32.UnsafeNativeMethods.SetActiveWindow(HandleRef hWnd)
at System.Windows.SplashScreen.Close(TimeSpan fadeoutDuration)
at System.Windows.SplashScreen.
b__0(Object splashObj) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.Run()
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at System.Windows.Application.Run()
at Qmastor.WinApp.PortManagement.Loader.App.Main() in C:\SourceCode\PitToPort\PortManagement\Qmastor.WinApp.PortManagement.Loader\obj\Debug\App.g.cs:line 0
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
The suggested solution on the connect website doesn't seem to work for me and I failed to find any other solutions on the net other than rolling your own splash screen. However I found that adding this line of code before closing the splash screen seems to fix the problem for me:
App.Current.MainWindow.Focus();
_splash.Close(TimeSpan.Zero);