C# 将可观察集合中的可观察集合绑定到Listbox项

C# 将可观察集合中的可观察集合绑定到Listbox项,c#,wpf,listbox,C#,Wpf,Listbox,我有一个列表框,其中包含4个图像,每个图像显示一个工具提示,我想将代码重构为一种样式。问题是listbox位于datagrid中,itemsource设置为collectionView <CollectionViewSource x:Key="ItemsViewSource" Source="{Binding PicklistItemCollection}" /> 还有数据网格 <DataGrid x:Name ="PicklistItemDataGrid" x:Fiel

我有一个列表框,其中包含4个图像,每个图像显示一个工具提示,我想将代码重构为一种样式。问题是listbox位于datagrid中,itemsource设置为collectionView

<CollectionViewSource x:Key="ItemsViewSource" Source="{Binding PicklistItemCollection}" />

还有数据网格

<DataGrid x:Name ="PicklistItemDataGrid" x:FieldModifier="public" AutoGenerateColumns="False"
          SelectionMode="Single" SelectionUnit="FullRow" VerticalScrollBarVisibility="Auto"
          MaxHeight="492" ItemsSource="{Binding Source={StaticResource ItemsViewSource}}"
          DataContext="{Binding Path=this, Mode=TwoWay}">

现在,图像源需要绑定到ImageUrl数组[0]、[1]、[2]、[3]

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Auto" >
<ListBox.ItemsPanel>
    <ItemsPanelTemplate>
        <WrapPanel IsItemsHost="True" />
    </ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBoxItem>
    <Image ToolTipService.ShowDuration="60000" Source="{Binding Images[0].ImageUrl}" Width="240" Height="240" Stretch="Uniform" Margin="0,8,5,8">
        <Image.ToolTip>

            <ToolTip  MaxWidth="600" MaxHeight="580" 
                      DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}">
                <Border BorderBrush="Black" BorderThickness="1" Margin="5,7,5,5">
                    <Image Source="{Binding Source}" Stretch="Fill" />
                </Border>
            </ToolTip>
        </Image.ToolTip>
    </Image>
</ListBoxItem>
<ListBoxItem>
    <Image ToolTipService.ShowDuration="60000" Source="{Binding Images[1].ImageUrl}" Width="240" Height="240" Stretch="Uniform" Margin="0,8,5,8">
        <Image.ToolTip>
            <ToolTip  MaxWidth="600" MaxHeight="580" 
                      DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}">
                <Border BorderBrush="Black" BorderThickness="1" Margin="5,7,5,5">
                    <Image Source="{Binding Source}" Stretch="Fill" />
                </Border>
            </ToolTip>
        </Image.ToolTip>
    </Image>
</ListBoxItem>

.....and so on... for each array item

等等对于每个数组项
我的视图模型和类:

class PicklistItemViewModel : INotifyPropertyChanged
{
    private ObservableCollection<PicklistItem> _picklistItemCollection;

    public PicklistItemViewModel()
    {



        List<Rootobject> rootObjectList = CARestRequest.ChannelAdvisorRestCall(CARestRequest.url.product, CARestRequest.GetProductParameterList(CARestRequest.GetSkuString()));
        ObservableCollection<PicklistItem> picklist = new ObservableCollection<PicklistItem>();


        foreach (var rol in rootObjectList)
        {
            foreach (var r in rol.value)
            {
                picklist.Add(AddPicklistItem(r));
            }

        }
        PicklistItemCollection = picklist;

    }
}

public class PicklistItem:INotifyPropertyChanged
{
    private string itemcode;
    private string location;
    private int qty;
    private string title;
    public bool RowActive { get; set; } = false;

    //Here is old images list and the getter and setters for the private 
    //variables i deleted to save space
    //public List<PicklistImageUrl> Images { get; set; }

    private ObservableCollection<PicklistImageUrl> _imageCollection;
    public ObservableCollection<PicklistImageUrl> Images
    {
        get { return _imageCollection; }
        set { _imageCollection = value; }
    }
}
public class PicklistImageUrl
{
    public string ImageUrl { get; set; }
}
类PicklistItemViewModel:INotifyPropertyChanged
{
私有ObservableCollection\u picklistItemCollection;
公共PicklistItemViewModel()
{
List rootObjectList=CARestRequest.ChannelAdvisorRestCall(CARestRequest.url.product,CARestRequest.GetProductParameterList(CARestRequest.GetSkusString());
ObservableCollection选取列表=新的ObservableCollection();
foreach(rootObjectList中的变量rol)
{
foreach(以账面价值表示的var r)
{
添加(AddPicklistItem(r));
}
}
PicklistItemCollection=picklist;
}
}
公共类PicklistItem:INotifyPropertyChanged
{
私有字符串项代码;
私有字符串位置;
私人整数数量;
私有字符串标题;
public bool RowActive{get;set;}=false;
//这是旧图像列表以及私有图像的getter和setter
//为了节省空间,我删除了变量
//公共列表图像{get;set;}
私人可观测采集(imageCollection);;
公众可观测采集图像
{
获取{return\u imageCollection;}
设置{u imageCollection=value;}
}
}
公共类PicklistImageUrl
{
公共字符串ImageUrl{get;set;}
}

因此,在将Images属性设置为可观察集合后,如果我使用数组索引[0,1,2,3,…],它们仍然会填充。如何将Images集合绑定到listbox项中的image控件,并最好为ListboxItem创建一个样式,以便除了Images数组索引之外,没有[x]个listboxitems具有相同代码:

<ListBox ItemsSource="{Binding Images}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Image ToolTipService.ShowDuration="60000" Source="{Binding ImageUrl}" Width="240" Height="240" Stretch="Uniform" Margin="0,8,5,8">
                <Image.ToolTip>
                    <ToolTip  MaxWidth="600" MaxHeight="580" 
                DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}">
                        <Border BorderBrush="Black" BorderThickness="1" Margin="5,7,5,5">
                            <Image Source="{Binding Source}" Stretch="Fill" />
                        </Border>
                    </ToolTip>
                </Image.ToolTip>
            </Image>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

是的。项目模板忘记了那个