WPF自定义项控件和绑定
我已经创建了一个名为Toolbox的自定义ItemsControl。我希望能够在工具箱中显示图像-它是图表设计器的一部分 我的xaml如下所示:WPF自定义项控件和绑定,wpf,custom-controls,itemscontrol,Wpf,Custom Controls,Itemscontrol,我已经创建了一个名为Toolbox的自定义ItemsControl。我希望能够在工具箱中显示图像-它是图表设计器的一部分 我的xaml如下所示: <d:Toolbox ItemsSource="{Binding}"> <ItemsControl.ItemTemplate> <DataTemplate>
<d:Toolbox ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source="{Binding Library}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</d:Toolbox>
<d:Toolbox ItemsSource="{Binding Library}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</d:Toolbox>
和我的ViewModel:
public ObservableCollection<ElectricalLibrary> l = null;
public ObservableCollection<Image> _images = null;
public ObservableCollection<Image> Library
{
get
{
if (l == null)
{
DataAccessLayerClass dc = new DataAccessLayerClass();
dc.LoadComponents();
l = dc.Library;
foreach (ElectricalLibrary lib in l) {
Image finalImage = new Image();
finalImage.Width = 80;
BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.UriSource = new Uri(lib.url.ToString());
logo.EndInit();
finalImage.Source = logo;
MessageBoxResult result = MessageBox.Show(logo.UriSource.ToString());
_images.Add(finalImage);
}
}
return _images;
}
set { _images = value; }
}
公共ObservableCollection l=null;
公共可观测集合_images=null;
公共图书馆
{
得到
{
if(l==null)
{
DataAccessLayerClass dc=新的DataAccessLayerClass();
加载组件();
l=dc.库;
foreach(l中的电子图书馆库){
图像最终图像=新图像();
最终尺寸。宽度=80;
BitmapImage徽标=新的BitmapImage();
logo.BeginInit();
logo.UriSource=新Uri(lib.url.ToString());
logo.EndInit();
finalImage.Source=徽标;
MessageBoxResult=MessageBox.Show(logo.UriSource.ToString());
_添加(最终图像);
}
}
返回图像;
}
设置{u images=value;}
}
这是工具箱本身的资源文件:
<Style TargetType="{x:Type s:Toolbox}">
<Setter Property="SnapsToDevicePixels"
Value="true" />
<Setter Property="Focusable"
Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border BorderThickness="{TemplateBinding Border.BorderThickness}"
Padding="{TemplateBinding Control.Padding}"
BorderBrush="{TemplateBinding Border.BorderBrush}"
Background="{TemplateBinding Panel.Background}"
SnapsToDevicePixels="True">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<WrapPanel Margin="0,5,0,5"
ItemHeight="{Binding Path=DefaultItemSize.Height, RelativeSource={RelativeSource AncestorType=s:Toolbox}}"
ItemWidth="{Binding Path=DefaultItemSize.Width, RelativeSource={RelativeSource AncestorType=s:Toolbox}}" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
我只在数据库中存储图像的URL,图像存储在光盘上。我获取实体对象并创建一个图像,将其添加到图像的ObservableCollection中,并将图像控件绑定到xaml中的库中
显然,代码不起作用。但如何让它起作用呢?已正确加载包含图像的列表
谢谢您的帮助。****编辑****
对-经过一些更改后,我成功地使代码正常工作。更改库属性以从数据库对象返回Uri集的列表-确保确实返回了一些内容。我为您的属性提出以下建议(如果您需要一个更健壮的属性,而该属性不会在每次有get时重新蚀刻,请更改它)
public ObservableCollection<Uri> Library
{
get
{
OberservableCollection<Uri> library = new OberservableCollection<Uri>();
DataAccessLayerClass dc = new DataAccessLayerClass();
dc.LoadComponents();
foreach (ElectricalLibrary lib in dc.Library)
{
library.Add(new Uri(lib.url.ToString()));
}
return library;
}
公共可观测集合库
{
得到
{
OberservableCollection库=新建OberservableCollection();
DataAccessLayerClass dc=新的DataAccessLayerClass();
加载组件();
foreach(直流图书馆中的电子图书馆图书馆)
{
Add(新Uri(lib.url.ToString());
}
返回库;
}
然后,您的XAML可以如下所示:
<d:Toolbox ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source="{Binding Library}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</d:Toolbox>
<d:Toolbox ItemsSource="{Binding Library}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</d:Toolbox>
这样做对我来说很好
原文因历史原因留下
您似乎正在将图像绑定到整个集合。如果它只是您需要的图像的单个列表,则工具箱的ItemsSource应该是库集合,其中图像作为DataTemplate的一部分(我现在无法测试此项,因此它可能不是100%准确的代码)
****编辑****
正确-我已设法在一些更改后使代码正常工作。更改库属性以从数据库对象返回Uri集的列表-确保您实际返回了一些内容。我建议您的属性使用以下内容(如果您需要一个更健壮的属性,而该属性不会在每次出现get时都重新蚀刻,请更改它
public ObservableCollection<Uri> Library
{
get
{
OberservableCollection<Uri> library = new OberservableCollection<Uri>();
DataAccessLayerClass dc = new DataAccessLayerClass();
dc.LoadComponents();
foreach (ElectricalLibrary lib in dc.Library)
{
library.Add(new Uri(lib.url.ToString()));
}
return library;
}
公共可观测集合库
{
得到
{
OberservableCollection库=新建OberservableCollection();
DataAccessLayerClass dc=新的DataAccessLayerClass();
加载组件();
foreach(直流图书馆中的电子图书馆图书馆)
{
Add(新Uri(lib.url.ToString());
}
返回库;
}
然后,您的XAML可以如下所示:
<d:Toolbox ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source="{Binding Library}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</d:Toolbox>
<d:Toolbox ItemsSource="{Binding Library}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</d:Toolbox>
这样做对我来说很好
原文因历史原因留下
您似乎正在将图像绑定到整个集合。如果它只是您需要的图像的单个列表,则工具箱的ItemsSource应该是库集合,其中图像作为DataTemplate的一部分(我现在无法测试此项,因此它可能不是100%准确的代码)
试试这个:
<d:Toolbox ItemsSource="{Binding Library}">
<Image Source="{Binding}"/>
试试这个:
<d:Toolbox ItemsSource="{Binding Library}">
<Image Source="{Binding}"/>
谢谢你的回答。我试过了,但图像不可见。@Leom Burke:即使这是一个答案,你一分钟前也给出了同样的答案。我不值得。谢谢你的回答。我试过了,但图像不可见。@Leom Burke:即使这是一个答案,你一分钟前也给出了同样的答案。我不知道注意这一点。@Veer:如果我没有帐户,如何接受答案?@Cloverness:我应该告诉你创建一个帐户吗?:)旁注-你会在任何时候初始化图像吗,因为它不是在那个时间完成的getter@Veer-我会改过自新:)@Leom Burke-我不确定我是否理解您确实,如果我使用此条件,它将无法初始化。但是从getter中删除if后,它也不起作用。