zoukankan      html  css  js  c++  java
  • [WPF] 为Style 里的button添加鼠标点击响应事件

    一个TabControl, 用的是PagedTabControl style, 在style中有个button, button在style里已经写了click事件,但是现在还需要加上一段功能,就是在响应事件之前对界面作一下判断。该怎么办呢?先看代码:

    1. 控件XAML部分代码(位于文件form_loadatorigin.xaml):

            <!-- Form Body -->
            <TabControl x:Name="formLoadUnload"
                Style="{StaticResource PagedTabControl}"  
                Tag="" HorizontalAlignment="Stretch"
                Grid.Row="0" Grid.RowSpan="2" 
                Grid.IsSharedSizeScope="True"
                >
    
                <!-- Page 1 -->
                <TabItem Name="LoadUnloadPage1" Foreground="Black">
                    <!-- 此处为Page1页面 略去-->
                </TabItem>
    
                <!-- Page 2 -->
                <TabItem Name="LoadUnloadPage2" Foreground="Black">
                    <!-- 此处为Page2页面 略去-->
                </TabItem>
            </TabControl>

    2. Style PagedTabControl代码:

    <Style x:Key="PagedTabControl" TargetType="{x:Type TabControl}">
            <Setter Property="OverridesDefaultStyle" Value="True" />
            <Setter Property="SnapsToDevicePixels" Value="True" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabControl}">
                        <Grid KeyboardNavigation.TabNavigation="Local">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <TabPanel Name="PagedHeaderPanel"
                                Grid.Row="0"
                                Grid.ColumnSpan="2"
                                Panel.ZIndex="0"
                                Margin="2,0,4,0" 
                                KeyboardNavigation.TabIndex="1"
                                      />
                            <StackPanel Grid.Column="1" Grid.Row="0" Orientation="Horizontal" Panel.ZIndex="1" Margin="0,0,50,0"
                                        KeyboardNavigation.TabIndex="1">
                                <Button x:Name="PreviousPageButton"
                                            Grid.Column="0" 
                                            Background="White">
                                    <Button.Content>
                                        <Image Source="/Common.WPF.Assets;component/Images/btn_left.png"/>
                                    </Button.Content>
                                    <Button.Style>
                                        <Style BasedOn="{StaticResource RoundButtonStyle}" TargetType="{x:Type Button}">
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent},Path=SelectedIndex}" Value="0">
                                                    <Setter Property="IsEnabled" Value="False"/>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Button.Style>
                                    <Button.Triggers>
                                        <EventTrigger RoutedEvent="Button.Click">
                                            <BeginStoryboard>
                                                <Storyboard>
                                                    <Int32Animation
                                                                Storyboard.Target="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=''}"
                                                                Storyboard.TargetProperty="SelectedIndex"
                                                                By="-1" Duration="0:0:.1" />
                                                </Storyboard>
                                            </BeginStoryboard>
                                        </EventTrigger>
                                    </Button.Triggers>
                                </Button>
                                <TextBlock VerticalAlignment="Center" Margin="10,0,10,0" Style="{StaticResource NormalText19Style}" HorizontalAlignment="Center" TextAlignment="Center" >
                                    <TextBlock.Text>
                                        <MultiBinding StringFormat="{}{0} {1} of {2}">
                                            <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Tag"/>
                                            <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="SelectedIndex" Converter="{StaticResource ZeroBasedIndexConverter}"/>
                                            <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Items.Count"/>
                                        </MultiBinding>
                                    </TextBlock.Text>
                                </TextBlock>
                                <Button x:Name="NextPageButton"
                                            Background="White">
                                    <Button.Content>
                                        <Image Source="/Common.WPF.Assets;component/Images/btn_right.png"/>
                                    </Button.Content>
    
                                    <Button.Style>
                                        <Style BasedOn="{StaticResource RoundButtonStyle}" TargetType="{x:Type Button}">
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent},Path=SelectedIndex}" Value="Items.Count">
                                                    <Setter Property="IsEnabled" Value="False"/>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Button.Style>
                                    <Button.Triggers>
    
                                        <EventTrigger RoutedEvent="Button.Click">
                                            <BeginStoryboard>
                                                <Storyboard>
                                                    <Int32Animation
                                                                Storyboard.Target="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=''}"
                                                                Storyboard.TargetProperty="SelectedIndex"
                                                                By="1" Duration="0:0:.1" />
                                                </Storyboard>
                                            </BeginStoryboard>
                                        </EventTrigger>
                                    </Button.Triggers>
                                </Button>
                            </StackPanel>
                            <TabPanel 
                                        Name="HeaderPanel"
                                        Grid.Row="0"
                                        Panel.ZIndex="1"
                                        Height="0"
                                        IsItemsHost="True">
                            </TabPanel>
                            <Border
                                        Name="Border" 
                                        Grid.Row="1"
                                        Grid.ColumnSpan="2"
                                        BorderThickness="1" 
                                        CornerRadius="2" 
                                        KeyboardNavigation.TabNavigation="Local"
                                        KeyboardNavigation.DirectionalNavigation="Contained"
                                        KeyboardNavigation.TabIndex="2">
                                <ContentPresenter 
                                            Name="PART_SelectedContentHost"
                                            Margin="4"
                                            ContentSource="SelectedContent" />
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    我们需要NextPageButton添加鼠标点击,下面就是在控件的实现文件里添加的代码:

    3. 在form_loadatorigin.xaml.cs文件里的实现代码:

    public form_loadatorigin()
    {
        InitializeComponent();
    
        this.Loaded += new RoutedEventHandler(form_loadatorigin_Loaded);
    }
    
    private void form_loadatorigin_Loaded(object sender, RoutedEventArgs e)
    {
        if (!IsFirstLoaded)
        {
            IsFirstLoaded = true;
            formLoadUnload.SelectionChanged += new SelectionChangedEventHandler(formLoadUnload_SelectionChanged);
    
            DependencyObject d1 = VisualTreeHelper.GetChild(formLoadUnload, 0);
            m_NextPageButton = LogicalTreeHelper.FindLogicalNode(d1, "NextPageButton") as Button;
            m_NextPageButton.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(m_NextPageButton_PreviewMouseLeftButtonDown);
        }
    }
    
    void m_NextPageButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        e.Handled = true;
        if (ValidateAndShowErrors())
        {
            m_NextPageButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent, m_NextPageButton));
        }
    }
    
    private Button m_NextPageButton;
    private bool IsFirstLoaded = false;        

    注意:

    1. m_NextPageButton必须为成员变量,如果改成局部变量,则添加事件不会起作用。至于原因,还没有找到为什么。

    2. 此为抛砖引玉之技法,如有更好的方法,还望高手不吝赐教!

  • 相关阅读:
    light oj 1105 规律
    light oj 1071 dp(吃金币升级版)
    light oj 1084 线性dp
    light oj 1079 01背包
    light oj 1068 数位dp
    light oj 1219 树上贪心
    light oj 1057 状压dp TSP
    light oj 1037 状压dp
    矩阵快速幂3 k*n铺方格
    矩阵快速幂2 3*n铺方格
  • 原文地址:https://www.cnblogs.com/bolddream/p/WPF-Add-event-to-button-in-Style.html
Copyright © 2011-2022 走看看