C# Silverlight ListBox与画布和图像绑定
我尝试创建自定义列表框:C# Silverlight ListBox与画布和图像绑定,c#,silverlight,binding,listbox,customization,C#,Silverlight,Binding,Listbox,Customization,我尝试创建自定义列表框: <ListBox x:Name="ImageList" ItemsSource="{Binding ImageControls}" Width="256" Height="256" Margin="256,0,0,0"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <Canvas/> &l
<ListBox x:Name="ImageList" ItemsSource="{Binding ImageControls}" Width="256" Height="256" Margin="256,0,0,0">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Top" Value="{Binding Top}" />
<Setter Property="Canvas.Left" Value="{Binding Left}" />
<Setter Property="ListBoxItem.Width" Value="128" />
<Setter Property="ListBoxItem.Height" Value="128" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Source="{Binding Source}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
它包含
public class ImageControl
{
public WriteableBitmap Source { get; set; }
public int Top { get; set; }
public int Left { get; set; }
}
我需要看到一些这样的想法:
结果,在从代码中添加列表元素后,我希望接收如下项目(带源的图像):
我做错了什么?
谢谢 问题似乎在于您在ImageControl类中声明源属性的方式。它使用了一个可写的位图(我没有用过)。但是,在XAML代码中,图像控件的源属性要求您指定一种字符串Uri类型。 尝试将ImageControl类中的源属性更改为字符串
public class ImageControl
{
public string Source { get; set; }
public int Top { get; set; }
public int Left { get; set; }
}
希望这个帮助有用!我向我的控件构造函数添加以下内容:
ImageControls = new ObservableCollection<ImageControl>();
(It was before)InitializeComponent();
ImageList.ItemsSource = ImageControls;
和列表框项目添加 但我需要源代码作为可写位图,而不是字符串。当我硬编码列表项(我最后写的)时,我可以使用可写位图更改其图像源,它们会更改,并且所有工作正常(但在此变体中硬编码的项)。“ImageList.ItemsSource始终为空”-ListBox的DataContext是什么?它必须是具有ImageControls属性的类。什么时候初始化ImageControl?它是在InitializeComponent之前还是之后?如果在触发Propertychanged事件之后?
ImageControls.Add(new ImageControl { Source = _bmp, Left = 0, Top = 0});
public class ImageControl
{
public string Source { get; set; }
public int Top { get; set; }
public int Left { get; set; }
}
ImageControls = new ObservableCollection<ImageControl>();
(It was before)InitializeComponent();
ImageList.ItemsSource = ImageControls;
ImageControls.Add(new ImageControl { Left = 128, Source = _bmp, Top = 0 });