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

除了提供代码之外,您是否可以在解释您正在做的事情时添加注释,以便我可以了解一般原理。

我知道这是一个简单的问题,所以我一直在阅读的地方,如:表达混合&WPF,有没有办法"提取"?表达式混合的WPF控件?,和http://social.msdn.microsoft.com/forums/en-US/wpf/thread/ffa981b8-9bba-43a2-ab5e-8e59bc10fc0d/不幸的是,这些都没有帮助。

在你的WPF应用程序中,你应该有一个App.xaml文件,在那里你可以添加Styles,在你的UI中使用。

的例子:

<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,这样你就可以应用到某些按钮在你的UI

<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#中都有相同的名称。

最新更新