C# 应用样式后不显示UserControl
我正在制作一个C# 应用样式后不显示UserControl,c#,wpf,C#,Wpf,我正在制作一个usercontrol库。因此,我没有app.xaml文件,也没有mainwindow.xaml 我从另一个WPF项目导入(复制)了一个滑块样式。此资源字典设置为页面,以前工作正常,但一旦我将其应用到滑块上,控件就不会显示在VisualStudio和运行时中。不会抛出任何错误 <UserControl x:Class="WPF.UserControls.CustomSlider" xmlns="http://schemas.microsoft.com/win
usercontrol
库。因此,我没有app.xaml
文件,也没有mainwindow.xaml
我从另一个WPF项目导入(复制)了一个滑块样式。此资源字典
设置为页面
,以前工作正常,但一旦我将其应用到滑块上,控件就不会显示在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>
我可能错过了什么。有什么提示吗
谢谢。请确保已添加对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
和一个包URI来指定源:
还请确保资源字典实际上名为“BaseSliderStyle.xaml”,并且位于名为“WPFuserControl”的项目/程序集根目录下名为“Styles”的文件夹下。WPFuserControl是DLL/项目的名称吗?检查中的资源是否已导入。还要检查是否导入了SliderVertical。