资源字典xaml中的事件处理程序

时间:2014-07-01 07:22:02

标签: vb.net xaml dictionary

我在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. 首先,当我在主窗口上,然后点击button1或button2时,我有一个msgbox(“你好”)。这是一个问题,因为当我点击字典中的按钮时,我只需要一个msgbox,例如BT_1(内容为test1的按钮)。

  2. 在我的词典中,我为我的词典按钮添加了一个样式和一个处理程序。但我想要的是为MyResources类中的每个按钮添加一个特定的处理程序,是否可能?

  3. 通常情况下,我想在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 ......

    我感谢您的回答。请不要给我发送类似线程被调动的链接,我已经骑了很多这些,这对我没有帮助......

1 个答案:

答案 0 :(得分:0)

不,你不能这样做,因为你无法在资源文件中动态创建事件处理程序名称。 我不认为XAML语法可以让你这样做。

仍然需要手动添加事件处理程序,对于您无法点击事件的每个按钮。