0
我已經在vb.net寫了這一點代碼:事件處理
首先在主窗口:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Button Content="Button1" Height="23" HorizontalAlignment="Left" Margin="35,35,0,0" Name="Button1" VerticalAlignment="Top" Width="75" />
<Button Content="Button2" Height="23" HorizontalAlignment="Left" Margin="35,85,0,0" Name="Button2" VerticalAlignment="Top" Width="75" />
<StackPanel Name="Display" Grid.Row="1" Grid.Column="0" Background="LightBlue" Margin="50,150,50,10"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ContentControl Name="Content">
</ContentControl>
</StackPanel>
</Grid>
然後詞典:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MyResources">
<Style TargetType="{x:Type Button}">
<EventSetter Event="Button.Click" Handler="Click"/>
</Style>
<DataTemplate x:Key="Template1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBox Margin="10,50,0,0" HorizontalAlignment="Center" VerticalAlignment="Top" Width="200" />
<Button Name="BT_1" Content="test1" Margin="10,10" HorizontalAlignment="right" VerticalAlignment="Top"
Width="150" />
</Grid>
</DataTemplate>
<DataTemplate x:Key="Template2">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBox Margin="10,50,0,0" HorizontalAlignment="Center" VerticalAlignment="Top" Width="200" />
<Button Name="BT_2" Content="test2" Margin="10,10" HorizontalAlignment="right" VerticalAlignment="Top"
Width="150" />
</Grid>
</DataTemplate>
最後類爲MyResources負責管理我的字典後面的代碼:
Public Class MyResources
Private Sub Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
MsgBox("hello")
End Sub
末級
此代碼編譯正常,沒有問題。但我的問題是這樣的一個:
首先,當我對我的主窗口和我點擊按鈕1或按鈕2,我有一個MsgBox(「你好」)。這是一個問題,因爲我只需要一個msgbox,當我點擊我的字典中的一個按鈕時,例如BT_1(具有內容test1的那個)。
在我的字典中,我爲字典的按鈕添加了樣式和處理程序。但是我想要的是爲MyResources類中的每個按鈕添加特定的處理程序,這可能嗎?
通常情況下,我想在爲MyResources類類似的東西:
Private Sub Click_one(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
MsgBox("BT_1 clicked")
End Sub
Private Sub Click_two(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
MsgBox("BT_2 clicked")
End Sub
但我的方法後,我可以不加手柄BT_1 ... ..
我給你感謝你的答案。請不要送我同類者線程已經evocated,我已經騎過很多人,並沒有幫助我的鏈接...
謝謝我自己找到了解決方案。 – user3656508