我有一个带有Border元素的UserControl,我想用特定的Border样式设置样式。它编译但不会启动,给出一个XamlParseException,说“找不到资源......”
有办法做到这一点吗?
感谢。
的App.xaml:
<cal:CaliburnApplication x:Class="WahnamProgressTracker.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.caliburnproject.org"
xmlns:Converters="clr-namespace:WahnamProgressTracker.Converters;assembly=WahnamProgressTracker"
xmlns:Model="clr-namespace:WahnamProgressTracker.Model">
<Application.Resources>
<Style x:Key="FancyBorder"
TargetType="{x:Type Border}">
<Setter Property="Margin" Value="0,0,0,8"/>
<Setter Property="Padding" Value="8"/>
...
</Style>
</Application.Resources>
MainView.xaml:
<Window x:Class="WahnamProgressTracker.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.caliburnproject.org"
xmlns:uc="clr-namespace:WahnamProgressTracker.UserControls"
MinHeight="500" MinWidth="800">
<DockPanel>
<uc:MainViewMenu x:Name="menu"
DockPanel.Dock="Top" />
<StatusBar x:Name="quoteBar"
DockPanel.Dock="Bottom">
<TextBlock Text="{Binding Path=Quote.Text, Mode=OneWay}" />
</StatusBar>
<uc:MainViewNavigation x:Name="navigationBar"
DockPanel.Dock="Left" />
<uc:ProgressGraph x:Name="graph" />
</DockPanel>
MainViewNavigation.xaml(用户控件):
<UserControl x:Class="WahnamProgressTracker.UserControls.MainViewNavigation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Border Style="{StaticResource FancyBorder}">
...
</Border>
</UserControl>
答案 0 :(得分:2)
你能发表你的意思吗?发生问题的唯一情况是创建用户控件,然后在应用程序的可视树之外呈现。
下面的XAML对我有用:
的App.xaml:
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
<Style TargetType="{x:Type TextBlock}" x:Key="myStyle">
<Setter Property="Foreground" Value="Green" />
<Setter Property="FontWeight" Value="Bold" />
</Style>
</Application.Resources>
</Application>
Window1.xaml:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<local:UserControl1 />
</Grid>
</Window>
UserControl1.xaml:
<UserControl x:Class="WpfApplication1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<Grid>
<TextBlock Style="{StaticResource myStyle}">HEY!</TextBlock>
</Grid>
</UserControl>