代码之家  ›  专栏  ›  技术社区  ›  estanford

WPF中的派生类和主题继承?

  •  4
  • estanford  · 技术社区  · 14 年前

    编辑:我应该注意这些自定义控件是在运行时实例化的,所以我不能直接通过XAML操作它们的属性。我可以通过在应用程序资源字典中使用Setter来更改特定的属性,如背景色,但还没有找到使用该技术设置主题的方法。

    1 回复  |  直到 14 年前
        1
  •  1
  •   Fredrik Hedblad    14 年前

    如果在名为Dictionary1.xaml的资源字典中有此样式

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Style x:Key="MyButtonStyle"
               TargetType="{x:Type Button}" 
               BasedOn="{StaticResource {x:Type Button}}">
            <Setter Property="Width" Value="75" />
            <Setter Property="Height" Value="23" />
        </Style>
    </ResourceDictionary>
    

    然后你可以把它设置在任何按钮上

    Uri resourceLocater = new Uri("/YourAssemblyName;component/Dictionary1.xaml", System.UriKind.Relative);
    ResourceDictionary resourceDictionary = (ResourceDictionary)Application.LoadComponent(resourceLocater);
    Style myButtonStyle = resourceDictionary["MyButtonStyle"] as Style;
    
    Button button = new Button();
    button.Style = myButtonStyle;