图像效果的控件模板中的 WPF 绑定错误

发布于 2024-10-31 12:07:15 字数 4303 浏览 6 评论 0原文

我需要一些帮助来解决从自定义按钮上的样式中收到的绑定错误。 这是 xaml:

<Style TargetType="{x:Type Controls:DropDownButton}" BasedOn="{StaticResource ButtonBaseStyle}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Controls:DropDownButton}">
                        <Border
                            x:Name="border"
                            Padding="{TemplateBinding Padding}"
                            HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                            VerticalAlignment="{TemplateBinding VerticalAlignment}">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>

                                <Grid Grid.Column="0" >
                                    <Border Padding="5">
                                        <Grid>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="*"/>
                                                <RowDefinition Height="*"/>
                                            </Grid.RowDefinitions>

                                            <Grid Grid.Row="0">
                                                <Image 
                                                    Style="{StaticResource DefaultImageStyle}" 
                                                    Source="{Binding SmallImage, Converter={StaticResource LocalizingConverter}}">
                                                    <Image.Effect>
                                                        <ShaderEffects:GrayscaleEffect 
                                                            DesaturationFactor="1" 
                                                            IsEnabled="{Binding IsEnabled, RelativeSource={RelativeSource TemplatedParent}}"/>
                                                    </Image.Effect>
                                                </Image>
                                            </Grid>
                                            <Grid Grid.Row="1">
                                                <TextBlock 
                                                    Style="{StaticResource DefaultTextHeaderStyle}" 
                                                    Text="{Binding Name, Converter={StaticResource LocalizingConverter}}"/>
                                            </Grid>
                                        </Grid>
                                    </Border>
                                </Grid>
                                <Grid 
                                    Grid.Column="1"
                                    VerticalAlignment="Stretch">
                                    <Polygon                                                                    
                                        x:Name="arrow"
                                        Points="0,0 8,0 4,5"                                                                                                                                              
                                        Fill="{TemplateBinding Foreground}"
                                        VerticalAlignment="Center"
                                        HorizontalAlignment="Center"/>
                                </Grid>
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

当应用程序启动时,会出现以下绑定错误:

System.Windows.Data 错误:2:无法 找到管理 FrameworkElement 或 目标的 FrameworkContentElement 元素。 BindingExpression:Path=IsEnabled; 数据项=空;目标元素是 '灰度效果'(哈希码=4769001); 目标属性是“IsEnabled”(类型 '布尔值')

图像效果似乎工作正常,但是绑定错误是怎么回事以及如何修复它?

I need some help with a binding error I'm getting from a style on a custom button.
Here is the xaml:

<Style TargetType="{x:Type Controls:DropDownButton}" BasedOn="{StaticResource ButtonBaseStyle}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Controls:DropDownButton}">
                        <Border
                            x:Name="border"
                            Padding="{TemplateBinding Padding}"
                            HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                            VerticalAlignment="{TemplateBinding VerticalAlignment}">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>

                                <Grid Grid.Column="0" >
                                    <Border Padding="5">
                                        <Grid>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="*"/>
                                                <RowDefinition Height="*"/>
                                            </Grid.RowDefinitions>

                                            <Grid Grid.Row="0">
                                                <Image 
                                                    Style="{StaticResource DefaultImageStyle}" 
                                                    Source="{Binding SmallImage, Converter={StaticResource LocalizingConverter}}">
                                                    <Image.Effect>
                                                        <ShaderEffects:GrayscaleEffect 
                                                            DesaturationFactor="1" 
                                                            IsEnabled="{Binding IsEnabled, RelativeSource={RelativeSource TemplatedParent}}"/>
                                                    </Image.Effect>
                                                </Image>
                                            </Grid>
                                            <Grid Grid.Row="1">
                                                <TextBlock 
                                                    Style="{StaticResource DefaultTextHeaderStyle}" 
                                                    Text="{Binding Name, Converter={StaticResource LocalizingConverter}}"/>
                                            </Grid>
                                        </Grid>
                                    </Border>
                                </Grid>
                                <Grid 
                                    Grid.Column="1"
                                    VerticalAlignment="Stretch">
                                    <Polygon                                                                    
                                        x:Name="arrow"
                                        Points="0,0 8,0 4,5"                                                                                                                                              
                                        Fill="{TemplateBinding Foreground}"
                                        VerticalAlignment="Center"
                                        HorizontalAlignment="Center"/>
                                </Grid>
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

Which when the application starts up gives me the following binding error:

System.Windows.Data Error: 2 : Cannot
find governing FrameworkElement or
FrameworkContentElement for target
element.
BindingExpression:Path=IsEnabled;
DataItem=null; target element is
'GrayscaleEffect' (HashCode=4769001);
target property is 'IsEnabled' (type
'Boolean')

The image effect appears to be working fine, but whats the deal with the binding error and how do I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我三岁 2024-11-07 12:07:15

我认为 Effect 实际上没有 DataContext,因为它不是从 FrameworkContentElement 继承的,因此它无法通过继承的 DataContext 找到模板化父级。我在 Camera 对象中看到了同样的情况,它也没有获取 DataContext。请访问:http://msdn。 microsoft.com/en-us/library/system.windows.media.effects.effect.aspx

IsEnabled 部分是否正常工作?

I don't think that Effect actually has a DataContext because it doesn't inherit from FrameworkContentElement, so it can't find the templated parent through an inherited DataContext. I've seen the same sort of thing with the Camera object, which also doesn't get DataContext. Check it out at: http://msdn.microsoft.com/en-us/library/system.windows.media.effects.effect.aspx

Is the IsEnabled part working correctly for you?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文