0

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 & WPFExpression Blend の WPF コントロールを「抽出」する方法はありますか? 、およびhttp://social.msdn.microsoft.com/forums/en-US/wpf/thread/ffa981b8-9bba-43a2-ab5e-8e59bc10fc0d/残念ながら、これらのどれも役に立ちませんでした。

4

1 に答える 1

1

WPF アプリケーションにはファイルが必要です。そこに、UI 全体で使用するApp.xamlファイルを追加できます。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>

または、すべてのボタンに適用したくない場合は、UI の特定のボタンに適用できるようにStyleaを指定できます。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>

でこれを使用するStyleにはButton、バインディングを のStyleプロパティに追加するだけです。Button

  <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# ではすべて同じ名前になります。

于 2012-12-24T00:03:39.037 に答える