Silverlight의 캔버스에있는 모든 이미지와 텍스트 "레이어"를 표시하는 목록 상자가 필요합니다. 목록 상자를 보려고하거나 요소를 추가 할 때 목록 상자를 볼 때 현재 코드가 충돌합니다. 나는 이유를 알 수 없다. 누군가가 이것으로 올바른 방향으로 나를 가리킬 수 있습니까?Silverlight에서 프로그래밍 방식으로 목록 상자에 바인딩하기
XML - 당신이 당신의 Grid 요소에 경로 속성을 사용하여 바인딩 때문에
<Grid DataContext="{Binding Path=Project}">
...
...
<TextBlock Name="textBlock1" Text="Layers" Margin="18,16,0,0" />
<StackPanel Grid.Row="1" Grid.RowSpan="2" Grid.ColumnSpan="2">
<ListBox ItemsSource="{Binding Path=Elements}" Height="175" Name="listBox1" Width="172"/>
</StackPanel>
</Grid>
Project.cs는
//List of elements
private ObservableCollection<FrameworkElement> elements;
public ObservableCollection<FrameworkElement> Elements
{
get { return elements; }
set
{
elements = value;
NotifyPropertyChanged("Elements");
}
}
// An example of how an element is added to the Elements collection
// There are also image elements added similarly
private void AddTextElement(object param)
{
TextBlock textBlock = new TextBlock();
textBlock.Text = "New Text";
textBlock.Foreground = new SolidColorBrush(Colors.Gray);
textBlock.FontSize = 25;
textBlock.FontFamily = new FontFamily("Arial");
textBlock.Cursor = Cursors.Hand;
textBlock.Tag = null;
this.Elements.Add(textBlock);
numberOfElements++;
this.SelectedElement = textBlock;
this.selectedTextElement = textBlock;
}
private void AddImageElement(object param)
{
bool? gotImage;
string fileName;
BitmapImage imageSource = GetImageFromLocalMachine(out gotImage, out fileName);
if (gotImage == true)
{
Image image = new Image();
OrderElements(image);
image.Name = fileName;
image.Source = imageSource;
image.Height = imageSource.PixelHeight;
image.Width = imageSource.PixelWidth;
image.MaxHeight = imageSource.PixelHeight;
image.MaxWidth = imageSource.PixelWidth;
image.Cursor = Cursors.Hand;
image.Tag = null;
AddDraggingBehavior(image);
image.MouseLeftButtonUp += element_MouseLeftButtonUp;
this.Elements.Add(image);
numberOfElements++;
this.SelectedElement = image;
this.SelectedImageElement = image;
}
}
. 이 문제를 해결하기 위해 어쨌든 알고 있습니까? 텍스트 요소의 일부 텍스트를 표시하지만 이미지의 파일 이름 만 표시하려고합니다. – Steve
나는 이것을 어떻게 수행했는지 보여주기 위해 위의 대답을 업데이트했다. –