Calendrier

Juillet 2009
L M M J V S D
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    
<< < > >>

Présentation

Recherche

W3C

  • Flux RSS des articles

[WPF] –
rendre une resource (style-template) accessible à toute l’application
1 – Ajouter un fichier de resources
resourcedictironnary.JPG

2 - On définit un style dans le fichier de ressource
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      <Style x:Key="BackgroundStyle1">
            <Setter Property="Button.Background" Value="Orange"/>
      </Style>
</ResourceDictionary>
 
3 – App.xaml
-          Source : Donner le chemin relatif vers le fichier de ressources
<Application x:Class="WPFTypedControls.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window1.xaml">
    <Application.Resources>
         <ResourceDictionary Source="StylesDictionary.xaml"/>
     </Application.Resources>
</Application>
 
 
4 – utilisation
Dans le Xaml
<Window x:Class="WPFTypedControls.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ContactControls="clr-namespace:WPFTypedControls"
    xmlns:MyNamespace="clr-namespace:WPFTypedControls"
    Title="Window1" Height="300" Width="400" Loaded="Window_Loaded">
      <WrapPanel>
            <ContactControls:ContactComboBox x:Name="contactComboBox1" Height="25" Width="150" />
            <Button Name="btnGetContact" Width="170" Height="25" Content="Obtenir le contact selectionné" Click="btnGetContact_Click" Style="{StaticResource BackgroundStyle1}"/>
      </WrapPanel>
</Window>
 
Dans le code-behind (C#)
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
 
            btnGetContact.Style = (Style)(Application.Current.Resources["BackgroundStyle1"]);
        }
 
    }
 
Par Romagny13
Ecrire un commentaire - Voir les 0 commentaires - Recommander
Retour à l'accueil
 
blog gratuit sur over-blog.com - Contact - C.G.U. - Rémunération en droits d'auteur - Signaler un abus