うまくいけば、誰かがこれで私を助けてくれます。画像の編集に使用する Silverlight アプリを作成しています。ユーザーは、要素を含むレイヤーを含むプロジェクトを持っています。(要素は text 要素と image 要素です)。
プロジェクトを表すクラスがあります。これには ObservableCollection が含まれ、各 Layer には ObservableCollection があります。要素は抽象クラスです。Element を継承する TextElement クラスと ImageElement クラスがあります。
私の問題は、コレクション内の要素を変更しても UI が更新されないことです。私はすべてのプロパティで INotifyPropertyChanged を使用しており、コレクションで CollectionChanged をキャッチしていますが、まだうまくいきません。ObservableCollection の CollectionChanged イベントは、その要素の 1 つの更新でヒットすることはありません。
これは私がもともと持っていたコードです:
void Elements_CollectionChanged(
object sender,
System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{ this.NotifyChange("Elements"); }
これはバインディングです:
<!-- Workspace -->
<ScrollViewer Grid.Column="1" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Margin="5" DataContext="{Binding Project}">
<Canvas x:Name="Canvas" Background="LightGray" Width="{Binding Path=CanvasWidth}" Height="{Binding Path=CanvasHeight}">
<ItemsControl ItemsSource="{Binding Path=Layers, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding Path=Elements, Mode=OneWay}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Background="{Binding Path=BackgroundColor}"
Visibility="{Binding Path=Visible, Converter={StaticResource BoolConverter}}"
Opacity="{Binding Path=Opacity}"
Width="{Binding ElementName=Canvas, Path=DataContext.CanvasWidth}"
Height="{Binding ElementName=Canvas, Path=DataContext.CanvasHeight}">
</Canvas>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Canvas>
<ContentControl Content="{Binding Converter={StaticResource ElementConverter}}"
Canvas.Top="{Binding Path=Top, Mode=TwoWay}"
Canvas.Left="{Binding Path=Left, Mode=TwoWay}"
Opacity="{Binding Path=Opacity}"
MouseLeftButtonDown="ContentControl_MouseLeftButtonDown">
<interactivity:Interaction.Behaviors>
<behaviors:DragInCanvasBehavior />
</interactivity:Interaction.Behaviors>
</ContentControl>
</Canvas>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Canvas>
</ScrollViewer>
これは私のコンバーターです:
public class LayerElementToFrameworkElementConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Element element = value as Element;
if (element is ImageEditor.Client.BLL.TextElement)
{
ImageEditor.Client.BLL.TextElement txt = element as ImageEditor.Client.BLL.TextElement;
return CreateTextBlock(txt);
}
else if (element is ImageElement)
{
ImageElement imgEle = element as ImageElement;
return CreateImage(imgEle);
}
else
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
FrameworkElement frameworkElement = value as FrameworkElement;
if (frameworkElement is TextBlock)
{
TextBlock txtBlock = value as TextBlock;
return CreateTextElement(txtBlock);
}
else if (frameworkElement is Image)
{
Image img = value as Image;
return CreateImageElement(img);
}
else
return null;
}
private TextBlock CreateTextBlock(TextElement textElement)
{
TextBlock text = new TextBlock();
text.Text = textElement.Text;
text.Cursor = Cursors.Hand;
text.SetValue(Canvas.ZIndexProperty, textElement.Order);
ColorHexToBrushConverter converter = new ColorHexToBrushConverter();
SolidColorBrush brush = (SolidColorBrush)converter.Convert(textElement.Color, null, null, null);
text.Foreground = brush;
return text;
}
private TextElement CreateTextElement(TextBlock textBlock)
{
TextElement textElement = new TextElement();
textElement.Text = textBlock.Text;
textElement.Top = (double)textBlock.GetValue(Canvas.TopProperty);
textElement.Left = (double)textBlock.GetValue(Canvas.LeftProperty);
textElement.Order = (int)textBlock.GetValue(Canvas.ZIndexProperty);
ColorHexToBrushConverter converter = new ColorHexToBrushConverter();
textElement.Color = (string)converter.ConvertBack(textBlock.Foreground, null, null, null);
return textElement;
}
private Image CreateImage(ImageElement imageElement)
{
Image img = new Image();
img.Width = imageElement.Width;
img.Height = imageElement.Height;
img.Source = imageElement.Source;
img.Opacity = imageElement.Opacity;
img.Cursor = Cursors.Hand;
img.SetValue(Canvas.ZIndexProperty, imageElement.Order);
img.SetValue(Canvas.TopProperty, imageElement.Top);
img.SetValue(Canvas.LeftProperty, imageElement.Left);
return img;
}
private ImageElement CreateImageElement(Image image)
{
ImageElement imageElement = new ImageElement();
imageElement.Width = image.Width;
imageElement.Height = image.Height;
imageElement.Source = (BitmapImage)image.Source;
imageElement.Order = (int)image.GetValue(Canvas.ZIndexProperty);
return imageElement;
}
}
これはコレクションに含まれるクラスです:
public class Layer : BaseBLL
{
private int numberOfElements;
#region Properties
public int Order
{
get { return this.GetValue<int>("Order"); }
set { this.SetValue<int>("Order", value); }
}
public string BackgroundColor
{
get { return this.GetValue<string>("BackgroundColor"); }
set { this.SetValue<string>("BackgroundColor", value); }
}
public double Opacity
{
get { return this.GetValue<double>("Opacity"); }
set { this.SetValue<double>("Opacity", value); }
}
public bool Visible
{
get { return this.GetValue<bool>("Visible"); }
set { this.SetValue<bool>("Visible", value); }
}
public ObservableCollection<Element> Elements { get; set; }
public Element SelectedElement
{
get { return this.GetValue<Element>("SelectedElement"); }
set { this.SetValue<Element>("SelectedElement", value); }
}
#endregion
#region Commands
public ICommand AddTextElementCommand { get; set; }
public ICommand AddImageElementCommand { get; set; }
public ICommand DeleteElementCommand { get; set; }
public ICommand SetSelectedElementCommand { get; set; }
#endregion
#region Methods
public Layer()
:this("Untitled", 0, "#ffffff", 1, true, new ObservableCollection<Element>())
{
}
public Layer(string name, int order, string backgroundColor, double opacity, bool visible, ObservableCollection<Element> elements)
: base(name)
{
this.Order = order;
this.BackgroundColor = backgroundColor;
this.Opacity = opacity;
this.Visible = visible;
this.Elements = elements;
this.Elements.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Elements_CollectionChanged);
this.AddTextElementCommand = new DelegateCommand(AddTextElement, CanAddTextElement);
this.AddImageElementCommand = new DelegateCommand(AddImageElement, CanAddImageElement);
this.DeleteElementCommand = new DelegateCommand(DeleteElement, CanDeleteElement);
}
private void Elements_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
foreach (Element element in e.NewItems)
element.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(element_PropertyChanged);
}
void element_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
this.NotifyChange("Elements");
}
private bool CanAddTextElement(object param) { return true; }
private void AddTextElement(object param)
{
TextElement text = new TextElement();
text.Order = this.numberOfElements;
this.Elements.Add(text);
numberOfElements++;
this.SelectedElement = text;
}
private bool CanAddImageElement(object param) { return true; }
private void AddImageElement(object param)
{
var dialog = new OpenFileDialog()
{
Filter = "Image Files (*.bmp;*.jpg;*.gif;*.png;)|*.bmp;*.jpg;*.gif;*.png;",
Multiselect = false
};
bool? userClickedOK = dialog.ShowDialog();
if (userClickedOK == true)
{
string fileName = dialog.File.Name;
FileStream stream = dialog.File.OpenRead();
var imageSource = new BitmapImage();
using (FileStream fileStream = stream)
{
imageSource.SetSource(fileStream);
byte[] data = new byte[fileStream.Length];
fileStream.Read(data, 0, data.Length);
fileStream.Flush();
fileStream.Close();
}
ImageElement img = new ImageElement();
img.Name = fileName;
img.Order = this.numberOfElements;
img.Source = imageSource;
img.Height = imageSource.PixelHeight;
img.Width = imageSource.PixelWidth;
this.Elements.Add(img);
this.numberOfElements++;
this.SelectedElement = img;
}
}
private bool CanDeleteElement(object param)
{
if (this.SelectedElement != null)
return true;
else
return false;
}
private void DeleteElement(object param)
{
throw new NotImplementedException();
}
#endregion
}
これは要素の 1 つのコードです。
public class TextElement : Element
{
public string Text
{
get { return this.GetValue<string>("Text"); }
set { this.SetValue<string>("Text", value); }
}
public int FontSize
{
get { return this.GetValue<int>("FontSize"); }
set { this.SetValue<int>("FontSize", value); }
}
public bool Bold
{
get { return this.GetValue<bool>("Bold"); }
set { this.SetValue<bool>("Bold", value); }
}
public bool Italic
{
get { return this.GetValue<bool>("Italic"); }
set { this.SetValue<bool>("Italic", value); }
}
public string Color
{
get { return this.GetValue<string>("Color"); }
set { this.SetValue<string>("Color", value); }
}
public FontFamily Font
{
get { return this.GetValue<FontFamily>("Font"); }
set { this.SetValue<FontFamily>("Font", value); }
}
public TextElement()
: this("Untitled", 1, 5, 5, 0, 0, 0, "New text", 12, false, false, "#aaaaaa", new FontFamily("Arial"))
{
}
public TextElement(string name, double opacity, double top, double left, double rotateAngle, double centerX, double centerY,
string text, int fontSize, bool bold, bool italic, string color, FontFamily font)
: base(name, opacity, top, left, rotateAngle, centerX, centerY)
{
this.Text = text;
this.FontSize = fontSize;
this.Bold = bold;
this.Italic = italic;
this.Color = color;
this.Font = font;
}
}
誰かが助けてくれれば、とても感謝しています。