天天看點

WPF 為資源字典 添加事件響應的背景類

原文: WPF 為資源字典 添加事件響應的背景類

前言,有許多同學在寫WPF程式時在資源字典裡加入了其它控件,但又想寫事件來控制這個控件,但是資源字典沒有CS檔案,不像窗體XAML還有一個背景的CS檔案,怎麼辦呢?

在工作時也遇到了這個問題,現在把它分享出來

比如說我們現在要寫一個TabControl控件,在TabItem中有一個關閉按鈕或其它按鈕,這個按鈕要能響應某個事件。

WPF 為資源字典 添加事件響應的背景類
現在開始寫資源字典裡的 TabItem的樣式,代碼如下

<Style x:Key="TIStyle" TargetType="{x:Type TabItem}">
        <Setter  Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TabItem}">
                    <Border x:Name="layout" BorderBrush="Gray" BorderThickness="1,1,1,0" Background="{TemplateBinding Background}"
                                CornerRadius="3" Margin="2,0,2,0">
                        <Grid Height="20">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition Width="25"/>
                            </Grid.ColumnDefinitions>
                            <TextBlock  TextAlignment="Center" Text="{TemplateBinding Header}" Grid.Column="0" Margin="4,0,3,0"
                            VerticalAlignment="Center"    HorizontalAlignment="Center"/>
                            <Button Content="X" Grid.Column="1"  Height="8" Width="8" Margin="4,1,3,2" 
                            Tag="{TemplateBinding Header}" Click="Button_Click"
                            Background="{x:Null}" BorderBrush="{x:Null}" VerticalAlignment="Center">
                                <Button.Template>
                                    <ControlTemplate >
                                        <Grid>
                                            <Rectangle>
                                                <Rectangle.Fill>
                                                    <VisualBrush>
                                                        <VisualBrush.Visual>
                                                            <Path x:Name="btnPath" 
                                                        Data="M0 0L10 10M0 10L10 0" Stroke="Gray"
                                                        StrokeThickness="1"/>
                                                        </VisualBrush.Visual>
                                                    </VisualBrush>
                                                </Rectangle.Fill>
                                            </Rectangle>
                                        </Grid>
                                        <ControlTemplate.Triggers>
                                            <Trigger Property="IsMouseOver" Value="True">
                                                <Setter TargetName="btnPath" Property="Stroke" Value="Red"/>
                                            </Trigger>
                                        </ControlTemplate.Triggers>
                                    </ControlTemplate>
                                </Button.Template>
                            </Button>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="White"/>
                            <Setter TargetName="layout" Property="Margin" Value="2,0,2,-1.5"/>
                        </Trigger>
                        <Trigger Property="IsSelected" Value="false">
                            <Setter Property="Background" Value="LightBlue"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>      

然後為資源字典建一個背景類DicEvent.cs

public  partial class DicEvent : ResourceDictionary
    {
       public void Button_Click(object sender, RoutedEventArgs e)
       {
           //省去處理,如果顯示,表明調用成功。
           MessageBox.Show("你成功了!");
       }
    }      

在資源字典裡,添加對背景類的引用

WPF 為資源字典 添加事件響應的背景類

主視窗裡調用:

<Window x:Class="WpfDicButton.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">
    <Grid>
        <TabControl>
            <TabItem Header="第一" Height=" 30" Style="{DynamicResource ResourceKey=TIStyle}"></TabItem>
            
        </TabControl>
    </Grid>
</Window>      

記住APP檔案裡加入資源字典

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="TabControlDictionary.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>      

測試,小功告成

WPF 為資源字典 添加事件響應的背景類

奉上DEMO  

下載下傳位址