Overblog Tous les blogs Top blogs Économie, Finance & Droit Tous les blogs Économie, Finance & Droit
Editer l'article Suivre ce blog Administration + Créer mon blog
MENU

Cycle de progression Faire (quelque chose qui marche) -> comprendre ce que l’on fait/comment cela marche -> pousser plus loin les notions

Publicité

Accèder à une resource de la window dans le code Behind

[WPF] –
Accèder à une resource de la window dans le code Behind

Soit un style tout simple défini dans les ressources de la Window que je vais affecter au background d’un button
<Window x:Class="WPFTypedControls.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="400" Loaded="Window_Loaded">
      <Window.Resources>
            <Style x:Key="BackgroundStyle1">
                  <Setter Property="Button.Background" Value="Orange"/>
            </Style>
      </Window.Resources>
      <WrapPanel>      
            <Button Name="btnGetContact" Width="170" Height="25" Content="Obtenir le contact selectionné" Click="btnGetContact_Click"/>
      </WrapPanel>
</Window>
 
2 possibilités pour affecter ce style :
-          Dans le Xaml
 
            <Button Name="btnGetContact" Width="170" Height="25" Content="Obtenir le contact selectionné" Click="btnGetContact_Click" Style="{StaticResource BackgroundStyle1}"/>
 
-          Dans le code-behind
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
 
            btnGetContact.Style = (Style)(this.Resources["BackgroundStyle1"]);
        }
}
Publicité
Retour à l'accueil
Partager cet article
Repost0
Pour être informé des derniers articles, inscrivez vous :
Commenter cet article