2017-12-02 2 views
1

クリックでスタイルを変更したいのですが、その方法はわかりません。ここで はスタイルクリックしてウィンドウのスタイルを変更するWPF

<Style x:Key="CustomWindowStyle" TargetType="{x:Type Window}"> 
     <Setter Property="WindowStyle" Value="None"/> 
     <Setter Property="AllowsTransparency" Value="True"/> 
     <Setter Property="MinWidth" Value="200"/> 
     <Setter Property="MinHeight" Value="46"/>  
     <!--CaptionHeight + ResizeBorderThickness * 2--> 
     <Setter Property="Background" Value="Yellow"/> 
     <Setter Property="BorderBrush" Value="Green"/> 
     <Setter Property="BorderThickness" Value="7"/> 
     <Setter Property="Foreground" Value="DarkRed"/> 
     <Setter Property="Template" Value="{StaticResource WindowTemplate}"/> 
    </Style> 

    <!--the red style window--> 
    <Style x:Key="RedWindowStyle" TargetType="{x:Type Window}"> 
     <Setter Property="WindowStyle" Value="None"/> 
     <Setter Property="AllowsTransparency" Value="True"/> 
     <Setter Property="MinWidth" Value="100"/> 
     <Setter Property="MinHeight" Value="46"/>    
     <Setter Property="Background" Value="white"/> 
     <Setter Property="BorderBrush" Value="DarkRed"/> 
     <Setter Property="BorderThickness" Value="7"/> 
     <Setter Property="Foreground" Value="DarkGray"/> 
     <Setter Property="Template" Value="{StaticResource WindowTemplate}"/> 
    </Style> 

である私は、私はおそらく私のスタイルでこれを結合するであろう

答えて

0

を助けてくださいMainWindow.xaml.cs

private void button_Click(object sender, RoutedEventArgs e) 
     { 
      this.Style ?? // dont know what to do 
     } 

でそれを変更したいです。たとえば:

MainWindow.xaml:

<Window x:Class="WpfApp2.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:WpfApp2" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <Style TargetType="local:MainWindow"> 
      <Setter Property="Background" Value="Red" /> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding SomeProperty}" Value="True"> 
        <Setter Property="Background" Value="Blue" /> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </Window.Resources> 
    <Grid> 
     <Button HorizontalAlignment="Center" VerticalAlignment="Center" Click="Button_Click">Click Me, Yo!</Button> 
    </Grid> 
</Window> 

MainWindow.xaml.cs:

using System.ComponentModel; 
using System.Runtime.CompilerServices; 
using System.Windows; 

namespace WpfApp2 
{ 
    public partial class MainWindow : Window, INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 

     private bool _someProperty; 
     public bool SomeProperty 
     { 
      get { return _someProperty; } 
      set 
      { 
       _someProperty = value; 
       OnPropertyChanged(); 
      } 
     } 

     public MainWindow() 
     { 
      DataContext = this; 
      InitializeComponent(); 
     } 

     protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
     { 
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
     } 

     private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      SomeProperty = !SomeProperty; 
     } 
    } 
} 

は基本的に、あなたは、コンバータを使用して、あなたのViewModelでいくつかのプロパティから、あなたのウィンドウの状態を導き出します必要に応じて。もちろん

あなたは直接のソリューションをしたい場合は、あなただけにfindResourceを使用することができます。

this.Style = this.FindResource("MyLocalStyleResourceName") as Style; 

これはhereに記載されています。

関連する問題