Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对ObservableCollection执行选择_C#_Wpf_Linq_Data Binding_Observablecollection - Fatal编程技术网

C# 对ObservableCollection执行选择

C# 对ObservableCollection执行选择,c#,wpf,linq,data-binding,observablecollection,C#,Wpf,Linq,Data Binding,Observablecollection,同样,我可以使用集合的ICollectionView上的filter属性从ObservableCollection中筛选项目,是否也可以对集合执行选择(与使用Linq的方式相同) 例如,假设我有一个Food对象的列表,其中没有Selected属性,但我在一个单独的列表中维护选择的食品。理想情况下,我希望执行如下选择: <ListBox ItemsSource="{Binding FoodView}"> <ListBox.ItemTemplate> <Data

同样,我可以使用集合的ICollectionView上的filter属性从ObservableCollection中筛选项目,是否也可以对集合执行选择(与使用Linq的方式相同)

例如,假设我有一个
Food
对象的列表,其中没有
Selected
属性,但我在一个单独的列表中维护选择的食品。理想情况下,我希望执行如下选择:

<ListBox ItemsSource="{Binding FoodView}">
<ListBox.ItemTemplate>
    <DataTemplate>
        <CheckBox IsChecked="{Binding Selected}">
            <TextBlock Text="{Binding Food.Name}"/>
        </CheckBox>
    </DataTemplate>
</ListBox.ItemTemplate>
view.Select(f=>returnnew{Food=f,Selected=selectedFood.Contains(f)})

然后,我可以绑定到从该选择返回的项目,如下所示:

<ListBox ItemsSource="{Binding FoodView}">
<ListBox.ItemTemplate>
    <DataTemplate>
        <CheckBox IsChecked="{Binding Selected}">
            <TextBlock Text="{Binding Food.Name}"/>
        </CheckBox>
    </DataTemplate>
</ListBox.ItemTemplate>

我目前正在通过维护单独的ObservableCollections并根据需要更新它们来实现我想要的结果,但我想知道是否有更优雅的解决方案


干杯

我不确定这是否是您想要的,但我创建了一个
IMultiValueConverter
,它接受两个绑定,第一个是当前数据对象,第二个是所选项目的列表:

<ListBox ItemsSource="{Binding FoodItems}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">

                <CheckBox>
                    <CheckBox.IsChecked>
                        <MultiBinding Converter="{StaticResource ListExistsConverter}" Mode="OneWay">
                            <Binding/>
                            <Binding Path="DataContext.SelectedFoodItems" RelativeSource="{RelativeSource AncestorType=UserControl}"/>
                        </MultiBinding>
                    </CheckBox.IsChecked>
                </CheckBox>

                <TextBox Text="{Binding Name}" Width="50" Height="20"/>

            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

转换器:

class ListExistsConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values == null) return null;
        var item = values[0] as Food;
        var list = values[1] as List<Food>;

        return list.Contains(item);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
类ListExistsConverter:IMultiValueConverter
{
公共对象转换(对象[]值,类型targetType,对象参数,CultureInfo区域性)
{
if(value==null)返回null;
var项目=作为食品的值[0];
var list=作为列表的值[1];
返回列表。包含(项目);
}
公共对象[]转换回(对象值,类型[]目标类型,对象参数,CultureInfo区域性)
{
抛出新的NotImplementedException();
}
}


使用的资源包括一个
可观察收集的FoodItems
和一个
列表SelectedFoodItems
。也许不是最优雅的,但应该可以根据需要在SelectedItem列表之间轻松切换。

您不能制作一个具有Food和IsSelected属性的对象包装SelectableFood吗?然后,您可以使用该包装类型的可观察集合,而不是类型食品的可观察集合。是否要使用交互(读/写)复选框或仅显示(只读)实现INotifyPropertyChanged的通用SelectableItem可以帮助general@Lithium鲁福爵士,我现在正在做这件事,但是列表需要显示在多个地方,每个地方都有不同的选择。我正在寻找的东西,将让我维护一个项目的主要列表,然后什么已经被选中的单独列表。谢谢你的快速回复,顺便说一句!