【问题标题】:Customizing Style&Template of a wpf user control from another assembly从另一个程序集中自定义 wpf 用户控件的样式和模板
【发布时间】:2013-09-28 21:37:02
【问题描述】:

我有一个单独的解决方案,我想在其中创建一个用户控件库,我可以在其他项目中将其作为 dll 引用(所以只使用 dll,不使用 xaml+code)。

比如我可以有这样一个控件:

<UserControl x:Class="WpfUserControlsLibrary.PlayerControls"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <Style x:Key="btnPlayer" TargetType="{x:Type Button}">
            <Setter Property="Width" Value="100"/>
            <Setter Property="Height" Value="50"/>
        </Style>
    </UserControl.Resources>
    <StackPanel Orientation="Horizontal">
        <Button Style="{StaticResource btnPlayer}" Name="btnPlay" Click="btnPlay_Click">Play</Button>
        <Button Style="{StaticResource btnPlayer}" Name="btnStop" Click="btnStop_Click">Stop</Button>
        <Button Style="{StaticResource btnPlayer}" Name="btnPause" Click="btnPause_Click">Pause</Button>
    </StackPanel>
</UserControl>

对于上面的代码,假设有另一个解决方案 TestUI 引用了 WpfUserControlsLibrary 的 dll,我想要以下内容:

  • 使用按钮的默认样式
  • 如果 TestUI 有自定义的外观和感觉,我希望能够通过定义或继承 TestUI 资源来更改按钮的默认样式(我还不知道这部分是否可以完成或如何完成)

是否有任何方法可以声明此控件或定义其样式/模板,以便可以从引用此 dll 的另一个项目自定义其 UI(同时保留代码隐藏功能)?

【问题讨论】:

    标签: c# wpf templates dll styles


    【解决方案1】:

    在您的 App.xaml 文件中,将资源文件添加为 MergedDictionary。

    由于您是从另一个程序集获取的,请在 MSDN - Pack URIs in WPF 处查看引用它的正确方法

    我正在我当前的项目中使用它! 您的代码应如下所示:

    <ResourceDictionary>
       <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary Source="myresourcedictionary.xaml"/>
          <ResourceDictionary Source="myresourcedictionary2.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-03
      • 1970-01-01
      • 1970-01-01
      • 2015-09-07
      • 2011-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多