■ Style 엘리먼트의 BasedOn 속성을 사용해 기존 디폴트 스타일에 스타일 트리거를 추가하는 방법을 보여준다.
▶ MainWindow.xaml
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 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="Style 엘리머트 : BasedOn 속성을 사용해 기존 디폴트 스타일에 스타일 트리거 추가하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <TextBlock Name="textBlock" HorizontalAlignment="Center" Text="비활성" /> <StackPanel HorizontalAlignment="Center" Margin="0 10 0 0" Orientation="Horizontal"> <Button Name="updateButton" Width="100" Height="30" Content="Update" /> <Button Name="testButton" Margin="10 0 0 0" Width="100" Height="30" Content="Test"> <Button.Style> <Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}"> <Style.Triggers> <DataTrigger Binding="{Binding ElementName=textBlock, Path=Text}" Value="비활성"> <Setter Property="IsEnabled" Value="False" /> </DataTrigger> </Style.Triggers> </Style> </Button.Style> </Button> </StackPanel> </StackPanel> </Grid> </Window> |
▶ MainWindow.xaml.cs
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
using System.Windows; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.updateButton.Click += updateButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region Update 버튼 클릭시 처리하기 - updateButton_Click(sender, e) /// <summary> /// Update 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void updateButton_Click(object sender, RoutedEventArgs e) { if(this.textBlock.Text == string.Empty) { this.textBlock.Text = "테스트"; } else { this.textBlock.Text = string.Empty; } } #endregion } } |