【问题标题】:WPF / MVVM : Need help to fix a broken binding with ContentControl + DataTemplateWPF / MVVM:需要帮助来修复与 ContentControl + DataTemplate 的损坏绑定
【发布时间】: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')"

标签: c# wpf xaml mvvm


【解决方案1】:

我找到了解决问题的方法:)

我为要使用 DataTrigger 显示的每个形状编写了一个 DataTemplate(我将其设置为 ContentTemplate),并且在输出窗口中没有更多错误消息:

<Window.Resources>
        <DataTemplate x:Key="LogDataTemplate">
            <StackPanel Orientation="Horizontal">

                <ContentControl Name="Indicator" Width="8" Height="8" Margin="0,0,5,0" HorizontalAlignment="Center"
                                >
                    <ContentControl.Resources>
                        <DataTemplate x:Key="TemplateError">
                            <ContentControl Content="{StaticResource Triangle}" Foreground="{StaticResource BaseRed}"/>
                        </DataTemplate>
                        <DataTemplate x:Key="TemplateWarning">
                            <ContentControl Content="{StaticResource Triangle}" Foreground="{StaticResource BaseYellow}"/>
                        </DataTemplate>
                        <DataTemplate x:Key="TemplateInformation">
                            <ContentControl Content="{StaticResource CircleFull}" Foreground="{StaticResource BaseGreen}"/>
                        </DataTemplate>
                        <DataTemplate x:Key="TemplateDefault">
                            <ContentControl Content="{StaticResource CircleBorderOnly}" Foreground="Gray"/>
                        </DataTemplate>
                        <DataTemplate x:Key="TemplateNull"/>
                    </ContentControl.Resources>

                    <ContentControl.Style>
                        <Style TargetType="{x:Type ContentControl}">
                            <Setter Property="ContentTemplate" Value="{StaticResource TemplateDefault}" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding sMessageType}" Value="Error">
                                    <Setter Property="ContentTemplate" Value="{StaticResource TemplateError}" />
                                    <Setter Property="LayoutTransform">
                                        <Setter.Value>
                                            <RotateTransform Angle="180"/>
                                        </Setter.Value>
                                    </Setter>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding sMessageType}" Value="Information">
                                    <Setter Property="ContentTemplate" Value="{StaticResource TemplateInformation}" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding sMessageType}" Value="Warning">
                                    <Setter Property="ContentTemplate" Value="{StaticResource TemplateWarning}" />
                                </DataTrigger>
                                <!-- Used to avoid to display a gray circle when nothing to display -->
                                <DataTrigger Binding="{Binding sMessageType}" Value="{x:Null}">
                                    <Setter Property="ContentTemplate" Value="{StaticResource TemplateNull}" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </ContentControl.Style>
                </ContentControl>

                <TextBlock Text="{Binding sMessage}" Style="{StaticResource DefaultLogTextBlockStyle}" />
            </StackPanel>
        </DataTemplate>
</Window.Resources>

【讨论】:

    猜你喜欢
    • 2015-11-10
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    相关资源
    最近更新 更多