【发布时间】:2021-02-13 22:59:57
【问题描述】:
XAML/视图
<ItemsControl ItemsSource="{Binding _shapes}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Width="800" Height="600"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Ellipse Width="{Binding width}" Height="{Binding height}" Fill="{Binding brush}">
<Ellipse.RenderTransform>
<TranslateTransform X="{Binding x}" Y="{Binding y}"/>
</Ellipse.RenderTransform>
</Ellipse>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
主视图模型
public ObservableCollection<GalaxyObjectShape> _shapes { get; set; }
银河物体形状
public class GalaxyObjectShape
{
public string name;
public System.Windows.Media.Brush brush;
public double x;
public double y;
public double vx;
public double vy;
public double height;
public double width;
}
谁能帮我看看为什么这不起作用?在输出中出现这些错误,我已经尝试了所有我能找到的关于这个错误的方法:
System.Windows.Data Error: 40 : BindingExpression path error: 'width' property not found on 'object' ''GalaxyObjectShape' (HashCode=407659)'. BindingExpression:Path=width; DataItem='GalaxyObjectShape' (HashCode=407659); target element is 'Ellipse' (Name=''); target property is 'Width' (type 'Double')
System.Windows.Data Error: 40 : BindingExpression path error: 'height' property not found on 'object' ''GalaxyObjectShape' (HashCode=407659)'. BindingExpression:Path=height; DataItem='GalaxyObjectShape' (HashCode=407659); target element is 'Ellipse' (Name=''); target property is 'Height' (type 'Double')
System.Windows.Data Error: 40 : BindingExpression path error: 'brush' property not found on 'object' ''GalaxyObjectShape' (HashCode=407659)'. BindingExpression:Path=brush; DataItem='GalaxyObjectShape' (HashCode=407659); target element is 'Ellipse' (Name=''); target property is 'Fill' (type 'Brush')
System.Windows.Data Error: 40 : BindingExpression path error: 'x' property not found on 'object' ''GalaxyObjectShape' (HashCode=407659)'. BindingExpression:Path=x; DataItem='GalaxyObjectShape' (HashCode=407659); target element is 'TranslateTransform' (HashCode=3668935); target property is 'X' (type 'Double')
System.Windows.Data Error: 40 : BindingExpression path error: 'y' property not found on 'object' ''GalaxyObjectShape' (HashCode=407659)'. BindingExpression:Path=y; DataItem='GalaxyObjectShape' (HashCode=407659); target element is 'TranslateTransform' (HashCode=3668935); target property is 'Y' (type 'Double')
我的其他绑定如:
Title="{Binding mainWindowTitle}"
为我的窗口工作(mainWindowTitle 是同一个 ViewModel 中的一个简单字符串)
【问题讨论】: