WPF and XAML - HOWTO: Difference between revisions

From Yggenyk
Jump to navigation Jump to search
No edit summary
 
(19 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[Category:English pages]]
[[Category:English pages]]
[[Category:Software development]]
[[Category:Software development]]
=Tools=
Show Me The Template is a great tool for looking at default control templates
* [http://www.sellsbrothers.com/Posts/Details/2091 Show Me The Template]
[[File:ShowMeTheTemplate.png]]
=Visual Studio Designer=
==Troubleshooting WPF and Silverlight Designer Load Failures==
http://msdn.microsoft.com/en-us/library/ff356886(VS.95).aspx
==Debugging a Designer Load Failure==
To debug a designer load failure
# Open a second instance of Visual Studio.
# From the Debug menu, choose Attach to Process.<br>The Attach To Process dialog box opens.
#In the Available Processes section, select the instance of devenv.exe that is loading your XAML and click Attach.<br>The Visual Studio debugger attaches to the selected devenv.exe instance.
#From the Debug menu, choose Exceptions.<br>The Exceptions dialog box opens.
#In the Break when exception is list, find the Common Language Runtime Exceptions name, and then check the Thrown check box.
#Click OK.
#In the first Visual Studio instance that you are debugging, reload the XAML file.<br>The second instance of Visual Studio opens and displays the load exception.
Note: You can click Stop Debugging in the second instance of Visual Studio and the first instance keeps running
=Formatting=
==Standard Date and Time Format Strings==
* [http://msdn.microsoft.com/en-us/library/az4se3k1.aspx Standard Date and Time Format Strings]
==Standard Numeric Format Strings==
* [http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.100).aspx Standard Numeric Format Strings]
=XAML=
==Label without accelerator==
==Label without accelerator==
<source lang="csharp">
<source lang="csharp">
<Label>
  <Label>
  <ContentPresenter RecognizesAccessKey="False" Content="_123">
      <ContentPresenter RecognizesAccessKey="False" Content="_123">
  </ContentPresenter>
      </ContentPresenter>
</Label>
  </Label>
</source>
</source>
==Virtualizing an ItemsControl==
<source lang="csharp">
    <Style x:Key="ItemsControlVirtualisedStyle"
          TargetType="ItemsControl">
        <Setter Property="VirtualizingStackPanel.IsVirtualizing"
                Value="True" />
        <Setter Property="VirtualizingPanel.IsVirtualizingWhenGrouping"
                Value="True" />
        <Setter Property="VirtualizingPanel.ScrollUnit"
                Value="Pixel" />
        <Setter Property="ScrollViewer.CanContentScroll"
                Value="True" />
        <Setter Property="ScrollViewer.PanningMode"
                Value="VerticalOnly" />
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel />
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ItemsControl">
                    <Border BorderThickness="{TemplateBinding Border.BorderThickness}"
                            Padding="{TemplateBinding Control.Padding}"
                            BorderBrush="{TemplateBinding Border.BorderBrush}"
                            Background="{TemplateBinding Panel.Background}"
                            SnapsToDevicePixels="True">
                        <ScrollViewer Padding="{TemplateBinding Control.Padding}"
                                      HorizontalScrollBarVisibility="Disabled"
                                      Focusable="False">
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</source>
You have to set ScrollViewer.CanContentScroll="True" otherwise you will lose virtualization.
You could set VirtualizingStackPanel.VirtualizationMode="Recycling". This will reduce the numbers of times item containers are created.
Read more on UI virtualization here [http://www.zagstudio.com/blog/497#.VBkxTjySx8E UI Virtualization | Zag Studio].
Raed more about WPF controls and performance here [http://msdn.microsoft.com/en-us/library/cc716879.aspx MSDN - Optimizing Performance: Controls]
==AffectsMeasure or AffectsArrange==
* [http://stackoverflow.com/questions/14414083/affectsmeasure-or-affectsarrange AffectsMeasure or AffectsArrange]
==Slider==
* [http://codingsense.wordpress.com/2010/02/01/customize-a-slider-in-wpf-step-by-step-tutorial/ Customize a slider in WPF, Step by Step Tutoria]

Latest revision as of 12:51, 15 October 2014

Tools

Show Me The Template is a great tool for looking at default control templates

ShowMeTheTemplate.png

Visual Studio Designer

Troubleshooting WPF and Silverlight Designer Load Failures

http://msdn.microsoft.com/en-us/library/ff356886(VS.95).aspx

Debugging a Designer Load Failure

To debug a designer load failure

  1. Open a second instance of Visual Studio.
  2. From the Debug menu, choose Attach to Process.
    The Attach To Process dialog box opens.
  3. In the Available Processes section, select the instance of devenv.exe that is loading your XAML and click Attach.
    The Visual Studio debugger attaches to the selected devenv.exe instance.
  4. From the Debug menu, choose Exceptions.
    The Exceptions dialog box opens.
  5. In the Break when exception is list, find the Common Language Runtime Exceptions name, and then check the Thrown check box.
  6. Click OK.
  7. In the first Visual Studio instance that you are debugging, reload the XAML file.
    The second instance of Visual Studio opens and displays the load exception.

Note: You can click Stop Debugging in the second instance of Visual Studio and the first instance keeps running

Formatting

Standard Date and Time Format Strings

Standard Numeric Format Strings

XAML

Label without accelerator

<source lang="csharp">

  <Label>
     <ContentPresenter RecognizesAccessKey="False" Content="_123">
     </ContentPresenter>
  </Label>

</source>

Virtualizing an ItemsControl

<source lang="csharp">

   <Style x:Key="ItemsControlVirtualisedStyle"
          TargetType="ItemsControl">
       <Setter Property="VirtualizingStackPanel.IsVirtualizing"
               Value="True" />
       <Setter Property="VirtualizingPanel.IsVirtualizingWhenGrouping"
               Value="True" />
       <Setter Property="VirtualizingPanel.ScrollUnit"
               Value="Pixel" />
       <Setter Property="ScrollViewer.CanContentScroll"
               Value="True" />
       <Setter Property="ScrollViewer.PanningMode"
               Value="VerticalOnly" />
       <Setter Property="ItemsPanel">
           <Setter.Value>
               <ItemsPanelTemplate>
                   <VirtualizingStackPanel />
               </ItemsPanelTemplate>
           </Setter.Value>
       </Setter>
       <Setter Property="Template">
           <Setter.Value>
               <ControlTemplate TargetType="ItemsControl">
                   <Border BorderThickness="{TemplateBinding Border.BorderThickness}"
                           Padding="{TemplateBinding Control.Padding}"
                           BorderBrush="{TemplateBinding Border.BorderBrush}"
                           Background="{TemplateBinding Panel.Background}"
                           SnapsToDevicePixels="True">
                       <ScrollViewer Padding="{TemplateBinding Control.Padding}"
                                     HorizontalScrollBarVisibility="Disabled"
                                     Focusable="False">
                           <ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                       </ScrollViewer>
                   </Border>
               </ControlTemplate>
           </Setter.Value>
       </Setter>
   </Style>

</source>

You have to set ScrollViewer.CanContentScroll="True" otherwise you will lose virtualization.

You could set VirtualizingStackPanel.VirtualizationMode="Recycling". This will reduce the numbers of times item containers are created.

Read more on UI virtualization here UI Virtualization | Zag Studio.

Raed more about WPF controls and performance here MSDN - Optimizing Performance: Controls

AffectsMeasure or AffectsArrange

Slider

id=siteTree