有這樣的做法, 在前面指定 Button.Click :
<StackPanel Button.Click="button_Click" Grid.RowSpan="20">
<Button Grid.Column="0" Grid.Row="0" FontWeight="Bold" BorderBrush="Black" Style="{StaticResource greenButton}">LT 1</Button>
<Button x:Name="btn100" Grid.Column="0" Grid.Row="2" Style="{StaticResource greenButton}">100</Button>
<Button x:Name="btn101" Grid.Column="0" Grid.Row="3" Style="{StaticResource greenButton}">101</Button>
<Button x:Name="btn102" Grid.Column="0" Grid.Row="4" Style="{StaticResource greenButton}">102</Button>
</StackPanel>
但是這樣會有 null exception 跑出來, 當然可以用 (e.OriginalSource as Button) 來避這個問題,
也可以在 Style 裡用 EventSetter :
<Style x:Key="greenButton" TargetType="Button">
...
<EventSetter Event="Click" Handler="button_Click"/>
</Style>
沒有留言:
張貼留言