找不到管理 FrameworkElement

Posted

技术标签:

【中文标题】找不到管理 FrameworkElement【英文标题】:Cannot find governing FrameworkElement 【发布时间】:2020-02-10 11:32:03 【问题描述】:

我已经阅读了很多关于此类错误的答案,但没有什么可以解决我的问题。

我的程序运行时出现此错误,但我知道为什么以及如何解决?或其已知的错误?感谢您的帮助

Appearance.Offset.X 错误:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. 
BindingExpression:Path=Appearance.Offset.X; DataItem=null; target element is 'TranslateTransform' (HashCode=62775401); target property is 'X' (type 'Double')

Appearance.Offset.Y 出现同样的错误


我的 xaml 文件

<UserControl 
                            :
                            :
    mc:Ignorable="d" d:DesignHeight="150" d:DesignWidth="70"
    x:Name="usercontrol"  ToolTip="Binding ToolTip"
    VerticalAlignment = "Top" HorizontalAlignment = "Left" ClipToBounds="True"
    cal:Message.Attach="[Event MouseEnter] = [Action MouseEnterInUC($eventArgs)];
                        [Event MouseLeftButtonDown] = [Action MouseLeftButtonDownOnUC($source, $mousepoint, $eventArgs)];
                        [Event MouseLeftButtonUp] = [Action MouseLeftButtonUp()]">

    <Grid x:Name="Switch"  Width="Binding Path=Layout.Width" Height="Binding Path=Layout.Height" >
        <Image x:Name="Pushed" Source="Binding Appearance.PushedImage, Mode=TwoWay"
               HorizontalAlignment="Center" VerticalAlignment="Center" >
                                :
                                :
        </Image>

        <Image x:Name="Normal" Source="Binding Appearance.Image"
               HorizontalAlignment="Center" VerticalAlignment="Center" >
                                :
                                :
        </Image>
        <TextBlock Text="Binding Appearance.GlyphText" Foreground="Binding Appearance.TextColor, Converter=StaticResource MyconverterColorToSolidColorBrush"
                   HorizontalAlignment="Binding Appearance.SelectedHAlignType" VerticalAlignment="Binding Appearance.SelectedVAlignType" 
                   >

            <TextBlock.Style>
                <Style TargetType="TextBlock">
                    <Style.Triggers>
                        <DataTrigger Binding="Binding Appearance.IndexImage" Value="1">
                            <Setter Property="RenderTransform" >
                                <Setter.Value>
Error here----->                    <TranslateTransform X="Binding Appearance.Offset.X" Y="Binding Appearance.Offset.Y"  />
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>

        <Path StrokeThickness="Binding Appearance.GlyphThickness" Stroke="Binding Appearance.GlyphColor, Converter=StaticResource MyconverterColorToSolidColorBrush" >
                            :
                            :
        </Path>
    </Grid>
</UserControl>

ViewModel 文件:

namespace Cockpit.Core.Plugins.Plugins

    [Identity(GroupName = "PushButton", Name ="", Type = typeof(PushButton_ViewModel))]
    [DataContract(Name = "Cockpit.Core.Plugins.Plugins.PushButton_ViewModel")]
    public class PushButton_ViewModel : PropertyChangedBase, IPluginModel 
    
        private readonly IEventAggregator eventAggregator;

        [DataMember] public PushButtonAppearanceViewModel Appearance  get; private set; 

        public PushButton_ViewModel(IEventAggregator eventAggregator, params object[] settings)
        
            Appearance = new PushButtonAppearanceViewModel(settings);

            NameUC = (string)settings[2];

            this.eventAggregator = eventAggregator;
            System.Diagnostics.Debug.WriteLine($"entree push NameUC this");
        
    


外观视图模型:

namespace Cockpit.Core.Plugins.Plugins.Properties

    [DataContract]
    public class PushButtonAppearanceViewModel : PropertyChangedBase, IPluginProperty

    
        public string NameUC  get; set; 
        public PushButtonAppearanceViewModel(params object[] settings)
        
                        :
                        :

            Name = "Appearance";
        


        public string Name  get; set; 



        private TextFormat textformat;
        public TextFormat TextFormat
        
            get => textformat;

            set
            
                textformat = value;
                NotifyOfPropertyChange(() => TextFormat);
            
        

        private string textPushOffset;
        public string TextPushOffset
        
            get => textPushOffset;

            set
            
                textPushOffset = value;
                var a = value.Split(',').Select(i => Convert.ToInt32(i)).ToArray();
                Offset = new Point(a[0], a[1]);
                NotifyOfPropertyChange(() => TextPushOffset);
            
        

        private Point offset;
        public Point Offset
        
            get => offset;

            set
            
                offset = value;
                NotifyOfPropertyChange(() => Offset);
            
        

    

【问题讨论】:

【参考方案1】:

这可能会对你有所帮助。

Datatemplate binding spam Output window with error: Cannot find governing FrameworkElemen

公认的答案是转换不存在于可视树或逻辑树中,因此它无法继承完成绑定所需的数据上下文。

推荐的解决方案是将转换定义为 TextBlock 的资源:

<TextBlock ...>
    <TextBlock.Resources>
        <TranslateTransform x:Key="MyTransform" X="Binding Appearance.Offset.X" Y="Binding Appearance.Offset.Y"  />
    </TextBlock.Resources>
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Style.Triggers>
                <DataTrigger Binding="Binding Appearance.IndexImage" Value="1">
                    <Setter Property="RenderTransform" >
                        <Setter.Value>
                            <StaticResource ResourceKey="MyTransform" />
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

我希望这会有所帮助。

【讨论】:

无赖!好的,我看看能不能找出问题的根源。 我将在这个周末进行测试...我现在无法访问我的应用...感谢您的帮助 如果真的不能继承DataContext,那你怎么还能写Binding Appearance.Offset.X而且还能用呢? @corentin,对于这个项目,我使用的是 caliburn 和 ninject,所以每个视图都链接到你可以在我的项目中看到的视图模型 github.com/Frenchy62620/Cockpit-master

以上是关于找不到管理 FrameworkElement的主要内容,如果未能解决你的问题,请参考以下文章

手机里的文件管理器找不到文件

加了块新硬盘,怎么找不到

设备管理器里找不到网卡怎么办

找不到 SQL Server 配置管理器

找不到窗口资源管理器[关闭]

Spring JPA - 找不到实体管理器工厂,为啥?