Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带有附加属性的样式在根命名空间中工作,在嵌套命名空间中或被外部程序集访问时失败_C#_Wpf_Xaml_Templates_Attached Properties - Fatal编程技术网

C# 带有附加属性的样式在根命名空间中工作,在嵌套命名空间中或被外部程序集访问时失败

C# 带有附加属性的样式在根命名空间中工作,在嵌套命名空间中或被外部程序集访问时失败,c#,wpf,xaml,templates,attached-properties,C#,Wpf,Xaml,Templates,Attached Properties,根据要求: 以下资源字典: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:l="clr-namespace:MCVE"> <RadialGradientBrush x:Key="Glow">

根据要求:

以下资源字典:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:l="clr-namespace:MCVE">
    <RadialGradientBrush x:Key="Glow">
        <RadialGradientBrush.RelativeTransform>
            <TranslateTransform X="0.0" Y="0.5" />
        </RadialGradientBrush.RelativeTransform>
        <GradientStop Color="#B28DD8FF" Offset="0.0" />
        <GradientStop Color="#008DD8FF" Offset="1.0" />
    </RadialGradientBrush>
    <Style x:Key="GlassButton" TargetType="{x:Type Button}">
        <Setter Property="BorderThickness" Value="5" />
        <Setter Property="l:GlassButton.Glow" Value="{StaticResource Glow}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border
                        Background="{Binding
                            Background,
                            RelativeSource={RelativeSource
                                TemplatedParent}}"
                        BorderBrush="{Binding
                            BorderBrush,
                             RelativeSource={RelativeSource 
                                TemplatedParent}}"
                        BorderThickness="{Binding
                            BorderThickness,
                            RelativeSource={RelativeSource 
                                TemplatedParent}}"
                        CornerRadius="10">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <Border
                                x:Name="brdGlow"
                                Background="{Binding
                                            (l:GlassButton.Glow),
                                            RelativeSource={RelativeSource
                                                TemplatedParent}}"
                                Opacity="0" BorderThickness="1" Grid.RowSpan="2"
                                VerticalAlignment="Stretch" CornerRadius="4"/>
                            <ContentPresenter Grid.RowSpan="2"/>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation
                                        Duration="0:0:0.3" FillBehavior="HoldEnd"
                                        From="0.0"
                                        Storyboard.TargetProperty="(UIElement.Opacity)"
                                        Storyboard.TargetName="brdGlow" To="1" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation
                                        Duration="0:0:0.3" FillBehavior="Stop" From="1"
                                        Storyboard.TargetProperty="(UIElement.Opacity)"
                                        Storyboard.TargetName="brdGlow" To="0.0" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.ExitActions>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
这个代码有效。如果运行该应用程序,您会注意到,当光标悬停在窗口上时,光晕淡入,然后当鼠标不再位于窗口上时,光晕淡出

这很好

但是,当我嵌套类名空间,并通过将l从MCVE更改为MCVE.NewNameSpace来反映ResourceDictionary中的更改,并运行该程序时,我在调试窗口中遇到以下错误:

System.Windows.Data错误:40:BindingExpression路径错误:'(l:GlassButton.Glow)'属性未在“对象”“按钮”(名称=“”)上找到。BindingExpression:Path=(l:GlassButton.Glow);DataItem='Button'(名称='');目标元素为“Border”(名称=“”);目标属性为“背景”(类型为“笔刷”)

为什么会这样

编辑:
当我尝试引用另一个程序集的此样式和附加属性时,也会发生这种情况,无论类名称空间是否嵌套。

我无法让它与您的代码一起运行-我总是遇到此绑定错误:)

请将您的绑定更改为:

Background="{TemplateBinding l:GlassButton.Glow}"
或指定路径属性:

Background="{Binding Path=(l:GlassButton.Glow), RelativeSource={RelativeSource TemplatedParent}}"
路径的显式说明是必要的,因为绑定的解析似乎被附加的括号{}弄糊涂了。至少其他人也有同样的问题,你可以看到

全部:

 <Border
       x:Name="brdGlow"
       Background="{TemplateBinding l:GlassButton.Glow}"
       Opacity="0" BorderThickness="1" Grid.RowSpan="2"
       VerticalAlignment="Stretch" CornerRadius="4"/>


使用此功能,我可以根据需要移动名称空间,并从xaml中另外指定附加属性。

我无法让它与您的代码一起运行-我总是遇到此绑定错误:)

请将您的绑定更改为:

Background="{TemplateBinding l:GlassButton.Glow}"
或指定路径属性:

Background="{Binding Path=(l:GlassButton.Glow), RelativeSource={RelativeSource TemplatedParent}}"
路径的显式说明是必要的,因为绑定的解析似乎被附加的括号{}弄糊涂了。至少其他人也有同样的问题,你可以看到

全部:

 <Border
       x:Name="brdGlow"
       Background="{TemplateBinding l:GlassButton.Glow}"
       Opacity="0" BorderThickness="1" Grid.RowSpan="2"
       VerticalAlignment="Stretch" CornerRadius="4"/>


使用此方法,我可以根据需要移动名称空间,并从xaml中另外指定附加属性。

xmlns:l=“clr namespace:MCVE;assembly=SomeOtherAssembly”
处理不同的程序集大小写。命名空间可能只需要重新启动VS和干净的生成。@EdPlunkett重新启动Visual Studio并尝试干净的生成不起作用。(在后一种情况下,不是前一种情况-我还没有尝试前一种情况)。
xmlns:l=“clr namespace:MCVE;assembly=SomeOtherAssembly”
处理不同的程序集情况。命名空间可能只需要重新启动VS和干净的生成。@EdPlunkett重新启动Visual Studio并尝试干净的生成不起作用。(在后一种情况下,不是前一种情况——我还没有尝试前一种情况)。谢谢遇到了另一个问题,但那是另一个问题。再次感谢。编辑:Nvm。我不敢相信您必须显式地声明
Path=(foo.bar)
,但它也起了作用。奇怪的再次感谢。TemplateBinding成功了。谢谢遇到了另一个问题,但那是另一个问题。再次感谢。编辑:Nvm。我不敢相信您必须显式地声明
Path=(foo.bar)
,但它也起了作用。奇怪的再次感谢。