Overblog Suivre ce blog
Editer l'article Administration Créer mon blog

Présentation

  • : Romagny13 - Du .NET,du pur .NET
  • Romagny13 - Du .NET,du pur .NET
  • : Cycle de progression Faire (quelque chose qui marche) -> comprendre ce que l’on fait/comment cela marche -> pousser plus loin les notions
  • Contact

Recherche

Articles Récents

1 mars 2014 6 01 /03 /mars /2014 12:34

1 – Ajout d’un dictionnaire de  ressources (Styles,DataTemplates,Brushes,Converters,Fonts,etc.)Exemple : Styles.xaml (ajouté dans un répertoire Ressources)



2 – Ajout d’un style
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
 
    <Style x:Key="TextBoxInError"  TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip" Value="{Binding (Validation.Errors)[0].ErrorContent, RelativeSource={RelativeSource Self}}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
 
</ResourceDictionary>

 

Remarque : si je n’ajoute pas de clé au Style celui-ci sera automatiquement appliqué à toutes les textBox de l’application
  
3- App.xaml
<Application x:Class="MVVMMediatorDemo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
       
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/DataTemplates/DataTemplates.xaml" />
                 <ResourceDictionary Source="/Ressources/Styles.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
       
    </Application.Resources>
</Application>

 

4- Utilisation du style
<TextBox Text="{Binding CurrentClient.Name, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" Style="{DynamicResource TextBoxInError}"/>

 

5- Plus loin          

 On peut aussi définir les ViewModels par exemple (App.xaml)

<Application x:Class="MVVMMediatorDemo.App"          
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml"
             xmlns:vm="clr-namespace:MVVMMediatorDemo.ViewModels"
             >
    <Application.Resources>
       
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/DataTemplates/DataTemplates.xaml" />
                <ResourceDictionary Source="/Ressources/Styles.xaml" />
            </ResourceDictionary.MergedDictionaries>
           
            <vm:ClientListViewModel x:Key="clientListViewModel"/>
           
        </ResourceDictionary>
    </Application.Resources> 
</Application>                    

 

 Puis définir le DataContext
<Window x:Class="MVVMMediatorDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Views="clr-namespace:MVVMMediatorDemo.Views"
        Title="MainWindow" Height="300" Width="500">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="180"/>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Views:ClientListView Grid.Column="0" DataContext="{DynamicResource clientListViewModel}" />
        <Views:ClientDetailsView  Grid.Column="1" />
    </Grid>
</Window>

 

Partager cet article

Repost 0
Published by Romagny13 - dans Xaml
commenter cet article

commentaires