我有 ItemsControl,其中 ItemsPanel 是 Canvas。数据模板是一个包裹在边框中的画布,其中包含额外的矩形。这是 ItemsControl XAML:
<ItemsControl x:Name="Items" ItemsSource="{Binding TileResources, ElementName=ResourceWindow}" >
<ItemsControl.ItemsPanel >
<ItemsPanelTemplate>
<Canvas Background="SkyBlue" Width="500" Height="500" IsItemsHost="True" Loaded="Canvas_Loaded_1" MouseDown="Canvas_MouseDown" MouseUp="Canvas_MouseUp" MouseMove="Canvas_MouseMove"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:ResourceBorderControl x:Name="ResRect" BorderThickness="2"
MouseDown="selectionBox_MouseDown" MouseUp="selectionBox_MouseUp" >
<Canvas Width="{Binding Width, ElementName=ResRect}" Height="{Binding Height, ElementName=ResRect}" Background="White" Opacity="0.1">
<Rectangle Stroke="Red" StrokeThickness="2"
Canvas.Left="{Binding CollisionX, ElementName=ResRect}"
Canvas.Top="{Binding CollisionY, ElementName=ResRect}"
Width="{Binding CollisionWidth, ElementName=ResRect}"
Height="{Binding CollisionHeight, ElementName=ResRect}"
/>
</Canvas>
</local:ResourceBorderControl>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsSelected}" Value="true">
<Setter Property="BorderBrush" Value="Purple" TargetName="ResRect"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsSelected}" Value="false">
<Setter Property="BorderBrush" Value="SeaGreen" TargetName="ResRect"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Left" Value="{Binding X}" />
<Setter Property="Canvas.Top" Value="{Binding Y}" />
<Setter Property="FrameworkElement.Width" Value="{Binding Width}" />
<Setter Property="FrameworkElement.Height" Value="{Binding Height}" />
<Setter Property="local:ResourceBorderControl.CollisionX" Value="{Binding CollideX}" />
<Setter Property="local:ResourceBorderControl.CollisionY" Value="{Binding CollideY}" />
<Setter Property="local:ResourceBorderControl.CollisionWidth" Value="{Binding CollideWidth}" />
<Setter Property="local:ResourceBorderControl.CollisionHeight" Value="{Binding CollideHeight}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
如您所见,我正在使用自定义边框控件 - ResourceBorderControl。它有 4 个附加属性来描述内部矩形: CollisionX CollisionY CollisionWidth CollisionHeight
如果我只是在 XAML 中对附加属性(CollisionX 等)进行硬编码,它就可以正常工作 - Canvas 中的 Rectangle 按我想要的方式呈现。
我希望使用样式(或任何其他方式,如果可能的话)设置它,因为我需要用来自 ItemsSource 的渲染对象的值填充它。
它就是行不通。我什至无法在 WPF 树可视化工具中调试它——当我尝试查看 ResourceBorderControl 对象时,我看不到碰撞值。当我运行该程序时,我可以看到带画布的边框渲染(我可以看到不透明度),但里面什么都没有。我想这些值在被样式设置器设置后会丢失。
所以我的问题是——我在这里做错了吗?或者只是 WPF 不允许使用样式设置自定义附加值,我应该使用另一种方法?如果是后者,我还能怎么做?
编辑:这是绑定到 ItemsSource 的 ObservableCollection 中实例的类的代码
public class ResourceModelBase : IResourceModel
{
private String _name, _defaultLayer;
private double _x, _y, _width, _height, _collideX, _collideY, _collideWidth, _collideHeight;
private bool _isSelected;
public ResourceModelBase(double x, double y, double width, double height)
{
X = x;
Y = y;
Width = width;
Height = height;
CollideX = 1;
CollideY = 1;
CollideWidth = 100;
CollideHeight = 100;
IsSelected = false;
Name = "RES_" + width.ToString() + height.ToString();
}
public void Select()
{
IsSelected = true;
}
public void Deselect()
{
IsSelected = false;
}
public new String ToString()
{
return Name;
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public String Name
{
get
{
return _name;
}
set
{
if (value != _name)
{
_name = value;
NotifyPropertyChanged();
}
}
}
public String DefaultLayer
{
get
{
return _defaultLayer;
}
set
{
if (value != _defaultLayer)
{
_defaultLayer = value;
NotifyPropertyChanged();
}
}
}
public double X
{
get
{
return _x;
}
set
{
if (value != _x)
{
_x = value;
NotifyPropertyChanged();
}
}
}
public double Y
{
get
{
return _y;
}
set
{
if (value != _y)
{
_y = value;
NotifyPropertyChanged();
}
}
}
public double Width
{
get
{
return _width;
}
set
{
if (value != _width)
{
_width = value;
NotifyPropertyChanged();
}
}
}
public double Height
{
get
{
return _height;
}
set
{
if (value != _height)
{
_height = value;
NotifyPropertyChanged();
}
}
}
public bool IsSelected
{
get
{
return _isSelected;
}
set
{
if (value != _isSelected)
{
_isSelected = value;
NotifyPropertyChanged();
}
}
}
public double CollideX
{
get
{
return _collideX;
}
set
{
if (value != _collideX)
{
_collideX = value;
NotifyPropertyChanged();
}
}
}
public double CollideY
{
get
{
return _collideY;
}
set
{
if (value != _collideY)
{
_collideY = value;
NotifyPropertyChanged();
}
}
}
public double CollideWidth
{
get
{
return _collideWidth;
}
set
{
if (value != _collideWidth)
{
_collideWidth = value;
NotifyPropertyChanged();
}
}
}
public double CollideHeight
{
get
{
return _collideHeight;
}
set
{
if (value != _collideHeight)
{
_collideHeight = value;
NotifyPropertyChanged();
}
}
}
}
如您所见 - 主要是公共属性,以及 INotifyPropertyChanged 接口的实现。
附加属性定义:
public static readonly DependencyProperty CollisionX = DependencyProperty.RegisterAttached(
"CollisionX",
typeof(double),
typeof(ResourceBorderControl),
new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender)
);
public static void SetCollisionX(UIElement element, object value)
{
element.SetValue(CollisionX, (double)value);
}
public static double GetCollisionX(UIElement element)
{
return (double)element.GetValue(CollisionX);
}