我正在尝试创建和使用自定义 Canvas 。这是 XAML (MyCanvas.xaml):
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:Core="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Namespace="clr-namespace:MyNamepace" xmlns:Properties="clr-namespace:MyNamepace.Properties" Core:Class="MyNamepace.MyCanvas">
<Canvas.Resources>
<Namespace:ImagesConverter Core:Key="ImagesConverter"/>
</Canvas.Resources>
<Image Source="{Binding Source={Core:Static Properties:Resources.Background}, Converter={StaticResource ImagesConverter}}" Stretch="Fill"/>
</Canvas>
这是代码声明 (MyCanvas.xaml.cs):
public partial class MyCanvas : Canvas
当我尝试像这样使用它时:
<Namespace:MyCanvas Core:Name="Layout" Loaded="OnLoaded">
<Namespace:MyUserControl Core:Name="Control1" Namespace:MyCanvas.Left="50" MyProperty="50">
<Namespace:MyCanvas.Top>
<MultiBinding Converter="{StaticResource MathConverter}" ConverterParameter="(x - y) / 2">
<Binding ElementName="Layout" Path="ActualHeight"/>
<Binding Path="ActualHeight" RelativeSource="{RelativeSource Self}"/>
</MultiBinding>
</Namespace:MyCanvas.Top>
</Namespace:MyUserControl>
<Namespace:MyUserControl Core:Name="Control2" Namespace:MyCanvas.Left="744" Namespace:MyCanvas.Top="42" MyProperty="150"/>
</Namespace:MyCanvas>
我得到两个不同的错误:
The property "Content" can only be set once. ==> Isn't it inheriting Canvas?!?!?!
The member "Top" is not recognized or is not accessible. ==> Isn't it inheriting Canvas again?!?!?! The member "Left" is not recognized or is not accessible. ==> Isn't it inheriting Canvas again?!?!?!
编辑:这是我目前所拥有的...仍然出现“内容”已设置错误!
MyCanvas.xaml
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:Core="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Namespace="clr-namespace:MyNamespace" xmlns:Properties="clr-namespace:MyNamespace.Properties" Core:Class="MyNamespace.MyCanvas">
<Canvas.Background>
<ImageBrush ImageSource="{Binding Source={Core:Static Properties:Resources.Background}, Converter={StaticResource ImagesConverter}}" Stretch="Fill"/>
</Canvas.Background>
<Canvas.Resources>
<Namespace:ImagesConverter Core:Key="ImagesConverter"/>
</Canvas.Resources>
</Canvas>
MyCanvas.xaml.cs
public class MyCanvas : Canvas
{
// ...
}
主窗口.xaml
<Namespace:MyCanvas Core:Name="MyCanvas" Loaded="OnLoaded">
<Namespace:MyUserControl ...
<Namespace:MyUserControl ...
<Namespace:MyUserControl ...
</Namespace:MyCanvas>
最佳答案
Left
和 Top
是 attached properties .这意味着它们不会被您的类继承。
您需要更改用户控件声明以改为使用 Canvas.Left
和 Canvas.Top
:
<Namespace:MyUserControl Core:Name="Control2" Canvas.Left="744" Canvas.Top="42"
MyProperty="150"/>
content的问题是你设置了两次,就像报错信息说的那样。
- 在
MyCanvas.xaml
中,您将其设置为Image
。 - 使用它时,您将其设置为您的用户控件。
要修复它,您需要将一个ItemsControl
添加到MyCanvas
并将其声明为表示内容的控件:
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:Core="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Namespace="clr-namespace:MyNamepace" xmlns:Properties="clr-namespace:MyNamepace.Properties" Core:Class="MyNamepace.MyCanvas">
<Canvas.Resources>
<Namespace:ImagesConverter Core:Key="ImagesConverter"/>
</Canvas.Resources>
<Image Source="{Binding Source={Core:Static Properties:Resources.Background}, Converter={StaticResource ImagesConverter}}" Stretch="Fill"/>
<ItemsControl Content="{Binding Path=LocalContent, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Namespace:MyCanvas}}}" />
</Canvas>
在你的类文件中:
[ContentProperty("LocalContent")]
public partial class MyCanvas : Canvas
{
public static readonly DependencyProperty LocalContentProperty =
DependencyProperty.Register(
"LocalContent",
typeof(UIElementCollection),
typeof(MyCanvas ),
new PropertyMetadata(default(UIElementCollection)));
}
关于c# - 自定义 WPF/XAML Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14752084/