Wpf – relative image source path

There’s a handy method in System.IO.Path which can help with this: return Path.GetFullPath(“Resources/image.jpg”); This should return ‘C:\Folders\MoreFolders\Resources\image.jpg’ or whatever the full path is in your context. It will use the current working folder as the starting point. Link to MSDN documentation on GetFullPath.

How can I produce a “print preview” of a FlowDocument in a WPF application?

Taking the hint from the comment added to my question, I did this: private string _previewWindowXaml = @”<Window xmlns=”http://schemas.microsoft.com/netfx/2007/xaml/presentation” xmlns:x =’http://schemas.microsoft.com/winfx/2006/xaml’ Title=”Print Preview – @@TITLE” Height=”200″ Width=”300″ WindowStartupLocation =’CenterOwner’> <DocumentViewer Name=”dv1″/> </Window>”; internal void DoPreview(string title) { string fileName = System.IO.Path.GetRandomFileName(); FlowDocumentScrollViewer visual = (FlowDocumentScrollViewer)(_parent.FindName(“fdsv1”)); try { // write the XPS document using (XpsDocument doc … Read more

Global hotkeys in WPF working from every window

You can use the same approach as in WinForms with some adaptation: use WindowInteropHelper to get HWND (instead of Handle property of a form) use HwndSource to handle window messages (instead of overriding WndProc of a form) don’t use Key enumeration from WPF – it’s values are not the ones you want Complete code: [DllImport(“User32.dll”)] … Read more

Merge Cells in WPF DataGrid

Try use DataGridTemplateColumn. I created sample test class for databinding public class Test { public Test(string name, string attribute1, string attribute2) { Name = name; Attributes = new Attribute(attribute1, attribute2); } public string Name { get; set; } public Attribute Attributes { get; set; } } public class Attribute { public Attribute(string attribute1, string attribute2) … Read more

Is it possible to initialize WPF UserControls in different threads?

Background Information on UI Threading Models Normally an application has one “main” UI thread…and it may have 0 or more background/worker/non-UI threads where you (or the .NET runtime/framework) does background work. (…there’s a another special thread in WPF called the rendering thread but I will skip that for now…) For example, a simple WPF Application … Read more

Binding SelectedItems of ListView to ViewModel

1. One way to source binding: You have to use SelectionChanged event. The easiest way is to write eventhandler in codebehind to “bind selecteditems” to viewmodel. //ViewModel public ICollectionView BusinessCollection {get; set;} public List<YourBusinessItem> SelectedObject {get; set;} //Codebehind private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e) { var viewmodel = (ViewModel) DataContext; viewmodel.SelectedItems = listview.SelectedItems .Cast<YourBusinessItem>() .ToList(); … Read more