【问题标题】:Code-behind static/dynamic properties in WPF and Naming ConundrumWPF 中的代码隐藏静态/动态属性和命名难题
【发布时间】:2011-12-07 07:19:23
【问题描述】:

我在我的 XAML 中定义了一个按钮,它有一个按钮样式和一个矢量图形,它是这样定义的:

    <Button
        Height="48"
        Width="48"
        mvp:Presenter.Action="CreateStudentApplication"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        Grid.Row="5"
        Grid.Column="3"
        Style="{DynamicResource BaseButton2}">
        <ContentControl Template="{StaticResource ApplicationVector}"/>
    </Button>

在我的代码隐藏中,我有一种方法可以动态地将与此类似的新按钮添加到 StackPanel。简而言之,它做了以下事情:

            var button = new Button();
            button.Height = 48;
            button.Width = 48;
            button.Tag = x.ID;
            button.SetResourceReference(TemplateProperty, "ApplicationVector");
            button.SetResourceReference(StyleProperty, "BaseButton2");

现在这里是奇怪的部分——它只显示矢量图形,后面没有按钮。当我删除倒数第二行(带有矢量参考的行)时,它会显示按钮的样式!我假设设置模板会覆盖样式,但是它们似乎在 XAML 中友好地播放。我也尝试过设置 ContentProperty instidead of TemplateProperty,但这导致了类型的字符串。有任何想法吗?谢谢!

【问题讨论】:

    标签: wpf button code-behind vector-graphics dynamicresource


    【解决方案1】:

    您的 XAML 和后面的代码不等效。在 XAML 中,您将按钮的 Content 属性设置为 ContentControl。在后面的代码中,您将Template(或Content)属性设置为ApplicationVector 资源。

    您需要将按钮的Content 属性设置为ContentControl 的实例,其Template 属性设置为您的ApplicationVector 资源。

    var contentControl = new ContentControl();
    contentControl.SetResourceReference(TemplateProperty, "ApplicationVector");
    button.Content = contentControl;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-30
      • 1970-01-01
      • 2013-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 2014-10-27
      相关资源
      最近更新 更多