我有一个“板”,我正在展示框架元素。问题是,如果我克隆“BlockContainer”对象并创建我的frameworkElements(System.Windows.Controls.Image)的新实例,那么我会看到多个对象。
但是,如果我只创建一个“BlockContainer”的新实例,然后将带有FrameworkElements的列表从ReferenceList设置为新对象,则只显示新的Items。
但我需要一个参考。如果图像发生变化,我只想更新referenceList。
(所有代码都被修剪)
public abstract class HtMultipleDragSelectionBoardCoordinates : FrameworkContentElement, INotifyPropertyChanged
{
// ##############################################################################################################################
// Properties
// ##############################################################################################################################
/// <summary>
/// The x - coordinate of the location of the Item.
/// </summary>
public double X
{
get { return _X; }
set
{
_X = Math.Round(value, 1);
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("X"));
}
}
private double _X;
/// <summary>
/// The y - coordinate of the location of the Item.
/// </summary>
public double Y
{
get { return _Y; }
set
{
_Y = Math.Round(value, 1);
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Y"));
}
}
private double _Y;
/// <summary>
/// The rotation of the Item.
/// </summary>
public double Rotation
{
get { return _Rotation; }
set
{
if (value > 360.0)
value -= 360.0;
else if (value < 0.0)
value += 360.0;
_Rotation = Math.Round(value, 1);
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Rotation"));
}
}
private double _Rotation;
// ##############################################################################################################################
// EventHandler
// ##############################################################################################################################
/// <summary>
/// The <see cref="PropertyChanged"/> EventHandler.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Raise a the Event.
/// </summary>
/// <param name="propertyName"></param>
public void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class HtMultipleDragSelectionBoardItem : HtMultipleDragSelectionBoardCoordinates
{
// ##############################################################################################################################
// Konstruktor
// ##############################################################################################################################
/// <summary>
/// This ViewModel is required to build items for the <see cref="HTMultipleDragSelectionBoard"/>.
/// </summary>
/// <param name="xCoordinate">The x - coordinate of the location of the ViewModel.</param>
/// <param name="yCoordinate">The y - coordinate of the location of the ViewModel.</param>
/// <param name="rotation">The rotation of the ViewModel.</param>
/// <param name="frameworkElement">The <see cref="FrameworkElement"/> to be displayed.</param>
public HtMultipleDragSelectionBoardItem(double xCoordinate, double yCoordinate, double rotation, FrameworkElement frameworkElement)
{
this.X = xCoordinate;
this.Y = yCoordinate;
this.Rotation = rotation;
this.FrameworkElement = frameworkElement;
}
/// <summary>
/// This ViewModel is required to build items for the <see cref="HTMultipleDragSelectionBoard"/>.
/// </summary>
public HtMultipleDragSelectionBoardItem()
{
}
// ##############################################################################################################################
// Properties
// ##############################################################################################################################
/// <summary>
/// The <see cref="FrameworkElement"/> to be displayed.
/// </summary>
public FrameworkElement FrameworkElement
{
get { return _FrameworkElement; }
set
{
if (Equals(_FrameworkElement, value)) return;
_FrameworkElement = value;
OnPropertyChanged("FrameworkElement");
}
}
private FrameworkElement _FrameworkElement;
}
public class HtMultipleDragSelectionBoardItemContainer : HtMultipleDragSelectionBoardCoordinates
{
// ##############################################################################################################################
// Properties
// ##############################################################################################################################
/// <summary>
/// The Collection with all <see cref="HtMultipleDragSelectionBoardItem"/>s.
/// </summary>
public ObservableCollection<HtMultipleDragSelectionBoardItem> Items
{
get { return _Items; }
set
{
if (_Items == value) return;
_Items = value;
OnPropertyChanged("Items");
if(_Items != null)
Items.CollectionChanged -= ItemsOnCollectionChanged;
Items.CollectionChanged += ItemsOnCollectionChanged;
}
}
private void ItemsOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
{
OnPropertyChanged("MinX");
OnPropertyChanged("MaxX");
OnPropertyChanged("MinY");
OnPropertyChanged("MaxY");
OnPropertyChanged("BorderWidth");
OnPropertyChanged("BorderHeight");
OnPropertyChanged("Width");
OnPropertyChanged("Height");
}
private ObservableCollection<HtMultipleDragSelectionBoardItem> _Items;
}
public class BlockContainer : HtMultipleDragSelectionBoardItemContainer
{
/// <summary>
/// This is the Reference Block!
/// </summary>
public bool IsReferenceBlock
{
get { return (bool)GetValue(IsReferenceBlockProperty); }
set { SetValue(IsReferenceBlockProperty, value); }
}
/// <summary>
/// This is the Reference Block!
/// </summary>
public static readonly DependencyProperty IsReferenceBlockProperty = DependencyProperty.Register("IsReferenceBlock", typeof(bool), typeof(BlockContainer), new PropertyMetadata(false));
public BlockContainer Clone()
{
BlockContainer clone = new BlockContainer
{
X = this.X,
Y = this.Y,
Rotation = this.Rotation,
IsReferenceBlock = this.IsReferenceBlock
};
foreach (HtMultipleDragSelectionBoardItem item in Items)
{
Image image = item.FrameworkElement as Image;
if (image != null)
{
Image newImage = new Image
{
Source = image.Source,
Width = item.FrameworkElement.Width,
Height = item.FrameworkElement.Height
};
FrameworkElement frameworkElement = newImage;
frameworkElement.Width = newImage.Width;
frameworkElement.Height = newImage.Height;
HtMultipleDragSelectionBoardItem newItem = item.Clone();
newItem.FrameworkElement = frameworkElement;
clone.Items.Add(newItem);
}
}
return clone;
}
}
/// <summary>
/// The internal reference Block
/// </summary>
private readonly BlockContainer _ReferenceBlock = new BlockContainer();
BlockContainer clone = (BlockContainer)((BlockContainer)ListBox_Blocks.SelectedItem).Clone();
BlockContainer clone = new BlockContainer();
clone.Items = _ReferenceBlock.Items;