WPF之資源字典zz

最近在看wpf相關東西,雖然有過兩年的wpf方面的開發經驗,可是當時開發的時候,許多東西只知其一;不知其二,至今都是模模糊糊,框架基本是別人搭建,本身也就照着模板寫寫,如今許多東西慢慢的理解了,回顧之前的若干記憶,而後解決本身不懂的方面,在CSDN作記錄,寫下一些本身的理解,方便之後查閱:app

    今天寫的是關於wpf的資源字典裏面作觸發器,而後主界面進行調用:框架

   1: 首先創建了個wpf窗體程序WpfApplication1;工程裏面自動生成App.xaml和MainWindow.xaml兩個文件,這兩個文件就不作介紹了,App.xaml會在下面用到。orm

    2:而後呢。通常都會將控件的觸發器寫成這樣:xml

  <Button Name="btnUpdate" Grid.Row="1" Width="100" Height="30" Content="更 新"  Command="{Binding CmdCommand}" CommandParameter="{Binding Oberve}">
                <Button.Style>
                    <Style TargetType="{x:Type Button}">
                        <Style.Triggers>
                            <Trigger Property="Button.IsMouseOver" Value="True">
                                <Setter Property="Button.Foreground" Value="Blue"/>
                            </Trigger>
                            <Trigger Property="Button.IsPressed" Value="True">
                                <Setter Property="Button.Foreground" Value="Red"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
        </Button>ip

  那麼控件多的時候,就顯得臃腫了,因而就想到了【資源字典】,在資源字典裏面寫這些,而後調用,新建了【Dictionary1.xaml】文件資源

將要是實現的式樣寫進它裏面:開發

  <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="CircleButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Border.BorderThickness" Value="1,1,1,1" />
        <Setter Property="Border.CornerRadius" Value="3" />
        <Setter Property="Height" Value="36" />
        <Setter Property="Width" Value="36" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid>
                        <Ellipse Fill="{TemplateBinding Background}"/>
                        <Ellipse>
                            <Ellipse.Fill>
                                <RadialGradientBrush>
                                    <GradientStop Offset="0" Color="#00000000"/>
                                    <GradientStop Offset="0.88" Color="#00000000"/>
                                    <GradientStop Offset="1" Color="#80000000"/>
                                </RadialGradientBrush>
                            </Ellipse.Fill>
                        </Ellipse>
                        <Ellipse Margin="10" x:Name="highlightCircle" >
                            <Ellipse.Fill >
                                <LinearGradientBrush >
                                    <GradientStop Offset="0" Color="#50FFFFFF"/>
                                    <GradientStop Offset="0.5" Color="#00FFFFFF"/>
                                    <GradientStop Offset="1" Color="#50FFFFFF"/>
                                </LinearGradientBrush>
                            </Ellipse.Fill>
                        </Ellipse>
                        <ContentPresenter x:Name="content" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="RenderTransform">
                        <Setter.Value>
                            <RotateTransform Angle="10"></RotateTransform>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="RenderTransformOrigin" Value="0.5,0.5"></Setter>
                    <Setter Property="Background" Value="#FF0CC030" />
                </Trigger>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Foreground" Value="Red"/>
                </Trigger>
        </Style.Triggers>
    </Style>
</ResourceDictionary>get

     3:這下就用到App.xaml發揮做用了;App這個文件是協助運行主窗口的文件,在app文件裏面將數據資源裏面的東東引進:io

   <Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary1.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>form

  4:這樣後,MainWindow.xaml文件中就能夠引進數據字典裏面的式樣和觸發器了;

        <Grid>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="157,119,0,0" Name="button1"
  Style="{StaticResource CircleButtonStyle}"//引進數據字典裏面的式樣
                VerticalAlignment="Top" Width="75" />
    </Grid>

生成以下效果:

相關文章
相關標籤/搜索