User Tools

Site Tools


technical_notes:public:wpf-xaml

Parent

WPF - Xaml - Notes

Package Uri

To get resources, you can adress them through uri in wpf.

Sources :

ResourceDictionary

Resource Dictionary helps to gather informations about presentation rules as css. To declare it in a separate file, you can do that :

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="../Styles/DatePicker.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

As you can see above, we use relative path. Sometimes, it's better to do the reference using the path relative to components.

...
<!-- Refer to embedded resources -->
<ResourceDictionary Source="ResourceDll;component/Styles/DatePicker.xaml" />
...

Sources :

Attached Properties

Attached properties follows precedence rules when you affect them 1)

Sources :

Markup Extension

Resources

Storyboard

Sources :

Visual states

In WPF, we can refer visual states to controls or even windows. To complete this approach we can define visual transition.

Visual states come from silverlight that does not support trigger.

<UserControl x:Class="MainView"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:vw="clr-namespace:Desktop.Shell.Views">
  <Grid>
    <VisualStateManager.VisualStateGroups>
      <VisualStateGroup x:Name="MainVisualStateGroup">
        <VisualState x:Name="Error"/>
        <VisualState x:Name="InvalidUnfocused"/>
     </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
  </Grid>
</UserControl>

Sources :

Adorner

  • AdornedElementPlaceholder

Sources :

Binding

Each binding typically has these four components:

  • a binding target object
  • a target property
  • a binding source
  • a path to the value in the binding source to use.

How to do multibinding for a converter ?

<Button>
	<Button.IsEnabled>
		<MultiBinding Converter="{StaticResource HasAutorizationConverter}" ConverterParameter="ETA">
			<MultiBinding.Bindings>
				<Binding Path="Context1" />
				<Binding Path="Context2" />
			</MultiBinding.Bindings>
		</MultiBinding>
	</Button.IsEnabled>
</Button>

RelativeSource

Triggers

Triggers define a list of setters that are executed if the specified condition is fulfilled. WPF knows three diferent types of triggers.

Setting trigger

To set style trigger directly in component, use this code :

<wpf:ChildWindow.Style>
	<Style TargetType="wpf:ChildWindow">
		<Style.Triggers>
			<DataTrigger Binding="{Binding Path=Type}" Value="{x:Static type:Type.ERREUR}">
				<Setter Property="Background" Value="Yellow" />
			</DataTrigger>
		</Style.Triggers>
	</Style>
</wpf:ChildWindow.Style>

Property trigger

<Style x:Key="styleWithTrigger" TargetType="Rectangle">
    <Setter Property="Fill" Value="LightGreen" />
    <Style.Triggers>
       <Trigger Property="IsEnabled" Value="false">
          <Setter Property="Background" Value="#EEEEEE" />
       </Trigger>
    </Style.Triggers>
</Style>
 
<Rectangle Style="{StaticResource styleWithTrigger}"></Rectangle>

MultiTrigger

<Style.Triggers>
  <MultiTrigger>
    <MultiTrigger.Conditions>
      <Condition Property="HasItems" Value="false" />
      <Condition Property="Width" Value="Auto" />
    </MultiTrigger.Conditions>
    <Setter Property="MinWidth" Value="120"/>
  </MultiTrigger>
</Style.Triggers>

Sources :

,
technical_notes/public/wpf-xaml.txt · Last modified: 2014/01/03 17:01 by Fabien Arcellier