Christian Moser's WPF Tutorial.net News and tutorials around the Windows Presentation Foundation en http://www.wpftutorial.net Fri, 03 Sep 2010 15:42:53 +0200 RadioButton moc@zuehlke.com (Christian Moser) <h1>Radio Button</h2> <img style="padding-left: 100px;" src="images/radiobuttons.jpg" /> <h2>Introduction</h2> <p>The <code>RadioButton</code> control has its name from old analog radios which had a number of programmable station buttons. When you pushed one in, the previosly selected poped out. So only one station can be selected at a time.</p> <p>The <code>RadioButton</code> control has the same behavior. It lets the user choose <b>one option out of a few</b>. It the list of options gets longer, you should prefer a combo or list box instead.</p> <p>To define which RadioButtons belong togehter, you have to <b>set the <code>GroupName</code> </b>to the same name.</p> <p>To <b>preselect</b> one option set the <b><code>IsChecked</code> property to <code>True</code>.</b></p> <br/> <div class="codeblock"><pre class="xaml xaml" style="font-family: courier new,courier,monospace;"> <span style="color: #800000;"><span style="color: #800000;">&lt;StackPanel<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;RadioButton</span> <span style="color: #ff0000;">GroupName</span>=<span style="color: #0000ff;">&quot;Os&quot;</span> <span style="color: #ff0000;">Content</span>=<span style="color: #0000ff;">&quot;Windows XP&quot;</span> <span style="color: #ff0000;">IsChecked</span>=<span style="color: #0000ff;">&quot;True&quot;</span><span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;RadioButton</span> <span style="color: #ff0000;">GroupName</span>=<span style="color: #0000ff;">&quot;Os&quot;</span> <span style="color: #ff0000;">Content</span>=<span style="color: #0000ff;">&quot;Windows Vista&quot;</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;RadioButton</span> <span style="color: #ff0000;">GroupName</span>=<span style="color: #0000ff;">&quot;Os&quot;</span> <span style="color: #ff0000;">Content</span>=<span style="color: #0000ff;">&quot;Windows 7&quot;</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;RadioButton</span> <span style="color: #ff0000;">GroupName</span>=<span style="color: #0000ff;">&quot;Office&quot;</span> <span style="color: #ff0000;">Content</span>=<span style="color: #0000ff;">&quot;Microsoft Office 2007&quot;</span> <span style="color: #ff0000;">IsChecked</span>=<span style="color: #0000ff;">&quot;True&quot;</span><span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;RadioButton</span> <span style="color: #ff0000;">GroupName</span>=<span style="color: #0000ff;">&quot;Office&quot;</span> <span style="color: #ff0000;">Content</span>=<span style="color: #0000ff;">&quot;Microsoft Office 2003&quot;</span><span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;RadioButton</span> <span style="color: #ff0000;">GroupName</span>=<span style="color: #0000ff;">&quot;Office&quot;</span> <span style="color: #ff0000;">Content</span>=<span style="color: #0000ff;">&quot;Open Office&quot;</span><span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/StackPanel<span style="color: #800000;">&gt;</span></span></span> </pre></div> <h2>How to DataBind Radio Buttons in WPF</h2> <p>The radio button control has a known issue with data binding. If you bind the <code>IsChecked</code> property to a boolean and check the RadioButton, the value gets <code>True</code>. But when you check another RadioButton, the databound value still remains true.</p> <p>The reason for this is, that the Binding gets lost during the unchecking, because the controls internally calls <code>ClearValue()</code> on the dependency property.</p> <div class="codeblock"><pre class="xaml xaml" style="font-family: courier new,courier,monospace;"> <span style="color: #800000;"><span style="color: #800000;">&lt;Window</span>.Resources<span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;EnumMatchToBooleanConverter</span> <span style="color: #ff0000;">x:Key</span>=<span style="color: #0000ff;">&quot;enumConverter&quot;</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/Window</span>.Resources<span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;RadioButton</span> <span style="color: #ff0000;">Content</span>=<span style="color: #0000ff;">&quot;Option 1&quot;</span> <span style="color: #ff0000;">GroupName</span>=<span style="color: #0000ff;">&quot;Options1&quot;</span> </span> <span style="color: #800000;"> <span style="color: #ff0000;">IsChecked</span>=<span style="color: #0000ff;">&quot;{Binding Path=CurrentOption, Mode=TwoWay, </span> <span style="color: #800000;"> Converter={StaticResource enumConverter},</span> <span style="color: #800000;"> ConverterParameter=Option1}&quot;</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;RadioButton</span> <span style="color: #ff0000;">Content</span>=<span style="color: #0000ff;">&quot;Option 2&quot;</span> <span style="color: #ff0000;">GroupName</span>=<span style="color: #0000ff;">&quot;Options2&quot;</span> </span> <span style="color: #800000;"> <span style="color: #ff0000;">IsChecked</span>=<span style="color: #0000ff;">&quot;{Binding Path=CurrentOption, Mode=TwoWay, </span> <span style="color: #800000;"> Converter={StaticResource enumConverter},</span> <span style="color: #800000;"> ConverterParameter=Option2}&quot;</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;RadioButton</span> <span style="color: #ff0000;">Content</span>=<span style="color: #0000ff;">&quot;Option 3&quot;</span> <span style="color: #ff0000;">GroupName</span>=<span style="color: #0000ff;">&quot;Options3&quot;</span> </span> <span style="color: #800000;"> <span style="color: #ff0000;">IsChecked</span>=<span style="color: #0000ff;">&quot;{Binding Path=CurrentOption, Mode=TwoWay, </span> <span style="color: #800000;"> Converter={StaticResource enumConverter},</span> <span style="color: #800000;"> ConverterParameter=Option3}&quot;</span> <span style="color: #800000;">/&gt;</span></span> </pre></div> <div class="codeblock"><pre class="csharp csharp" style="font-family: courier new,courier,monospace;"> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #2b91af; font-weight: bold;">class</span> EnumMatchToBooleanConverter <span style="color: #008000;">:</span> IValueConverter <span style="color: #000000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #2b91af; font-weight: bold;">object</span> Convert<span style="color: #000000;">&#40;</span><span style="color: #2b91af; font-weight: bold;">object</span> value, Type targetType, <span style="color: #2b91af; font-weight: bold;">object</span> parameter, CultureInfo culture<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>value <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> || parameter <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">return</span> false; <span style="color: #2b91af; font-weight: bold;">string</span> checkValue <span style="color: #008000;">=</span> value.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; <span style="color: #2b91af; font-weight: bold;">string</span> targetValue <span style="color: #008000;">=</span> parameter.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; <span style="color: #0600FF; font-weight: bold;">return</span> checkValue.<span style="color: #0000FF;">Equals</span><span style="color: #000000;">&#40;</span>targetValue, StringComparison.<span style="color: #0000FF;">InvariantCultureIgnoreCase</span><span style="color: #000000;">&#41;</span>; <span style="color: #000000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #2b91af; font-weight: bold;">object</span> ConvertBack<span style="color: #000000;">&#40;</span><span style="color: #2b91af; font-weight: bold;">object</span> value, Type targetType, <span style="color: #2b91af; font-weight: bold;">object</span> parameter, CultureInfo culture<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>value <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> || parameter <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">return</span> null; <span style="color: #2b91af; font-weight: bold;">bool</span> useValue <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #2b91af; font-weight: bold;">bool</span><span style="color: #000000;">&#41;</span>value; <span style="color: #2b91af; font-weight: bold;">string</span> targetValue <span style="color: #008000;">=</span> parameter.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>useValue<span style="color: #000000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #2b91af; font-weight: bold;">Enum</span>.<span style="color: #0000FF;">Parse</span><span style="color: #000000;">&#40;</span>targetType, targetValue<span style="color: #000000;">&#41;</span>; <span style="color: #0600FF; font-weight: bold;">return</span> null; <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span> </pre></div> http://www.wpftutorial.net/RadioButton.html Fri, 03 Sep 2010 15:42:53 +02002010-08-26 20:42:54 Triggers moc@zuehlke.com (Christian Moser) <h1>Triggers in Styles</h1> <p>Triggers define a list of setters that are executed if the specified condition is fulfilled. WPF knows tree diferent types of triggers</p> <ul> <li><b>Property Triggers</b> execute when a property gets a specified value.</li> <li><b>Event Triggers</b> execute when a specified event is fired.</li> <li><b>Data Triggers</b> execute when a binding expression reaches a specified value.</li> </ul> <h2>Property Triggers</h2> <p>A property trigger executes a list of setters when a property gets a specified value. If the value changes the trigger undoes the setters.</p> <img style="padding: 10px; padding-left: 100px;" src='images/propertytrigger.jpg' /> <div class="codeblock"><pre class="xaml xaml" style="font-family: courier new,courier,monospace;"> <span style="color: #800000;"><span style="color: #800000;">&lt;Style</span> <span style="color: #ff0000;">x:Key</span>=<span style="color: #0000ff;">&quot;styleWithTrigger&quot;</span> <span style="color: #ff0000;">TargetType</span>=<span style="color: #0000ff;">&quot;Rectangle&quot;</span><span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;Setter</span> <span style="color: #ff0000;">Property</span>=<span style="color: #0000ff;">&quot;Fill&quot;</span> <span style="color: #ff0000;">Value</span>=<span style="color: #0000ff;">&quot;LightGreen&quot;</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;Style</span>.Triggers<span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;Trigger</span> <span style="color: #ff0000;">Property</span>=<span style="color: #0000ff;">&quot;IsMouseOver&quot;</span> <span style="color: #ff0000;">Value</span>=<span style="color: #0000ff;">&quot;True&quot;</span><span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;Setter</span> <span style="color: #ff0000;">Property</span>=<span style="color: #0000ff;">&quot;Fill&quot;</span> <span style="color: #ff0000;">Value</span>=<span style="color: #0000ff;">&quot;Red&quot;</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/Trigger<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/Style</span>.Triggers<span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/Style<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;Rectangle</span> <span style="color: #ff0000;">Style</span>=<span style="color: #0000ff;">&quot;{StaticResource styleWithTrigger}&quot;</span><span style="color: #800000;">&gt;</span><span style="color: #800000;">&lt;/Rectangle<span style="color: #800000;">&gt;</span></span></span> </pre></div> <h2>Event Triggers</h2> <h2>Data Triggers</h2> <h2>Multi Triggers</h> <h2>Trick to use triggers everywhere</h2> <p>Triggers are only available in styles and templates. Even if every control has a property <code>Triggers</code>, it only works with <code>EventTriggers</code>. If you want to use normal triggers outside a style or a template, you can use a little trick. Just wrap your code with the following snippet:</p> <div class="codeblock"><pre class="xaml xaml" style="font-family: courier new,courier,monospace;"> <span style="color: #800000;"><span style="color: #800000;">&lt;Control<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;Control</span>.Template<span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;ControlTemplate<span style="color: #800000;">&gt;</span></span></span> <span style="color: #808080; font-style: italic;">&lt;!-- Triggers are available here --&gt;</span> <span style="color: #800000;"><span style="color: #800000;">&lt;/ControlTemplate<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/Control</span>.Template<span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/Control<span style="color: #800000;">&gt;</span></span></span> </pre></div> http://www.wpftutorial.net/Triggers.html Fri, 03 Sep 2010 15:42:54 +02002010-07-30 12:01:51 WPF Inspector moc@zuehlke.com (Christian Moser) <h1>Download WPF Inspector and get the insight!</h1> <img src="images/inspector_ad.jpg" /> <br/><br/> <div style="text-align:right; margin-right: 100px;"> <b><a href="uploads/WPFInspector_0_9_5.msi" style="font-size: 16px; color:blue;" onclick="javascript:urchinTracker('uploads/WPFInspector_0_9_5.msi')"></>Download WPF Inspector 0.9.5 (Beta)</a></b> </div> <h2>My motivation</h2> <p>While developing WPF applications I often had difficulties to see what is excatly going on in there. What is the DataContext of a particular element? Why is that data binding not working? On what element do I have binding errors? Why is this trigger not active?. I wanted to build a tool that helps me in my daily work to get around this typical issues...</p> <p>There are some pretty powerful tools out there that do a good job, like <a href="http://blois.us/Snoop/">Snoop</a> or <a href="http://joshsmithonwpf.wordpress.com/mole/">Mole</a>. But I wanted to add some more functionality. So I started the "WPF Inspector" project.</p> <p>It's still a bit buggy and can cause some side effects while inspecting your application, but I think its really helpful and time to share.<br/><br/> Just download it and try it out. If you have any feedback or feature requests, feel free to <a href="christian.php">contact me</a>. </p> <h2>Start to inspect an application</h2> <p>Start the WPF inspector from your startmenu. WPF Inspecor will automatically list all running 32-bit and 64-bit WPF applications. Double click on one, or choose "inspect" to start the inspection.</p> <img src="images/inspector1.png" style="padding-left: 100px;" /> <h2>Browse the logical and visual tree</h2> <p>After the inspector has been sucessfully hooked into your application, a window opens, that shos you the elements of your application. You can navigate through the visual and logical tree and see properties, datacontext, resources and triggers.</p> <img src="images/inspector2.png" style="padding-left: 100px;" /> <h2>Find elements directly in the application</h2> <p>You can use the mouse to hover over the inspecting application by holding the CTRL key pressed to automatically select the element below the mouse. A red or blue outline indicates the selected element, depending if you are have the visual or the logical tree browser active.</p> <img src="images/inspector3.png" style="padding-left: 100px;" /> <h2>See what's in the DataContext</h2> <p>By using the WPF Inspector it's very easy to see what data structure is inherited to a specific element by the data context. Just select the element and switch to the DataContext tab. You can even change editable values from your data model.</p> <img src="images/inspector4.png" style="padding-left: 100px;" /> <h2>Debug Triggers</h2> <p>Triggers are a very flexible and powerful thing, but they are event hard to debug. WPF Inspector allows you to directly see all conditions and values of a trigger and check if he is currently active or not. This makes life a lot easier!</p> <img src="images/inspector5.png" style="padding-left: 100px;" /> <h2>Check your application against prooven WPF design principles</h2> <p>WPF Inspector includes a set or rules that check your application against some design principles. The rules are divided into the categories functionality, performance and maintainability. The found issues are categorizes into tree levels of severity. My favorite rule is the databinding error checker. It shows you excactly what property on what element failed to bind!</p> <br/><br/> <img src="images/inspector6.png" style="padding-left: 100px;" /> <h2>Version History</h2> <ul> <li><b>WPF Inspector 0.9.5</b> <ul> <li>Fixed issue with logical tree selection</li> <li>Improved adorner for grids</li> <li>Added property editor for GridLengths</li> <li>Fixed issue with theming</li> </ul> </li> <li><b>WPF Inspector 0.9.4</b> <ul> <li>Fixed layout recursion issue</li> <li>Application Theme switcher</li> <li>Size changer (resize to common sizes)</li> <li>Highlighting of changed properties</li> <li>Breakpoints on property changes</li> <li>Style explorer</li> <li>Lot of bugfixes</li> </ul> </li> <li><b>WPF Inspector 0.9.3</b> <ul> <li>Re-evaluating rules when a value changed</li> <li>Refactored rule engine</li> <li>Performance improvements</li> </ul> </li> <li><b>WPF Inspector 0.9.2</b> <ul> <li>Fixed some styling issues</li> <li>Added Introduction Dialog on first starup</li> <li>Changed installer package type to 32-bit</li> </ul> </li> </ul> http://www.wpftutorial.net/Inspector.html Fri, 03 Sep 2010 15:42:54 +02002010-07-29 21:11:13 Debugging Animations moc@zuehlke.com (Christian Moser) <h1>Debugging style and template Errors</h1> <h3>Common Errors</h3> <p>If you get the following error: <code>Cannot animate ... on an immutable object instance.</code> it could be that you are run into one of the following issues:</p> <ul> <li>You are animating a dependency property without having a value set</li> <li>You are animating a dependency property who's current value is defined in another assembly that is not merged into the resource dictionary.</li> <li>You are animating a value that is currently databound</li> <li>You have an invalid property path specified.</li> </ul> http://www.wpftutorial.net/DebuggingAnimations.html Fri, 03 Sep 2010 15:42:54 +02002010-07-28 14:21:16 Home moc@zuehlke.com (Christian Moser) <table cellpadding="0" cellspacing="0"> <tr> <td style="width: 511px; padding-bottom: 20px; vertical-align: top;"> <h1>Welcome to the WPF Tutorial</h1> <img src="images/title.jpg" style="width: 400px; height: 349px; padding-left: 30px;" /> <p style="padding-top: 20px; padding-right: 10px; padding-left: 0px;">Welcome to my website about the Windows Presentation Foundation. The tutorials will show you how to create the next generation user experience. I hope you will get amazed by the possibilities of this fascinating technology.</p> <br/> [news] </td> <td rowspan="2" style="padding-left: 30px; margin-top: 10px; vertical-align: top; border-left: #eeeeee 1px solid;" > [tipofday] [popular] </td> </tr> </table> http://www.wpftutorial.net/Home.html Fri, 03 Sep 2010 15:42:54 +02002010-07-28 12:05:20 Third Party Controls moc@zuehlke.com (Christian Moser) <h1>WPF - Third Party Controls</h1> <h2>WPF Component Vendors</h2> <ul> <li><a href="http://www.componentart.com/products/wpf/navigation/">Component Art</a></li> <li><a href="http://www.devexpress.com/Products/NET/Controls/WPF/">DevExpress</a></li> <li><a href="http://www.syncfusion.com/products/user-interface-edition/wpf">SyncFusion</a></li> <li><a href="http://www.infragistics.com/dotnet/netadvantage/wpf.aspx#Overview">Infragistics</a></li> <li><a href="http://xceed.com/pages/TopMenu/Products/ProductSearch.aspx?Lang=EN-CA&Category=0617b4dd-af9a-4e34-a1a1-d1129237d614">Xceed</a></li> <li><a href="http://www.telerik.com/products/wpf.aspx">Telerik</a></li> <li><a href="http://www.actiprosoftware.com/Products/DotNet/WPF/WPFStudio/Default.aspx">Actipro</a></li> </ul> <hr> <table> <tr> <td style="vertical-align: top; width: 300px;"> <h2>Data Grids</h2> <ul> <li><a href="http://www.infragistics.com/dotnet/netadvantage/wpf/xamdatagrid.aspx#Overview">Infragistics Data Grid</a></li> <li><a href="http://xceed.com/Grid_WPF_New.html">Xceed Data Grid</a></li> <li><a href="http://www.componentone.com/SuperProducts/GridWPF/ ">Component One Data Grid</a></li> <li><a href="http://www.syncfusion.com/products/user-interface-edition/wpf/grid">Syncfusion Essential Grid</a></li> <li><a href="http://www.telerik.com/products/wpf/gridview.aspx">Telerik RadGridView for WPF</a></li> <li><a href="http://www.componentart.com/products/wpf/datagrid/">ComponentArt DataGrid</a></li> </ul> <h2>Charts</h2> <ul> <li><a href="http://www.infragistics.com/dotnet/netadvantage/wpf/xamchart.aspx#Overview ">Infragistics xamChart</a></li> <li><a href="http://sourceforge.net/projects/swordfishcharts">Swordfish Charts</a></li> <li><a href="http://www.componentone.com/SuperProducts/ChartWPF/ ">Component One Chart</a></li> <li><a href="http://www.visifire.com/">Visifire Chart for WPF and Silverlight</a></li> <li><a href="http://www.codeproject.com/KB/WPF/wpfgraph.aspx">WPF Graph on Code Project</a></li> <li><a href="http://www.codeproject.com/KB/WPF/WPF_3D_Bar_chart_control.aspx">Free 3D Chart</a></li> <li><a href="http://www.codeproject.com/KB/WPF/WPFChart3D.aspx">Free High Performance 3D Chart</a></li> <li><a href="http://dynamicdatadisplay.codeplex.com/">D3 Dynamic Data Display</a></li> <li><a href="http://www.syncfusion.com/products/user-interface-edition/wpf/chart">Syncfusion Essential Chart</a></li> <li><a href="http://www.syncfusion.com/products/user-interface-edition/wpf/gauge">Syncfusion Gauge</a></li> <li><a href="http://www.telerik.com/products/wpf/gauge.aspx">Telerik RadGauge for WPF</a></li> <li><a href="http://www.telerik.com/products/wpf/chart.aspx">Telerik RadChart for WPF</a></li> <li><a href="http://www.componentart.com/products/wpf/charting/">ComponentArt Chart</a></li> </ul> <h2>Dialogs</h2> <ul> <li><a href="http://wpfdialogs.codeplex.com/">Pure WPF FileOpen, FileSave and FolderBrowser Dialogs</a></li> </ul> <h2>Dock</h2> <ul> <li><a <li><a href="http://www.infragistics.com/dotnet/netadvantage/wpf/xamdockmanager.aspx#Overview ">Infragistics Dock Manager</a></li> <li><a href="http://www.actiprosoftware.com/Products/DotNet/WPF/Docking/Default.aspx">Actipro Dock Panel</a></li> <li><a href="http://www.devcomponents.com/dotnetbar-wpf/">DevComponents Dock Panel</a></li> <li><a href="http://sourceforge.net/projects/wpfdockinglib/">WPF Docking Library (Open Source)</a></li> <li><a href="http://avalondock.codeplex.com">Avalon Dock (Open Source)</a></li> <li><a href="http://www.telerik.com/products/wpf/docking.aspx">Telerik RadDocking for WPF</a></li> </ul> <h2>Editors</h2> <ul> <li><a href="http://www.infragistics.com/dotnet/netadvantage/wpf/xameditors.aspx#Overview ">Infragistics xamEditors</a></li> <li><a href="http://xceed.com/Editors_WPF_Intro.html">Xceed Editors</a></li> <li><a href="http://www.devcomponents.com/dotnetbar-wpf/WPFNumericDoubleInput.aspx ">DevComponents Numeric Editor</a></li> <li><a href="http://www.telerik.com/products/wpf/numericupdown.aspx">Telerik RadNumericUpDown for WPF</a></li> <li><a href="http://www.syncfusion.com/products/user-interface-edition/wpf/edit">Syncfusion Essential Edit (with Syntax Highlighting)</a></li> <li><a href="http://www.syncfusion.com/products/user-interface-edition/wpf/diagram">Syncfusion Essential Diagram Editor</a></li> <li><a href="http://wpfcalendarcontrol.codeplex.com/">WPF Calendar Control</a></li> </ul> <h2>Effects</h2> <ul> <li><a href="http://www.codeplex.com/transitionals">Transitionals - Framework to transition between screens.</li> <li><a href="http://www.codeplex.com/fx">WPF Shader and Transition FX</a></li> <li><a href="http://www.codeplex.com/wpffx">Windows Presentation Foundation Pixel Shader Effects Library</a></li> <li><a href="http://www.codeplex.com/dotwaywpf">DotWay WPF - Color Picker, Panels and several Shader Effects</a></li> <li><a href="http://www.telerik.com/products/wpf/drag-drop.aspx">Telerik Drag&amp;Drop for WPF</a></li> </ul> <h2>GIS and Maps</h2> <ul> <li><a href="http://vewpf.codeplex.com/">Microsoft Virual Earth Control</a></li> <li><a href="http://wpfsharpmapcontrols.codeplex.com/">Sharp Map Control</a></li> </ul> <h2>Multimedia</h2> <ul> <li><a href="http://directshownet.sourceforge.net/">DirectShowLib - .NET Wrapper for DirectShow</a></li> <li><a href="http://videorendererelement.codeplex.com/">VideoRenderElement</a></li> <li><a href="http://wpfcap.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=13380#ReleaseFiles">Webcam Control</a></li> <li><a href="http://www.codeplex.com/WPFMediaKit">WPF Media Kit - DVD Player, DirectShow, WebCam</li> </ul> </td> <td style="padding-left: 40px; vertical-align: top;"> <h2>Misc</h2> <ul> <li><a href="http://www.codeplex.com/wpf/Release/ProjectReleases.aspx?ReleaseId=15598">WPF Toolkit</a></li> <li><a href="http://www.infragistics.com/dotnet/netadvantage/wpf/xamTabControl.aspx#Overview ">Infragistics Tab Control</a></li> <li><a href="http://www.infragistics.com/dotnet/netadvantage/wpf/xamMonthCalendar.aspx#Overview">Infragistics MonthCalendar</a></li> <li><a href="http://www.actiprosoftware.com/Products/DotNet/WPF/BarCode/Default.aspx">Actipro BarCode</a></li> <li><a href="http://www.actiprosoftware.com/Products/DotNet/WPF/Wizard/Default.aspx">Actipro Wizard</a></li> <li><a href="http://www.actiprosoftware.com/Products/DotNet/WPF/PropertyGrid/Default.aspx">Actipro Property Grid</a></li> <li><a href="http://www.mindscape.co.nz/products/WpfPropertyGrid/default.aspx">Mindscape Property Grid</a></li> <li><a href="http://www.mindscape.co.nz/products/wpfflowdiagrams/">Mindscape Flow Diagrams</a></li> </ul> <h2>Outlook Bar</h2> <ul> <li><a href="http://www.infragistics.com/dotnet/netadvantage/wpf/xamOutlookBar.aspx#Overview ">Infragistics Outlook Bar</a></li> <li><a href="http://www.actiprosoftware.com/Products/DotNet/WPF/Navigation/Default.aspx">Actipro Outlook Bar</a></li> <li><a href="http://www.devcomponents.com/dotnetbar-wpf/NavigationPane.aspx">DevComponents Outlook Bar</a></li> <li><a href="http://www.codeproject.com/KB/WPF/WPFOutlookBar.aspx">Odyssey Outlook Bar</a></li> <li><a href="http://www.codeproject.com/KB/WPF/WPFExplorerBar.aspx">Odyssey Explorer Bar</a></li> <li><a href="http://www.telerik.com/products/wpf/outlookbar.aspx">Telerik RadOutlookBar for WPF</a></li> </ul> <h2>Panels</h2> <ul> <li><a href="http://www.infragistics.com/dotnet/netadvantage/wpf/xamcarouselpanel.aspx#Overview">Infragistics Carousel Panel</a></li> <li><a href="http://www.telerik.com/products/wpf/carousel.aspx">Telerik WPF Carousel Control</a></li> <li><a href="http://www.telerik.com/products/wpf/tileview.aspx">Telerik RadTileView for WPF</a></li> </ul> <h2>Reporting</h2> <ul> <li><a href="http://www.infragistics.com/dotnet/netadvantage/wpf/reporting.aspx#Overview ">Infragistics Reporting for WPF</a></li> <li><a href="http://www.componentone.com/SuperProducts/ReportsWPF/">Component One Reports</a></li> </ul> <h2>Ribbon</h2> <ul> <li><a href="http://fluent.codeplex.com">Fluent Ribbon Control Suite</a></li> <li><a href="http://www.infragistics.com/dotnet/netadvantage/wpf/xamRibbon.aspx#Overview">Infragistics Ribbon</a></li> <li><a href="http://www.actiprosoftware.com/Products/DotNet/WPF/Ribbon/Default.aspx">Actipro Ribbon</a></li> <li><a href="http://www.devcomponents.com/dotnetbar-wpf/wpfribbon/ ">DevComponents Ribbon</a></li> <li><a href="http://www.codeproject.com/KB/WPF/OdysseyRibbonBar.aspx">Odyssey Ribbon</a></li> <li><a href="http://www.telerik.com/products/wpf/ribbonbar.aspx">Telerik WPF UI RibbonBar</a></li> <li><a href="http://windowsclient.net/wpf/wpf35/wpf-35sp1-ribbon-walkthrough.aspx">Free Microsoft WPF Ribbon Control</a></li> </ul> <h2>Toolbar</h2> <ul> <li><a href="http://www.devexpress.com/Products/NET/Controls/WPF/Bar_Menu/">DevExpress ToolBar</a></li> <li><a href="http://www.codeproject.com/KB/tree/WPFBreadcrumbBar.aspx">Odyssey Breadcrumb Bar</a></li> <li><a href="http://www.componentart.com/products/wpf/navigation/">ComponentArt Toolbar</a></li> </ul> <h2>Theming</h2> <ul> <li><a href="http://wpfthemeselector.codeplex.com/">WPF Theme Selector</a></li> </ul> <h2>Tree</h2> <ul> <li><a href="http://www.telerik.com/products/wpf/treeview.aspx">Telerik RadTree View for WPF</a></li> </ul> <h2>Schedule</h2> <ul> <li><a href="http://www.devcomponents.com/dotnetbar-wpf/WPFScheduleControl.aspx ">DevComponents Schedule Control</a></li> <li><a href="http://www.devcomponents.com/dotnetbar-wpf/WPFDateTimePicker.aspx ">DevComponents DateTime Picker</a></li> <li><a href="http://www.componentone.com/SuperProducts/ScheduleWPF/">Component One Schedule</a></li> <li><a href="http://timeline.codeplex.com/">Timeline Control</a></li> <li><a href="http://www.telerik.com/products/wpf/scheduler.aspx">Telerik RadScheduler for WPF</a></li> <li><a href="http://wpfschedule.codeplex.com/">Free WPF Schedule Control</a></li> </ul> <h2>3D</h2> <ul> <li><a href="http://xceed.com/3DViews_WPF_Intro.html">Xceed 3D Views</a></li> </ul> <h2>Web Browser</h2> <ul> <li><a href="http://wpfchromium.codeplex.com/SourceControl/changeset/view/28084#413984">Chromium Web Browser</a></li> </ul> </td> </tr> </table> http://www.wpftutorial.net/3rdPartyLibs.html Fri, 03 Sep 2010 15:42:54 +02002010-07-22 08:35:24 Menus moc@zuehlke.com (Christian Moser) <h1>Menus in WPF</h1> <h2>Menu</h2> <p>The <code>Menu</code> control derives from <code>HeaderedItemsControl</code>. It stacks it items horizontally and draws the typical gray background. The only property that the Menu adds to <code>ItemsControl</code> is the <code>IsMainMenu</code> property. This controls if the menu grabs the focus if the user presses F10 or the ALT key.</p> <img style="padding: 10px; padding-left: 120px;" src='images/menu2.jpg' /> <div class="codeblock"><pre class="xaml xaml" style="font-family: courier new,courier,monospace;"> <span style="color: #800000;"><span style="color: #800000;">&lt;Menu</span> <span style="color: #ff0000;">IsMainMenu</span>=<span style="color: #0000ff;">&quot;True&quot;</span><span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;MenuItem</span> <span style="color: #ff0000;">Header</span>=<span style="color: #0000ff;">&quot;_File&quot;</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;MenuItem</span> <span style="color: #ff0000;">Header</span>=<span style="color: #0000ff;">&quot;_Edit&quot;</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;MenuItem</span> <span style="color: #ff0000;">Header</span>=<span style="color: #0000ff;">&quot;_View&quot;</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;MenuItem</span> <span style="color: #ff0000;">Header</span>=<span style="color: #0000ff;">&quot;_Window&quot;</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;MenuItem</span> <span style="color: #ff0000;">Header</span>=<span style="color: #0000ff;">&quot;_Help&quot;</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/Menu<span style="color: #800000;">&gt;</span></span></span> </pre></div><h2>MenuItem</h2> <p>The <code>MenuItem</code> is a <code>HeaderedItemsControl</code>. The content of the <code>Header</code> property is the caption of the menu. The <code>Items</code> of a MenuItems are its sub menus. The <code>Icon</code> property renders a second content on the left of the caption. This is typically used to draw a little image. But it can be used for type of content.</p> <p>You can define a keyboard shortcut by adding an underscore in front of a character.</p> <img style="padding: 10px; padding-left: 120px;" src='images/wpfmenu2.jpg' /> <div class="codeblock"><pre class="xaml xaml" style="font-family: courier new,courier,monospace;"> <span style="color: #800000;"><span style="color: #800000;">&lt;MenuItem</span> <span style="color: #ff0000;">Header</span>=<span style="color: #0000ff;">&quot;_Edit&quot;</span><span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;MenuItem</span> <span style="color: #ff0000;">Header</span>=<span style="color: #0000ff;">&quot;_Cut&quot;</span> <span style="color: #ff0000;">Command</span>=<span style="color: #0000ff;">&quot;Cut&quot;</span><span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;MenuItem</span>.Icon<span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;Image</span> <span style="color: #ff0000;">Source</span>=<span style="color: #0000ff;">&quot;Images/cut.png&quot;</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/MenuItem</span>.Icon<span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/MenuItem<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;MenuItem</span> <span style="color: #ff0000;">Header</span>=<span style="color: #0000ff;">&quot;_Copy&quot;</span> <span style="color: #ff0000;">Command</span>=<span style="color: #0000ff;">&quot;Copy&quot;</span><span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;MenuItem</span>.Icon<span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;Image</span> <span style="color: #ff0000;">Source</span>=<span style="color: #0000ff;">&quot;Images/copy.png&quot;</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/MenuItem</span>.Icon<span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/MenuItem<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;MenuItem</span> <span style="color: #ff0000;">Header</span>=<span style="color: #0000ff;">&quot;_Paste&quot;</span> <span style="color: #ff0000;">Command</span>=<span style="color: #0000ff;">&quot;Paste&quot;</span><span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;MenuItem</span>.Icon<span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;Image</span> <span style="color: #ff0000;">Source</span>=<span style="color: #0000ff;">&quot;Images/paste.png&quot;</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/MenuItem</span>.Icon<span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/MenuItem<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/MenuItem<span style="color: #800000;">&gt;</span></span></span> </pre></div><h2>Checkable MenuItems</h2> <p>You can make a menu item checkable by setting the <code>IsCheckable</code> property to true. The check state can be queried by the <code>IsChecked</code> property. To get notified when the check state changes you can add a handler to the <code>Checked</code> and <code>Unchecked</code> property.</p> <img style="padding: 10px; padding-left: 120px;" src='images/checkMenu.jpg' /> <div class="codeblock"><pre class="xaml xaml" style="font-family: courier new,courier,monospace;"> <span style="color: #800000;"><span style="color: #800000;">&lt;MenuItem</span> <span style="color: #ff0000;">Header</span>=<span style="color: #0000ff;">&quot;_Debug&quot;</span><span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;MenuItem</span> <span style="color: #ff0000;">Header</span>=<span style="color: #0000ff;">&quot;Enable Debugging&quot;</span> <span style="color: #ff0000;">IsCheckable</span>=<span style="color: #0000ff;">&quot;True&quot;</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/MenuItem<span style="color: #800000;">&gt;</span></span></span> </pre></div><h2>Separators</h2> <p>Separator is a simple control to group menu items. It's rendered as a horizontal line. It can also be used in <code>ToolBar</code> and <code>StatusBar</code>.</p> <img style="padding: 10px; padding-left: 120px;" src="images/menuseparator.jpg" /> <div class="codeblock"><pre class="xaml xaml" style="font-family: courier new,courier,monospace;"> <span style="color: #800000;"><span style="color: #800000;">&lt;Menu<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;MenuItem</span> <span style="color: #ff0000;">Header</span>=<span style="color: #0000ff;">&quot;_File&quot;</span><span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;MenuItem</span> <span style="color: #ff0000;">Header</span>=<span style="color: #0000ff;">&quot;_New...&quot;</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;Separator</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;MenuItem</span> <span style="color: #ff0000;">Header</span>=<span style="color: #0000ff;">&quot;_Open...&quot;</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;Separator</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;MenuItem</span> <span style="color: #ff0000;">Header</span>=<span style="color: #0000ff;">&quot;_Save&quot;</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;MenuItem</span> <span style="color: #ff0000;">Header</span>=<span style="color: #0000ff;">&quot;_Save As...&quot;</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;Separator</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;MenuItem</span> <span style="color: #ff0000;">Header</span>=<span style="color: #0000ff;">&quot;_Exit&quot;</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/MenuItem<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/Menu<span style="color: #800000;">&gt;</span></span></span> </pre></div><h2>Callbacks</h2> <p>You can register a callback to any menu item by adding a callback to the <code>Click</code> event.</p> <div class="codeblock"><pre class="xaml xaml" style="font-family: courier new,courier,monospace;"> <span style="color: #800000;"><span style="color: #800000;">&lt;Menu<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;MenuItem</span> <span style="color: #ff0000;">Header</span>=<span style="color: #0000ff;">&quot;_File&quot;</span><span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;MenuItem</span> <span style="color: #ff0000;">Header</span>=<span style="color: #0000ff;">&quot;_New...&quot;</span> <span style="color: #ff0000;">Click</span>=<span style="color: #0000ff;">&quot;New_Click&quot;</span><span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/MenuItem<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/Menu<span style="color: #800000;">&gt;</span></span></span> </pre></div> <div class="codeblock"><pre class="csharp csharp" style="font-family: courier new,courier,monospace;"> <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">void</span> New_Click<span style="color: #000000;">&#40;</span><span style="color: #2b91af; font-weight: bold;">object</span> sender, RoutedEventArgs e<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> MessageBox.<span style="color: #0000FF;">Show</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;You clicked 'New...'&quot;</span><span style="color: #000000;">&#41;</span>; <span style="color: #000000;">&#125;</span> </pre></div> <h2>How to bind MenuItems dynamically using MVVM</h2> <p>If you are using the model-view-viewmodel pattern, you probably want to define the available menu command dynamically in your code and then bind them to a <code>MenuItem</code> control. The following sample shows you how to do this:</p> <div class="codeblock"><pre class="xaml xaml" style="font-family: courier new,courier,monospace;"> <span style="color: #800000;"><span style="color: #800000;">&lt;Menu<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;Menu</span>.Resources<span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;Style</span> <span style="color: #ff0000;">x:Key</span>=<span style="color: #0000ff;">&quot;ThemeMenuItemStyle&quot;</span> <span style="color: #ff0000;">TargetType</span>=<span style="color: #0000ff;">&quot;MenuItem&quot;</span><span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;Setter</span> <span style="color: #ff0000;">Property</span>=<span style="color: #0000ff;">&quot;Header&quot;</span> <span style="color: #ff0000;">Value</span>=<span style="color: #0000ff;">&quot;{Binding Name}&quot;</span><span style="color: #800000;">&gt;</span><span style="color: #800000;">&lt;/Setter<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;Setter</span> <span style="color: #ff0000;">Property</span>=<span style="color: #0000ff;">&quot;Command&quot;</span> <span style="color: #ff0000;">Value</span>=<span style="color: #0000ff;">&quot;{Binding ActivateCommand}&quot;</span><span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;Setter</span> <span style="color: #ff0000;">Property</span>=<span style="color: #0000ff;">&quot;IsChecked&quot;</span> <span style="color: #ff0000;">Value</span>=<span style="color: #0000ff;">&quot;{Binding IsActive}&quot;</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;Setter</span> <span style="color: #ff0000;">Property</span>=<span style="color: #0000ff;">&quot;IsCheckable&quot;</span> <span style="color: #ff0000;">Value</span>=<span style="color: #0000ff;">&quot;True&quot;</span><span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/Style<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/Menu</span>.Resources<span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;MenuItem</span> <span style="color: #ff0000;">Header</span>=<span style="color: #0000ff;">&quot;Themes&quot;</span> <span style="color: #ff0000;">ItemsSource</span>=<span style="color: #0000ff;">&quot;{Binding Themes}&quot;</span> </span> <span style="color: #800000;"> <span style="color: #ff0000;">ItemContainerStyle</span>=<span style="color: #0000ff;">&quot;{StaticResource ThemeMenuItemStyle}&quot;</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/Menu<span style="color: #800000;">&gt;</span></span></span> </pre></div><h2>Keyboard Shortcuts</h2> <p>To add a keyboard shortcut to a menu item, add a underscode "_" in front of the caracter you want to use as your hot key. This automatically sets the <code>InputGestureText</code> to an appropriate value. But you can also override the proposed text by setting this property to a text of your choice.</p> http://www.wpftutorial.net/Menus.html Fri, 03 Sep 2010 15:42:54 +02002010-07-21 10:14:30 Books moc@zuehlke.com (Christian Moser) <h1>Books about WPF</h1> <h2>Brand New!</h2> <table width="700"> <tr style="height: 200px;"> <td><img src="images/book_wpf4unleashed.png" style="width: 150px;"></td> <td style="vertical-align: top;"> <h3>WPF 4 - Unleashed</h3> <p>In my opinion one of the best books about WPF - now updated to version 4.0. It's printed fully in color, so all code samples have syntax highlighting. It includes all the good explanations and samples of version 3 including new chapters about multitouch, XAML 2009, VSM, improved text rendering, windows 7 shell integration and more. </p> <p><table> <tr> <td style="width: 100px;"><b>Autor:</b></td> <td>Adam Nathan</td> </tr> <tr> <td style="width: 100px;"><b>ISBN:</b></td> <td>978-0672331190</td> </tr> <tr> <td style="width: 100px;"><b>Published:</b></td> <td>June 2010</td> </tr> <tr> <td></td> <td><a href="http://www.amazon.de/WPF-4-Unleashed-Adam-Nathan/dp/0672331195/ref=sr_1_1?ie=UTF8&s=books-intl-de&qid=1279693891&sr=8-1" target="_blank">Find it at Amazon</td> </tr> </table> </p> </td> </tr> </table> <hr /> <table width="700"> <tr style="height: 200px;"> <td><img src="images/book_wpfcontroldev.png" style="width: 150px;"></td> <td style="vertical-align: top;"> <h3>WPF Control Development Unleashed</h3> <p>In this book, two leading Windows Presentation Foundation experts give developers everything they need to build next-generation WPF applications--software that is more robust, usable, and compelling. Drawing on their close ties with Microsoft's WPF development team, Pavan Podila and Kevin Hoffman give you a clear, robust, and practical understanding of WPF, its underpinnings, its overall architecture, and its design philosophy.</p> <p><table> <tr> <td style="width: 100px;"><b>Autor:</b></td> <td>Pavan Podila, Kevin Hoffman</td> </tr> <tr> <td style="width: 100px;"><b>ISBN:</b></td> <td>978-0672330339</td> </tr> <tr> <td style="width: 100px;"><b>Published:</b></td> <td>September 2009</td> </tr> <tr> <td></td> <td><a href="http://www.amazon.de/gp/product/0672330334/ref=sib_rdr_dp" target="_blank">Find it at Amazon</td> </tr> </table> </p> </td> </tr> <tr style="height: 200px;"> <td><img src="images/book_wpfunleashed.jpg" style="width: 150px;"></td> <td style="vertical-align: top;"> <h3>Windows Presentation Foundation - Unleashed (My favorite!)</h3> <p>In my opinion one of the best book about WPF. It covers all important themes including 3D programming. All code samples have syntax highlighting. Digging deeper sections with additional informations for advanced programmers.</p> <p><table> <tr> <td style="width: 100px;"><b>Autor:</b></td> <td>Adam Nathan</td> </tr> <tr> <td style="width: 100px;"><b>ISBN:</b></td> <td>978-0672328916</td> </tr> <tr> <td style="width: 100px;"><b>Published:</b></td> <td>January 2007</td> </tr> <tr> <td></td> <td><a href="http://www.amazon.de/Windows-Presentation-Foundation-Unleashed-Nathan/dp/0672328917/ref=pd_sim_eb_1" target="_blank">Find it at Amazon</td> </tr> </table> </p> </td> </tr> <tr style="height: 200px;"> <td><img src="images/book_appcodemarkup.jpg" style="width: 130px;"></td> <td style="vertical-align: top;"> <h3>Application = Code + Markup</h3> <p>"Get the definitive guide to the Windows Presentation Foundation (WPF), the new client programming interface for the Microsoft .NET Framework 3.0 and Windows Vista. Award-winning author Charles Petzold teaches you how to combine C# code and the Extensible Application Markup Language (XAML) to develop applications for the WPF.". I cannot unterstand how anyone can write a book about a UI technology without a single illustration. If you prefer gray text to colorful images, it might be the book for you. </p> <p><table> <tr> <td style="width: 100px;"><b>Autor:</b></td> <td>Charles Petzold</td> </tr> <tr> <td style="width: 100px;"><b>ISBN:</b></td> <td>978-0735619579</td> </tr> <tr> <td style="width: 100px;"><b>Published:</b></td> <td>August 2006</td> </tr> </table> </p> </td> </tr> <tr style="height: 200px;"> <td><img src="images/book_essentialwpf.jpg" style="width: 130px;"></td> <td style="vertical-align: top;"> <h3>Essential Windows Presentation Foundation</h3> <p>Chris Anderson was one of the chief architects of the next-generation GUI stack, the Windows Presentation Framework (WPF), which is the subject of this book. Chris's insights shine a light from the internals of WPF to those standing at the entrance, guiding you through the concepts that form the foundation of his creation.</p> <p><table> <tr> <td style="width: 100px;"><b>Autor:</b></td> <td>Chris Anderson</td> </tr> <tr> <td style="width: 100px;"><b>ISBN:</b></td> <td>978-0321374479</td> </tr> <tr> <td style="width: 100px;"><b>Published:</b></td> <td>April 2007</td> </tr> </table> </p> </td> </tr> <tr style="height: 200px;"> <td><img src="images/book_foundations.jpg" style="width: 130px;"></td> <td style="vertical-align: top;"> <h3>Foundations of WPF: An Introduction to Windows Presentation Foundation </h3> <p>Foundations of WPF: An Introduction to Windows Presentation Foundation teaches you everything you need to get started with the technology, and is broken into three parts. The first introduces and contextualizes the WPF technology; the second part dives deeper into the facets of the technology that are of immediate and valuable use in developing applications; the last part offers you the real-world perspective you need to be productive in the community and customer base.</p> <p><table> <tr> <td style="width: 100px;"><b>Autor:</b></td> <td>Laurence Moroney</td> </tr> <tr> <td style="width: 100px;"><b>ISBN:</b></td> <td>978-1590597606</td> </tr> <tr> <td style="width: 100px;"><b>Published:</b></td> <td>November 2006</td> </tr> </table> </p> </td> </tr> <tr style="height: 200px;"> <td><img src="images/book_prowpf.jpg" style="width: 130px;"></td> <td style="vertical-align: top;"> <h3>Pro WPF in C# 2008</h3> <p>This book explains how WPF works from the ground up. It follows on from the author s previous and highly successful books, covering Windows Forms (WPF's predecessor technology) and earlier versions of WPF. It is a one-stop shop in Apress proven Pro style that leaves readers with a deep understanding of the technology and able to take the concepts away and apply them for themselves.</p> <p><table> <tr> <td style="width: 100px;"><b>Autor:</b></td> <td>Matthew McDonald</td> </tr> <tr> <td style="width: 100px;"><b>ISBN:</b></td> <td>978-1590599556</td> </tr> <tr> <td style="width: 100px;"><b>Published:</b></td> <td>Februar 2008</td> </tr> </table> </p> </td> </tr> <tr style="height: 200px;"> <td><img src="images/book_programming_wpf.jpg" style="width: 130px;"></td> <td style="vertical-align: top;"> <h3>Foundations of WPF: An Introduction to Windows Presentation Foundation </h3> <p>If you want to build applications that take full advantage of Windows Vista's new user interface capabilities, you need to learn Microsoft's Windows Presentation Foundation (WPF). This new edition, fully updated for the official release of .NET 3.0, is designed to get you up to speed on this technology quickly.</p> <p><table> <tr> <td style="width: 100px;"><b>Autor:</b></td> <td>Chris Sells and Ian Griffiths</td> </tr> <tr> <td style="width: 100px;"><b>ISBN:</b></td> <td>978-0596510374</td> </tr> <tr> <td style="width: 100px;"><b>Published:</b></td> <td>September 2007</td> </tr> </table> </p> </td> </tr> </table> http://www.wpftutorial.net/WPFBools.html Fri, 03 Sep 2010 15:42:54 +02002010-07-21 08:47:41 XAML Editors moc@zuehlke.com (Christian Moser) <h1>XAML Editors</h1> <ul> <li><a href="http://lcbell4.tripod.com/SeniorProject/Senior_Project_XAML_Editor.htm">XAML Editor (by Lary Bell)</a></li> <li><a href="http://blogs.msdn.com/llobo/archive/2006/10/31/XamlPad-Xtended-_2800_with-an-interpreter_2900_.aspx">XAML&#160;PadX (extended)</a></li> <li><a href="http://shevaspace.spaces.live.com/blog/cns!FD9A0F1F8DD06954!505.entry">XAML Hack</a></li> <li><a href="http://www.codeplex.com/Kaxaml">KaXaml</a></li> </ul> http://www.wpftutorial.net/XAMLEditors.html Fri, 03 Sep 2010 15:42:54 +02002010-07-15 15:45:08 Inline Images in a FlowDocument moc@zuehlke.com (Christian Moser) <h1>How to inline Images in a FlowDocument</h1> <h2>Introduction</h2> <p>Sometimes you want to deploy, share or load just a plain XAML file containing a FlowDocument. Then you want to put in some inline images. HTML provides a functionality to express the image as Base64 encoded CDATA section, but WPF does not have such a functionality.<br/><br/> I found a solution how to do this, by creating a custom <code>InlineImage</code> element, that does the trick. The following example shows how to do it.</p> <div class="codeblock"><pre class="xaml xaml" style="font-family: courier new,courier,monospace;"> <span style="color: #800000;"><span style="color: #800000;">&lt;FlowDocument<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;InlineImage</span> <span style="color: #ff0000;">Width</span>=<span style="color: #0000ff;">&quot;100&quot;</span> <span style="color: #ff0000;">Height</span>=<span style="color: #0000ff;">&quot;100&quot;</span> <span style="color: #ff0000;">Stretch</span>=<span style="color: #0000ff;">&quot;Fill&quot;</span><span style="color: #800000;">&gt;</span></span> <span style="color: #ff0000;">&lt;![CDATA[iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAB3RJTUUH2AQP</span> <span style="color: #ff0000;"> SFlzAAALEgAACxIB0t1+/AAAAARnQU1BAACxjwv8YQUAAAnOSURBVHjaxVcLcBvVFT1vV</span> <span style="color: #ff0000;"> ki3Hju3GCQnGjkObONQkJkxCSIHQQGnIdEr5TFs+LaGl7RRCSUvDp8nglH4mDGQ6EwZIm=]]&gt;</span> <span style="color: #800000;"><span style="color: #800000;">&lt;/InlineImage<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/FlowDocument<span style="color: #800000;">&gt;</span></span></span> </pre></div> <h2>Implementation of the InlineImage Control</h2> <div class="codeblock"><pre class="csharp csharp" style="font-family: courier new,courier,monospace;"> <span style="color: #0600FF; font-weight: bold;">namespace</span> XamlInlineImageDemo <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#91;</span>assembly<span style="color: #008000;">:</span> XmlnsDefinition<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span>, <span style="color: #666666;">&quot;XamlInlineImageDemo&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#91;</span>ContentProperty<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Base64Source&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #2b91af; font-weight: bold;">class</span> InlineImage <span style="color: #008000;">:</span> BlockUIContainer <span style="color: #000000;">&#123;</span> <span style="color: #008000;">#region DependencyProperty 'Width'</span> <span style="color: #008000;">/// &lt;summary&gt;</span> <span style="color: #008000;">/// Gets or sets the width.</span> <span style="color: #008000;">/// &lt;/summary&gt;</span> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #2b91af; font-weight: bold;">double</span> Width <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #000000;">&#40;</span><span style="color: #2b91af; font-weight: bold;">double</span><span style="color: #000000;">&#41;</span>GetValue<span style="color: #000000;">&#40;</span>WidthProperty<span style="color: #000000;">&#41;</span>; <span style="color: #000000;">&#125;</span> set <span style="color: #000000;">&#123;</span> SetValue<span style="color: #000000;">&#40;</span>WidthProperty, value<span style="color: #000000;">&#41;</span>; <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span> <span style="color: #008000;">/// &lt;summary&gt;</span> <span style="color: #008000;">/// Registers a dependency property to get or set the width</span> <span style="color: #008000;">/// &lt;/summary&gt;</span> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> DependencyProperty WidthProperty <span style="color: #008000;">=</span> DependencyProperty.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Width&quot;</span>, <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">typeof</span></a><span style="color: #000000;">&#40;</span><span style="color: #2b91af; font-weight: bold;">double</span><span style="color: #000000;">&#41;</span>, <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">typeof</span></a><span style="color: #000000;">&#40;</span>InlineImage<span style="color: #000000;">&#41;</span>, <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">new</span></a> FrameworkPropertyMetadata<span style="color: #000000;">&#40;</span><span style="color: #2b91af; font-weight: bold;">Double</span>.<span style="color: #0000FF;">NaN</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; <span style="color: #008000;">#endregion</span> <span style="color: #008000;">#region DependencyProperty 'Height'</span> <span style="color: #008000;">/// &lt;summary&gt;</span> <span style="color: #008000;">/// Gets or sets the height.</span> <span style="color: #008000;">/// &lt;/summary&gt;</span> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #2b91af; font-weight: bold;">double</span> Height <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #000000;">&#40;</span><span style="color: #2b91af; font-weight: bold;">double</span><span style="color: #000000;">&#41;</span>GetValue<span style="color: #000000;">&#40;</span>HeightProperty<span style="color: #000000;">&#41;</span>; <span style="color: #000000;">&#125;</span> set <span style="color: #000000;">&#123;</span> SetValue<span style="color: #000000;">&#40;</span>HeightProperty, value<span style="color: #000000;">&#41;</span>; <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span> <span style="color: #008000;">/// &lt;summary&gt;</span> <span style="color: #008000;">/// Registers a dependency property to get or set the height</span> <span style="color: #008000;">/// &lt;/summary&gt;</span> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> DependencyProperty HeightProperty <span style="color: #008000;">=</span> DependencyProperty.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Height&quot;</span>, <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">typeof</span></a><span style="color: #000000;">&#40;</span><span style="color: #2b91af; font-weight: bold;">double</span><span style="color: #000000;">&#41;</span>, <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">typeof</span></a><span style="color: #000000;">&#40;</span>InlineImage<span style="color: #000000;">&#41;</span>, <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">new</span></a> FrameworkPropertyMetadata<span style="color: #000000;">&#40;</span><span style="color: #2b91af; font-weight: bold;">Double</span>.<span style="color: #0000FF;">NaN</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; <span style="color: #008000;">#endregion</span> <span style="color: #008000;">#region DependencyProperty 'Stretch'</span> <span style="color: #008000;">/// &lt;summary&gt;</span> <span style="color: #008000;">/// Gets or sets the stretch behavior.</span> <span style="color: #008000;">/// &lt;/summary&gt;</span> <span style="color: #0600FF; font-weight: bold;">public</span> Stretch Stretch <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #000000;">&#40;</span>Stretch<span style="color: #000000;">&#41;</span>GetValue<span style="color: #000000;">&#40;</span>StretchProperty<span style="color: #000000;">&#41;</span>; <span style="color: #000000;">&#125;</span> set <span style="color: #000000;">&#123;</span> SetValue<span style="color: #000000;">&#40;</span>StretchProperty, value<span style="color: #000000;">&#41;</span>; <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span> <span style="color: #008000;">/// &lt;summary&gt;</span> <span style="color: #008000;">/// Registers a dependency property to get or set the stretch behavior</span> <span style="color: #008000;">/// &lt;/summary&gt;</span> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> DependencyProperty StretchProperty <span style="color: #008000;">=</span> DependencyProperty.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Stretch&quot;</span>, <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">typeof</span></a><span style="color: #000000;">&#40;</span>Stretch<span style="color: #000000;">&#41;</span>, <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">typeof</span></a><span style="color: #000000;">&#40;</span>InlineImage<span style="color: #000000;">&#41;</span>, <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">new</span></a> FrameworkPropertyMetadata<span style="color: #000000;">&#40;</span>Stretch.<span style="color: #0000FF;">Uniform</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; <span style="color: #008000;">#endregion</span> <span style="color: #008000;">#region DependencyProperty 'StretchDirection'</span> <span style="color: #008000;">/// &lt;summary&gt;</span> <span style="color: #008000;">/// Gets or sets the stretch direction.</span> <span style="color: #008000;">/// &lt;/summary&gt;</span> <span style="color: #0600FF; font-weight: bold;">public</span> StretchDirection StretchDirection <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #000000;">&#40;</span>StretchDirection<span style="color: #000000;">&#41;</span>GetValue<span style="color: #000000;">&#40;</span>StretchDirectionProperty<span style="color: #000000;">&#41;</span>; <span style="color: #000000;">&#125;</span> set <span style="color: #000000;">&#123;</span> SetValue<span style="color: #000000;">&#40;</span>StretchDirectionProperty, value<span style="color: #000000;">&#41;</span>; <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span> <span style="color: #008000;">/// &lt;summary&gt;</span> <span style="color: #008000;">/// Registers a dependency property to get or set the stretch direction</span> <span style="color: #008000;">/// &lt;/summary&gt;</span> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> DependencyProperty StretchDirectionProperty <span style="color: #008000;">=</span> DependencyProperty.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;StretchDirection&quot;</span>, <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">typeof</span></a><span style="color: #000000;">&#40;</span>StretchDirection<span style="color: #000000;">&#41;</span>, <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">typeof</span></a><span style="color: #000000;">&#40;</span>InlineImage<span style="color: #000000;">&#41;</span>, <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">new</span></a> FrameworkPropertyMetadata<span style="color: #000000;">&#40;</span>StretchDirection.<span style="color: #0000FF;">Both</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; <span style="color: #008000;">#endregion</span> <span style="color: #008000;">#region DependencyProperty 'Base64Source'</span> <span style="color: #008000;">/// &lt;summary&gt;</span> <span style="color: #008000;">/// Gets or sets the base64 source.</span> <span style="color: #008000;">/// &lt;/summary&gt;</span> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #2b91af; font-weight: bold;">string</span> Base64Source <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #000000;">&#40;</span><span style="color: #2b91af; font-weight: bold;">string</span><span style="color: #000000;">&#41;</span>GetValue<span style="color: #000000;">&#40;</span>Base64SourceProperty<span style="color: #000000;">&#41;</span>; <span style="color: #000000;">&#125;</span> set <span style="color: #000000;">&#123;</span> SetValue<span style="color: #000000;">&#40;</span>Base64SourceProperty, value<span style="color: #000000;">&#41;</span>; <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span> <span style="color: #008000;">/// &lt;summary&gt;</span> <span style="color: #008000;">/// Registers a dependency property to get or set the base64 source</span> <span style="color: #008000;">/// &lt;/summary&gt;</span> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> DependencyProperty Base64SourceProperty <span style="color: #008000;">=</span> DependencyProperty.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Base64Source&quot;</span>, <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">typeof</span></a><span style="color: #000000;">&#40;</span><span style="color: #2b91af; font-weight: bold;">string</span><span style="color: #000000;">&#41;</span>, <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">typeof</span></a><span style="color: #000000;">&#40;</span>InlineImage<span style="color: #000000;">&#41;</span>, <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">new</span></a> FrameworkPropertyMetadata<span style="color: #000000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">null</span>, OnBase64SourceChanged<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; <span style="color: #008000;">#endregion</span> <span style="color: #008000;">#region Private Members</span> <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">void</span> OnBase64SourceChanged<span style="color: #000000;">&#40;</span>DependencyObject sender, DependencyPropertyChangedEventArgs e<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> var inlineImage <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>InlineImage<span style="color: #000000;">&#41;</span>sender; var stream <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">new</span></a> MemoryStream<span style="color: #000000;">&#40;</span>Convert.<span style="color: #0000FF;">FromBase64String</span><span style="color: #000000;">&#40;</span>inlineImage.<span style="color: #0000FF;">Base64Source</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; var bitmapImage <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">new</span></a> BitmapImage<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; bitmapImage.<span style="color: #0000FF;">BeginInit</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; bitmapImage.<span style="color: #0000FF;">StreamSource</span> <span style="color: #008000;">=</span> stream; bitmapImage.<span style="color: #0000FF;">EndInit</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; var image <span style="color: #008000;">=</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">new</span></a> Image <span style="color: #000000;">&#123;</span> Source <span style="color: #008000;">=</span> bitmapImage, Stretch <span style="color: #008000;">=</span> inlineImage.<span style="color: #0000FF;">Stretch</span>, StretchDirection <span style="color: #008000;">=</span> inlineImage.<span style="color: #0000FF;">StretchDirection</span>, <span style="color: #000000;">&#125;</span>; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #2b91af; font-weight: bold;">double</span>.<span style="color: #0000FF;">IsNaN</span><span style="color: #000000;">&#40;</span>inlineImage.<span style="color: #0000FF;">Width</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> image.<span style="color: #0000FF;">Width</span> <span style="color: #008000;">=</span> inlineImage.<span style="color: #0000FF;">Width</span>; <span style="color: #000000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #2b91af; font-weight: bold;">double</span>.<span style="color: #0000FF;">IsNaN</span><span style="color: #000000;">&#40;</span>inlineImage.<span style="color: #0000FF;">Height</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> image.<span style="color: #0000FF;">Height</span> <span style="color: #008000;">=</span> inlineImage.<span style="color: #0000FF;">Height</span>; <span style="color: #000000;">&#125;</span> inlineImage.<span style="color: #0000FF;">Child</span> <span style="color: #008000;">=</span> image; <span style="color: #000000;">&#125;</span> <span style="color: #008000;">#endregion</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span> </pre></div> http://www.wpftutorial.net/InlineImagesXaml.html Fri, 03 Sep 2010 15:42:54 +02002010-07-15 08:22:33 Documents moc@zuehlke.com (Christian Moser) <h1>Documents</h1> [sublist] http://www.wpftutorial.net/Documents.html Fri, 03 Sep 2010 15:42:54 +02002010-07-14 16:50:08 FlowDocuments moc@zuehlke.com (Christian Moser) <h1>How to use FlowDocuments</h1> <h2>How to save or load a FlowDocument embedded images</h2> <p>There is a way to save and load a flow document including all embedded images by putting them into a <code>XamlPackage</code>. The following sample shows how to do this: <div class="codeblock"><pre class="csharp csharp" style="font-family: courier new,courier,monospace;"> <span style="color: #008000;">// Save </span> var source <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">new</span></a> FlowDocument<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; var range <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">new</span></a> TextRange<span style="color: #000000;">&#40;</span>source.<span style="color: #0000FF;">ContentStart</span>, source.<span style="color: #0000FF;">ContentEnd</span><span style="color: #000000;">&#41;</span>; <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #000000;">&#40;</span>var stream <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;output.pak&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> range.<span style="color: #0000FF;">Save</span><span style="color: #000000;">&#40;</span>stream, DataFormats.<span style="color: #0000FF;">XamlPackage</span><span style="color: #000000;">&#41;</span>; <span style="color: #000000;">&#125;</span> </pre></div> <div class="codeblock"><pre class="csharp csharp" style="font-family: courier new,courier,monospace;"> <span style="color: #008000;">// Load</span> <span style="color: #0600FF; font-weight: bold;">using</span><span style="color: #000000;">&#40;</span>var stream <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">OpenRead</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;output.pak&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> var target <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">new</span></a> FlowDocument<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; var range <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">new</span></a> TextRange<span style="color: #000000;">&#40;</span>target.<span style="color: #0000FF;">ContentStart</span>, target.<span style="color: #0000FF;">ContentEnd</span><span style="color: #000000;">&#41;</span>; range.<span style="color: #0000FF;">Load</span><span style="color: #000000;">&#40;</span>stream, DataFormats.<span style="color: #0000FF;">XamlPackage</span><span style="color: #000000;">&#41;</span>; <span style="color: #000000;">&#125;</span> </pre></div> <h2>How to navigate by hyperlinks</h2> <p>If you have a FlowDocument that contains Hyperlinks and you want to perform some action when the user clicks on the link, you can use hook up to the RoutedEvent <code>Hyperlink.RequestNavigateEvent</code>. The following code snippet shows you how to do it:</p> <div class="codeblock"><pre class="xaml xaml" style="font-family: courier new,courier,monospace;"> <span style="color: #800000;"><span style="color: #800000;">&lt;Window</span> <span style="color: #ff0000;">x:Class</span>=<span style="color: #0000ff;">&quot;HyperlinksInFlowDocument.Window1&quot;</span></span> <span style="color: #800000;"> <span style="color: #ff0000;">xmlns</span>=<span style="color: #0000ff;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></span> <span style="color: #800000;"> <span style="color: #ff0000;">xmlns:x</span>=<span style="color: #0000ff;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span></span> <span style="color: #800000;"> <span style="color: #ff0000;">Title</span>=<span style="color: #0000ff;">&quot;Window1&quot;</span> <span style="color: #ff0000;">Height</span>=<span style="color: #0000ff;">&quot;300&quot;</span> <span style="color: #ff0000;">Width</span>=<span style="color: #0000ff;">&quot;300&quot;</span><span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;Grid<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;FlowDocumentReader<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;FlowDocument<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;Paragraph<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;Hyperlink</span> <span style="color: #ff0000;">NavigateUri</span>=<span style="color: #0000ff;">&quot;http://www.google.ch&quot;</span><span style="color: #800000;">&gt;</span></span>Google<span style="color: #800000;"><span style="color: #800000;">&lt;/Hyperlink<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/Paragraph<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/FlowDocument<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/FlowDocumentReader<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/Grid<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/Window<span style="color: #800000;">&gt;</span></span></span> </pre></div> <div class="codeblock"><pre class="csharp csharp" style="font-family: courier new,courier,monospace;"> <span style="color: #0600FF; font-weight: bold;">public</span> Window1<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> InitializeComponent<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; AddHandler<span style="color: #000000;">&#40;</span>Hyperlink.<span style="color: #0000FF;">RequestNavigateEvent</span>, <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">new</span></a> RoutedEventHandler<span style="color: #000000;">&#40;</span>OnNavigationRequest<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; <span style="color: #000000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">void</span> OnNavigationRequest<span style="color: #000000;">&#40;</span><span style="color: #2b91af; font-weight: bold;">object</span> sender, RoutedEventArgs e<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> var source <span style="color: #008000;">=</span> e.<span style="color: #0000FF;">OriginalSource</span> <span style="color: #0600FF; font-weight: bold;">as</span> Hyperlink; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>source <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span> Process.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span>source.<span style="color: #0000FF;">NavigateUri</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; <span style="color: #000000;">&#125;</span> </pre></div> http://www.wpftutorial.net/FlowDocuments.html Fri, 03 Sep 2010 15:42:54 +02002010-07-08 17:45:05 Images moc@zuehlke.com (Christian Moser) <h1>Images in WPF</h1> <h2>How to create a Thumbnail of an Image</h2> <div class="codeblock"><pre class="csharp csharp" style="font-family: courier new,courier,monospace;"> <span style="color: #0600FF; font-weight: bold;">private</span> ImageSource GetThumbnail<span style="color: #000000;">&#40;</span> <span style="color: #2b91af; font-weight: bold;">string</span> fileName <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #2b91af; font-weight: bold;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> buffer <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">ReadAllBytes</span><span style="color: #000000;">&#40;</span>fileName<span style="color: #000000;">&#41;</span>; MemoryStream memoryStream <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">new</span></a> MemoryStream<span style="color: #000000;">&#40;</span>buffer<span style="color: #000000;">&#41;</span>; BitmapImage bitmap <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">new</span></a> BitmapImage<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; bitmap.<span style="color: #0000FF;">BeginInit</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; bitmap.<span style="color: #0000FF;">DecodePixelWidth</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">80</span>; bitmap.<span style="color: #0000FF;">DecodePixelHeight</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">60</span>; bitmap.<span style="color: #0000FF;">StreamSource</span> <span style="color: #008000;">=</span> memoryStream; bitmap.<span style="color: #0000FF;">EndInit</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; bitmap.<span style="color: #0000FF;">Freeze</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; <span style="color: #0600FF; font-weight: bold;">return</span> bitmap; <span style="color: #000000;">&#125;</span> </pre></div><h2>How to automatically crop an image</h2> <p>The following method allows you to automatically crop an image to it's content.</p> <div class="codeblock"><pre class="csharp csharp" style="font-family: courier new,courier,monospace;"> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> ImageSource AutoCropBitmap<span style="color: #000000;">&#40;</span>BitmapSource source<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>source <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">throw</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">new</span></a> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;source&quot;</span><span style="color: #000000;">&#41;</span>; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>source.<span style="color: #0000FF;">Format</span> <span style="color: #008000;">!=</span> PixelFormats.<span style="color: #0000FF;">Bgra32</span><span style="color: #000000;">&#41;</span> source <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">new</span></a> FormatConvertedBitmap<span style="color: #000000;">&#40;</span>source, PixelFormats.<span style="color: #0000FF;">Bgra32</span>, <span style="color: #0600FF; font-weight: bold;">null</span>, 0<span style="color: #000000;">&#41;</span>; <span style="color: #2b91af; font-weight: bold;">int</span> width <span style="color: #008000;">=</span> source.<span style="color: #0000FF;">PixelWidth</span>; <span style="color: #2b91af; font-weight: bold;">int</span> height <span style="color: #008000;">=</span> source.<span style="color: #0000FF;">PixelHeight</span>; <span style="color: #2b91af; font-weight: bold;">int</span> bytesPerPixel <span style="color: #008000;">=</span> source.<span style="color: #0000FF;">Format</span>.<span style="color: #0000FF;">BitsPerPixel</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">8</span>; <span style="color: #2b91af; font-weight: bold;">int</span> stride <span style="color: #008000;">=</span> width <span style="color: #008000;">*</span> bytesPerPixel; var pixelBuffer <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">new</span></a> <span style="color: #2b91af; font-weight: bold;">byte</span><span style="color: #000000;">&#91;</span>height <span style="color: #008000;">*</span> stride<span style="color: #000000;">&#93;</span>; source.<span style="color: #0000FF;">CopyPixels</span><span style="color: #000000;">&#40;</span>pixelBuffer, stride, 0<span style="color: #000000;">&#41;</span>; <span style="color: #2b91af; font-weight: bold;">int</span> cropTop <span style="color: #008000;">=</span> height, cropBottom <span style="color: #008000;">=</span> 0, cropLeft <span style="color: #008000;">=</span> width, cropRight <span style="color: #008000;">=</span> 0; <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #2b91af; font-weight: bold;">int</span> y <span style="color: #008000;">=</span> 0; y <span style="color: #008000;">&lt;</span> height; y<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #2b91af; font-weight: bold;">int</span> x <span style="color: #008000;">=</span> 0; x <span style="color: #008000;">&lt;</span> width; x<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #2b91af; font-weight: bold;">int</span> offset <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>y <span style="color: #008000;">*</span> stride <span style="color: #008000;">+</span> x <span style="color: #008000;">*</span> bytesPerPixel<span style="color: #000000;">&#41;</span>; <span style="color: #2b91af; font-weight: bold;">byte</span> blue <span style="color: #008000;">=</span> pixelBuffer<span style="color: #000000;">&#91;</span>offset<span style="color: #000000;">&#93;</span>; <span style="color: #2b91af; font-weight: bold;">byte</span> green <span style="color: #008000;">=</span> pixelBuffer<span style="color: #000000;">&#91;</span>offset <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span>; <span style="color: #2b91af; font-weight: bold;">byte</span> red <span style="color: #008000;">=</span> pixelBuffer<span style="color: #000000;">&#91;</span>offset <span style="color: #008000;">+</span> <span style="color: #FF0000;">2</span><span style="color: #000000;">&#93;</span>; <span style="color: #2b91af; font-weight: bold;">byte</span> alpha <span style="color: #008000;">=</span> pixelBuffer<span style="color: #000000;">&#91;</span>offset <span style="color: #008000;">+</span> <span style="color: #FF0000;">3</span><span style="color: #000000;">&#93;</span>; <span style="color: #008000;">//TODO: Define a threshold when a pixel has a content</span> <span style="color: #2b91af; font-weight: bold;">bool</span> hasContent <span style="color: #008000;">=</span> alpha <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">10</span>; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>hasContent<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> cropLeft <span style="color: #008000;">=</span> Math.<span style="color: #0000FF;">Min</span><span style="color: #000000;">&#40;</span>x, cropLeft<span style="color: #000000;">&#41;</span>; cropRight <span style="color: #008000;">=</span> Math.<span style="color: #0000FF;">Max</span><span style="color: #000000;">&#40;</span>x, cropRight<span style="color: #000000;">&#41;</span>; cropTop <span style="color: #008000;">=</span> Math.<span style="color: #0000FF;">Min</span><span style="color: #000000;">&#40;</span>y, cropTop<span style="color: #000000;">&#41;</span>; cropBottom <span style="color: #008000;">=</span> Math.<span style="color: #0000FF;">Max</span><span style="color: #000000;">&#40;</span>y, cropBottom<span style="color: #000000;">&#41;</span>; <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">new</span></a> CroppedBitmap<span style="color: #000000;">&#40;</span>source, <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">new</span></a> Int32Rect<span style="color: #000000;">&#40;</span>cropLeft, cropTop, cropRight <span style="color: #008000;">-</span> cropLeft, cropBottom <span style="color: #008000;">-</span> cropTop<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; <span style="color: #000000;">&#125;</span> </pre></div> <h2>How to flip an image horizontally or vertically</h2> <p>The easiest way to flip an image in WPF is to use a <code>TransformedBitmap</code>. It's a wrapper around a <code>BitmapImage</code> that takes a generic <code>Transform</code> object.</p> <div class="codeblock"><pre class="csharp csharp" style="font-family: courier new,courier,monospace;"> var flippedImage <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">new</span></a> TransformedBitmap<span style="color: #000000;">&#40;</span>originalImage, <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">new</span></a> ScaleTransform<span style="color: #000000;">&#40;</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span>,<span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; </pre></div> http://www.wpftutorial.net/Images.html Fri, 03 Sep 2010 15:42:54 +02002010-07-08 13:47:32 Interoperability moc@zuehlke.com (Christian Moser) <h2>How to get the Handle of a WPF Window</h2> <div class="codeblock"><pre class="csharp csharp" style="font-family: courier new,courier,monospace;"> IntPtr windowHandle <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">new</span></a> WindowInteropHelper<span style="color: #000000;">&#40;</span> Application.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">MainWindow</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Handle</span>; </pre></div> <h2>How to manually set the rendering mode to software</h2> <div class="codeblock"><pre class="csharp csharp" style="font-family: courier new,courier,monospace;"> var source <span style="color: #008000;">=</span> PresentationSource.<span style="color: #0000FF;">FromVisual</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #000000;">&#41;</span>; var hwndTarget <span style="color: #008000;">=</span> source.<span style="color: #0000FF;">CompositionTarget</span> <span style="color: #0600FF; font-weight: bold;">as</span> HwndTarget; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>hwndTarget <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> hwndTarget.<span style="color: #0000FF;">RenderMode</span> <span style="color: #008000;">=</span> RenderMode.<span style="color: #0000FF;">SoftwareOnly</span>; <span style="color: #000000;">&#125;</span> </pre></div> http://www.wpftutorial.net/Interoperability.html Fri, 03 Sep 2010 15:42:54 +02002010-07-06 16:40:47 MergedDictionaries moc@zuehlke.com (Christian Moser) <h1>Merged ResourceDictionaries</h1> <h2>Problems loading merged resource dictionaries in .NET 4.0</h2> <p>I experienced a problem, when I am trying to merge resource dictionaries at app-level. As soon as I changed the target framework from 3.5 to 4.0 they don't get loaded anymore. It seems as if they have changed something in the behavior of MergedResourceDictionaries in .NET 4.0.<br/><br/> I found a way how to fix this problem. Just add a dummy default style in the resource dictionary where you merge all resources together. See the following example:</p> <div class="codeblock"><pre class="xaml xaml" style="font-family: courier new,courier,monospace;"> <span style="color: #800000;"><span style="color: #800000;">&lt;Application</span>.Resources<span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;ResourceDictionary<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;ResourceDictionary</span>.MergedDictionaries<span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;ResourceDictionary</span> <span style="color: #ff0000;">Source</span>=<span style="color: #0000ff;">&quot;Styles/DefaultStyle.xaml&quot;</span><span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/ResourceDictionary</span>.MergedDictionaries<span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/ResourceDictionary<span style="color: #800000;">&gt;</span></span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/Application</span>.Resources<span style="color: #800000;">&gt;</span></span> </pre></div> <div class="codeblock"><pre class="xaml xaml" style="font-family: courier new,courier,monospace;"> <span style="color: #800000;"><span style="color: #800000;">&lt;ResourceDictionary</span> </span> <span style="color: #800000;"> <span style="color: #ff0000;">xmlns</span>=<span style="color: #0000ff;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></span> <span style="color: #800000;"> <span style="color: #ff0000;">xmlns:x</span>=<span style="color: #0000ff;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span><span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;ResourceDictionary</span>.MergedDictionaries<span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;ResourceDictionary</span> <span style="color: #ff0000;">Source</span>=<span style="color: #0000ff;">&quot;DefaultStyle/Button.xaml&quot;</span><span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/ResourceDictionary</span>.MergedDictionaries<span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;Style</span> <span style="color: #ff0000;">TargetType</span>=<span style="color: #0000ff;">&quot;Control&quot;</span> <span style="color: #ff0000;">BasedOn</span>=<span style="color: #0000ff;">&quot;{StaticResource {x:Type Control}}&quot;</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/ResourceDictionary<span style="color: #800000;">&gt;</span></span></span> </pre></div> <h2>Improve the performance of merged ResourceDictionaries</h2> <p>Each time a control references a <code>ResourceDictionary</code> XAML creates a new instance of it. So if you have a custom control library with 30 controls in it and each control references a common dictionary you create 30 identical resource dictionaries!</p> <div class="codeblock"><pre class="xaml xaml" style="font-family: courier new,courier,monospace;"> <span style="color: #800000;"><span style="color: #800000;">&lt;ResourceDictionary</span>.MergedDictionaries<span style="color: #800000;">&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;SharedResourceDictionary</span> <span style="color: #ff0000;">Source</span>=<span style="color: #0000ff;">&quot;/MyControlLibrary;component/Themes/Brushes.xaml&quot;</span> <span style="color: #800000;">/&gt;</span></span> <span style="color: #800000;"><span style="color: #800000;">&lt;/ResourceDictionary</span>.MergedDictionaries<span style="color: #800000;">&gt;</span></span> </pre></div> <p>To get rid of this problem, I created the <code>SharedResourceDictionary</code>. You can use it the same way as a conventional ResourceDictionary. The only suptile difference is, that if it is instanced multiple times, the resources are loaded only once.</p> <div class="codeblock"><pre class="csharp csharp" style="font-family: courier new,courier,monospace;"> <span style="color: #000000;">&#91;</span>assembly<span style="color: #008000;">:</span> XmlnsDefinition<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span>, <span style="color: #666666;">&quot;WPFTutorial.Utils&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">/// &lt;summary&gt;</span> <span style="color: #008000;">/// The shared resource dictionary is a specialized resource dictionary</span> <span style="color: #008000;">/// that loads it content only once. If a second instance with the same source</span> <span style="color: #008000;">/// is created, it only merges the resources from the cache.</span> <span style="color: #008000;">/// &lt;/summary&gt;</span> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #2b91af; font-weight: bold;">class</span> SharedResourceDictionary <span style="color: #008000;">:</span> ResourceDictionary <span style="color: #000000;">&#123;</span> <span style="color: #008000;">/// &lt;summary&gt;</span> <span style="color: #008000;">/// Internal cache of loaded dictionaries </span> <span style="color: #008000;">/// &lt;/summary&gt;</span> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> Dictionary<span style="color: #008000;">&lt;</span>Uri, ResourceDictionary<span style="color: #008000;">&gt;</span> _sharedDictionaries <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">new</span></a> Dictionary<span style="color: #008000;">&lt;</span>Uri, ResourceDictionary<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; <span style="color: #008000;">/// &lt;summary&gt;</span> <span style="color: #008000;">/// Local member of the source uri</span> <span style="color: #008000;">/// &lt;/summary&gt;</span> <span style="color: #0600FF; font-weight: bold;">private</span> Uri _sourceUri; <span style="color: #008000;">/// &lt;summary&gt;</span> <span style="color: #008000;">/// Gets or sets the uniform resource identifier (URI) to load resources from.</span> <span style="color: #008000;">/// &lt;/summary&gt;</span> <span style="color: #0600FF; font-weight: bold;">public</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0000ff; font-weight: bold;">new</span></a> Uri Source <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _sourceUri; <span style="color: #000000;">&#125;</span> set <span style="color: #000000;">&#123;</span> _sourceUri <span style="color: #008000;">=</span> value; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>_sharedDictionaries.<span style="color: #0000FF;">ContainsKey</span><span style="color: #000000;">&#40;</span>value<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #008000;">// If the dictionary is not yet loaded, load it by setting</span> <span style="color: #008000;">// the source of the base class</span> <span style="color: #0600FF; font-weight: bold;">base</span>.<span style="color: #0000FF;">Source</span> <span style="color: #008000;">=</span> value; <span style="color: #008000;">// add it to the cache</span> _sharedDictionaries.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>value, <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #000000;">&#41;</span>; <span style="color: #000000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span> <span style="color: #008000;">// If the dictionary is already loaded, get it from the cache</span> MergedDictionaries.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>_sharedDictionaries<span style="color: #000000;">&#91;</span>value<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>; <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span> </pre></div> http://www.wpftutorial.net/MergedDictionaryPerformance.html Fri, 03 Sep 2010 15:42:54 +02002010-07-02 13:29:38