簡體   English   中英

在WPF C#中動態創建Expression Blend控件

[英]Dynamically creating Expression Blend controls in WPF C#

我已經在Expression Blend 4中創建了一個按鈕。我想在運行時動態創建此按鈕的實例。

該按鈕的代碼如下:

    <Button Content="Button" HorizontalAlignment="Left" Height="139" Margin="46,107,0,0" VerticalAlignment="Top" Width="412" Grid.ColumnSpan="2">
        <Button.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="Black"/>
                <GradientStop Color="White" Offset="1"/>
            </LinearGradientBrush>
        </Button.Background>
    </Button>

除了提供代碼外,您還可以在解釋您的操作時添加注釋,以便我可以學習一般原理。

我知道這是一個簡單的問題,所以我一直在閱讀以下內容: Expression Blend 和WPF是否可以“提取” Expression Blend的WPF控件? ,以及http://social.msdn.microsoft.com/forums/en-US/wpf/thread/ffa981b8-9bba-43a2-ab5e-8e59bc10fc0d/ ,這些都沒有幫助。

在WPF應用程序中,您應該有一個App.xaml文件,您可以在其中添加將在整個UI中使用的Styles

例:

<Application x:Class="WpfApplication8.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>

        <!--The style for all your buttons, setting the background property to your custom brush-->
        <Style TargetType="{x:Type Button}"> <!--Indicate that this style should be applied to Button type-->
            <Setter Property="Background">
                <Setter.Value>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="Black"/>
                        <GradientStop Color="White" Offset="1"/>
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
        </Style>

    </Application.Resources>
</Application>

或者,如果您不想將其應用於所有按鈕,則可以為“ Style一個Key以便可以將其應用於用戶界面中的某些按鈕

<Application x:Class="WpfApplication8.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>

        <!--Add a x:Key value so you can use on certain Buttons not all-->
        <Style x:Key="MyCustomStyle" TargetType="{x:Type Button}"> 
            <Setter Property="Background">
                <Setter.Value>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="Black"/>
                        <GradientStop Color="White" Offset="1"/>
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
        </Style>

    </Application.Resources>
</Application>

要在Button上使用此Style ,只需將綁定添加到ButtonStyle屬性

  <Button Style="{StaticResource MyCustomStyle}" />

這會將Style僅應用於此Button

或者,如果您真的想在后面的代碼中執行此操作,則只需將所需的Brush添加到背景中

   Button b = new Button
   {
       Background = new LinearGradientBrush(Colors.Black, Colors.White, new Point(0.5, 1), new Point(0.5, 0))
   };

將xaml轉換為代碼非常容易,因為xaml使用完全相同的屬性名稱,例如我上面發布的代碼筆刷:

new LinearGradientBrush(Colors.Black, Colors.White, new Point(0.5, 1), new Point(0.5, 0)) 

是...

Brush(firstColor,secondColor,StartPoint EndPoint)

Xaml只是訪問按鈕中的屬性,它們在C#中都具有相同的名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM