简体   繁体   English

在WPF C#中动态创建Expression Blend控件

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

I have created a button in Expression Blend 4. I want to dynamically create instances of this button at run time. 我已经在Expression Blend 4中创建了一个按钮。我想在运行时动态创建此按钮的实例。

The code for the button is below: 该按钮的代码如下:

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

In addition to the supplying the code can you put comments in explaining what you are doing so that I can learn the general principle. 除了提供代码外,您还可以在解释您的操作时添加注释,以便我可以学习一般原理。

I know this is a simple question so I have been reading up places such as: Expression blend & WPF , Is there a way to "extract" WPF controls of Expression Blend? 我知道这是一个简单的问题,所以我一直在阅读以下内容: Expression Blend 和WPF是否可以“提取” Expression Blend的WPF控件? , and http://social.msdn.microsoft.com/forums/en-US/wpf/thread/ffa981b8-9bba-43a2-ab5e-8e59bc10fc0d/ unfortunately none of these have helped. ,以及http://social.msdn.microsoft.com/forums/en-US/wpf/thread/ffa981b8-9bba-43a2-ab5e-8e59bc10fc0d/ ,这些都没有帮助。

In your WPF application you should have a App.xaml file, in there you can add Styles that are to be used thoughout your UI. 在WPF应用程序中,您应该有一个App.xaml文件,您可以在其中添加将在整个UI中使用的Styles

Example: 例:

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

Or if you dont want to apply to all buttons, you can give your Style a Key so you can apply to certain Buttons in your UI 或者,如果您不想将其应用于所有按钮,则可以为“ 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>

To use this Style on a Button , just add a binding to the Style property of the Button 要在Button上使用此Style ,只需将绑定添加到ButtonStyle属性

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

this will apply the Style just to this Button 这会将Style仅应用于此Button

Or if you really want to do it in code behind you can just add the Brush you want to the background 或者,如果您真的想在后面的代码中执行此操作,则只需将所需的Brush添加到背景中

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

its very easy to translate xaml to code because xaml uses the exact same property names, like the code brush I posted above: 将xaml转换为代码非常容易,因为xaml使用完全相同的属性名称,例如我上面发布的代码笔刷:

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

is .... 是...

Brush(firstColor,secondColor,StartPoint EndPoint)

Xaml just accesses properties in the button, they will all have the same names in C#. Xaml只是访问按钮中的属性,它们在C#中都具有相同的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM