2017-03-29 34 views
0

我正在製作usercontrol庫。因此我沒有app.xaml文件,也沒有mainwindow.xaml用戶控件在應用樣式後不顯示

我從另一個WPF項目導入(複製)滑塊樣式。這resource dictionary設置爲page,並且以前工作正常,但是,只要我將它應用到我的滑塊,然後該控件不顯示在VisualStudio以及運行時。沒有錯誤被拋出。

<UserControl x:Class="WPF.UserControls.CustomSlider" 
     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" 
     xmlns:local="clr-namespace:WPF.UserControls" 
     x:Name="CustomSliderControl" 
     mc:Ignorable="d" 
     d:DesignHeight="407" d:DesignWidth="127"> 
<UserControl.Resources> 
    <ResourceDictionary Source="/WPFUserControls;component/Styles/BaseSliderStyle.xaml"/> 
</UserControl.Resources> 
<Grid> 
    <Slider x:Name="Hello" Style="{DynamicResource BaseSliderStyle}" Value="{Binding Value, Mode=TwoWay, 
RelativeSource={RelativeSource AncestorType={x:Type local:CustomSlider}}}" Minimum="0.0" Maximum="1.0"/> 
</Grid> 

這裏是滑蓋造型的一部分:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:local="clr-namespace:WPF.UserControls"> 
<Style x:Key="BaseSliderStyle" TargetType="{x:Type Slider}"> 
    <Setter Property="Stylus.IsPressAndHoldEnabled" Value="false"/> 
    <Setter Property="Background" Value="Transparent"/> 
    <Setter Property="BorderBrush" Value="Transparent"/> 
    <Setter Property="Foreground" Value="{StaticResource SliderThumb.Static.Foreground}"/> 
    <Setter Property="Template" Value="{StaticResource SliderHorizontal}"/> 
    <Style.Triggers> 
     <Trigger Property="Orientation" Value="Vertical"> 
      <Setter Property="Template" Value="{StaticResource SliderVertical}"/> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

我可能會錯過一些東西。任何提示?

感謝。

+0

是WPFUserControls你的DLL /項目的名稱? – GCamel

+0

檢查是否導入了 中的資源。另外檢查SliderVertical是否被導入。 –

回答

1

確保您已添加到WPFUserControls.dll參考,試試這個:

<UserControl ...> 
    <UserControl.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="pack://application:,,,/WPFUserControls;component/Styles/BaseSliderStyle.xaml"/> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </UserControl.Resources> 
    <Grid> 
     <Slider x:Name="Hello" Style="{StaticResource BaseSliderStyle}" Value="{Binding Value, Mode=TwoWay, 
        RelativeSource={RelativeSource AncestorType={x:Type local:CustomSlider}}}" Minimum="0.0" Maximum="1.0"/> 
    </Grid> 
</UserControl> 

由於我使用的是StaticResource標記擴展,你應該得到一個例外,如果「BaseSliderStyle」無法找到。如果確認已找到樣式並按預期方式應用,則可以切換回使用DynamicResource

另請注意,我正在使用合併的ResourceDictionary和pack URI來指定源:https://msdn.microsoft.com/en-us/library/aa970069(v=vs.110).aspx

另請確保資源字典實際名爲「BaseSliderStyle.xaml」,並且位於名爲「WPFUserControls」的項目/程序集根目錄下名爲「樣式」的文件夾下。