WPF之資源字典zz


最近在看wpf相關東西,雖然有過兩年的wpf方面的開發經驗,但是當時開發的時候,許多東西一知半解,至今都是模模糊糊,框架基本是別人搭建,自己也就照着模板寫寫,現在許多東西慢慢的理解了,回顧以前的若干記憶,然后解決自己不懂的方面,在CSDN做記錄,寫下一些自己的理解,方便以后查閱:

    今天寫的是關於wpf的資源字典里面做觸發器,然后主界面進行調用:

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

    2:然后呢。一般都會將控件的觸發器寫成這樣:

  <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>

  那么控件多的時候,就顯得臃腫了,於是就想到了【資源字典】,在資源字典里面寫這些,然后調用,新建了【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>

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

   <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>

  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>

生成如下效果:


免責聲明!

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



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