【发布时间】:2019-10-28 18:12:11
【问题描述】:
我无法解决使用 MVVM 模式在 WPF + C# 中创建的应用程序中的损坏绑定。
输出中的消息是
“System.Windows.Data 错误: 4: 找不到与引用'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.Control', AncestorLevel='1''的绑定源。BindingExpression:Path=Foreground; DataItem =null; 目标元素是 'Path' (Name=''); 目标属性是 'Fill' (type 'Brush')"。
我使用了一个已不在身边的伙伴制作的自制组件,该组件使用名为“LogDataTemplate”的DataTemplate。
我用 Google 进行了一些搜索,我发现了类似的情况,但我无法修复这个损坏的绑定:(
此代码的结果有效,圆圈和三角形按预期显示良好,但输出窗口中仍然存在此异常。
<HomeMadeComponent VerticalAlignment="Bottom"
LogListItemTemplate="{StaticResource LogDataTemplate}"
StandByBackgroundColor="#FFE6EAEF"
PostClickComponentHeight="150"
Grid.ColumnSpan="4"/>
<Window.Resources>
<DataTemplate x:Key="LogDataTemplate">
<StackPanel Orientation="Horizontal">
<ContentControl Name="Indicator"
Width="8"
Height="8"
Margin="0,0,5,0"
HorizontalAlignment="Center"
>
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="Foreground" Value="Orange" />
<Setter Property="Content" Value="{StaticResource CircleBorderOnly}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding sMessageType}" Value="Error">
<Setter Property="Foreground" Value="{StaticResource BaseRed}" />
<Setter Property="Content" Value="{StaticResource Triangle}" />
<Setter Property="LayoutTransform">
<Setter.Value>
<RotateTransform Angle="180"/>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding sMessageType}" Value="Warning">
<Setter Property="Foreground" Value="{StaticResource BaseYellow}" />
<Setter Property="Content" Value="{StaticResource Triangle}" />
</DataTrigger>
<DataTrigger Binding="{Binding sMessageType}" Value="Information">
<Setter Property="Foreground" Value="{StaticResource BaseGreen}" />
<Setter Property="Content" Value="{StaticResource CircleFull}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
<TextBlock Text="{Binding sMessage}" Style="{StaticResource DefaultLogTextBlockStyle}" />
</StackPanel>
</DataTemplate>
</Window.Resources>
在资源字典中我已经定义了我的路径
<Path x:Key="CircleFull"
x:Shared="False"
Data="M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2Z"
Stretch="Fill"
Fill="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Control}}}"
/>
<Path x:Key="CircleBorderOnly"
x:Shared="False"
Data="M12,20A8,8 0 0,1 4,12A8,8 0 0,1 12,4A8,8 0 0,1 20,12A8,8 0 0,1 12,20M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2Z"
Stretch="Fill"
Fill="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Control}}}"
/>
<Path x:Key="Triangle"
x:Shared="False"
Data="M1,21H23L12,2"
Stretch="Fill"
Fill="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Control}}}"
/>
这是我在这个网站上的第一篇文章,如果消息布局丑陋,请见谅。 感谢您的帮助:)
【问题讨论】:
-
我不确定这是否能解决问题,但您可以尝试这些更改:Fill="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentControl}}}" 并在您的 ContentControl 上设置前景,例如:
Foreground="Black" > -
感谢您的回答 lionthefox,但它不起作用,现在通过设置我的圆圈和三角形不再显示(没有任何东西),我在输出窗口“System.Windows.数据错误:4:找不到与引用'RelativeSource FindAncestor,AncestorType ='System.Windows.Controls.UserControl',AncestorLevel ='1''绑定的源。BindingExpression:Path = Foreground;DataItem = null;目标元素是'Path ' (Name=''); 目标属性是 'Fill' (类型 'Brush')"