[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. 此為拋磚引玉之技法,如有更好的方法,還望高手不吝賜教!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM