基于标准控件的边框样式导致运行时错误

时间:2017-06-03 12:04:26

标签: wpf xaml styles border basedon

我使用Blend 2017(.net 4.7)创建了一个新的WPF项目,其中包含一个窗口和此Xaml(后面没有添加任何代码):

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow"
        Height="350"
        Width="525">
    <Window.Resources>
        <Style x:Key="_borderStyleWithChildBinding"
               TargetType="{x:Type Border}"
               BasedOn="{StaticResource {x:Type Border}}">
            <Setter Property="BorderBrush"
                    Value="{Binding RelativeSource={RelativeSource Self}, Path=Child.Fill}" />
        </Style>
    </Window.Resources>
    <Grid Width="50"
          Height="30"
          Margin="10">
        <Border BorderThickness="5"
                Style="{StaticResource _borderStyleWithChildBinding}">
            <Border.Child>
                <Rectangle Width="20"
                           Height="10"
                           Fill="Green" />
            </Border.Child>
        </Border>
    </Grid>
</Window>

它编译但报告有关行

的运行时错误
BasedOn="{StaticResource {x:Type Border}}"

例外: System.Windows.Markup.XamlParseException: System.Windows.Markup.StaticResourceHolder的值导致异常。

InnerException:无法找到资源System.Windows.Controls.Border

设计师足够聪明,可以展示正确的东西:Border with child binding

1 个答案:

答案 0 :(得分:0)

我们可以通过省略边框样式的<img src = "picture.jpg" width = "20" height = "<?php echo $height;?>"> x:Key属性来解决问题:

BasedOn