【问题标题】:Dynamically creating Expression Blend controls in WPF C#在 WPF C# 中动态创建 Expression Blend 控件
【发布时间】:2012-12-24 09:09:41
【问题描述】:

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

除了提供代码之外,您能否让 cmets 解释您在做什么,以便我可以了解一般原理。

我知道这是一个简单的问题,所以我一直在阅读诸如 Expression blend & WPFIs there a way to "extract" WPF controls of Expression Blend?http://social.msdn.microsoft.com/forums/en-US/wpf/thread/ffa981b8-9bba-43a2-ab5e-8e59bc10fc0d/ 之类的地方,不幸的是这些都没有帮助。

【问题讨论】:

  • 看起来你想创建一个Style
  • 你能告诉我多一点什么是风格,那里有一些学习风格的好地方。谢谢:-)
  • @Brett WPF 教程很棒:wpftutorial.net/Styles.html
  • 谢谢,这是一个很好的资源。不幸的是,与我的目标并没有真正的关系。

标签: c# wpf wpf-controls expression-blend


【解决方案1】:

在您的 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,以便应用到 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# 中都将具有相同的名称。

【讨论】:

  • 我期待更像Button NewButton = new button(); 的东西。这都是 XAML。我想我们一定是在某种程度上偏离了轨道。
  • 如果您每次创建一个新按钮时都使用我发布的第一个示例,它看起来就像您在 Blend 中制作的那样。 Button NewButton = new button(); 与 App.xaml 中的样式看起来像您的混合样式
  • 仅添加代码示例,如果您真的想这样做的话
  • 我真的很想编写代码。你怎么知道代码是什么?特别是关于背景的部分是不直观的。有没有我可以查找的网站或其他资源来学习这些东西?
  • 你不想编码是什么意思?我知道代码是什么,因为您在问题中粘贴了代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-07
  • 2011-07-13
  • 1970-01-01
  • 2011-03-18
相关资源
最近更新 更多