我读过很多关于这类错误的答案,但没有什么可以解决我的问题。
我的程序运行时出现了这个错误,但我会知道为什么以及如何修复?还是已知的臭虫?特纳克斯请你帮忙
出现错误。偏移。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')外观相同错误。偏移。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}");
}
}
}外观ViewModel:
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);
}
}
}
}发布于 2019-10-13 22:17:04
这也许能帮到你。
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>我希望这是有帮助的。
https://stackoverflow.com/questions/58366650
复制相似问题