Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.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_Listbox_Observablecollection_Togglebutton - Fatal编程技术网

C# 是否将ObservableCollection与切换按钮列表框进行比较?

C# 是否将ObservableCollection与切换按钮列表框进行比较?,c#,wpf,listbox,observablecollection,togglebutton,C#,Wpf,Listbox,Observablecollection,Togglebutton,在项目运行后,我让列表框填写切换按钮列表,如果我有observablecollection,我想将此observablecollection与列表框中的项目进行比较,如果observablecollection中的项目存在于列表框中,我想选中此项目(切换按钮) 我已经尝试过这样做,但我无法访问代码隐藏中的切换按钮,因为运行项目后会显示切换按钮列表 以下是我的列表框代码: <ListBox x:Name="lbname" ItemsSource="{Binding source}">

在项目运行后,我让列表框填写切换按钮列表,如果我有observablecollection,我想将此observablecollection与列表框中的项目进行比较,如果observablecollection中的项目存在于列表框中,我想选中此项目(切换按钮)

我已经尝试过这样做,但我无法访问代码隐藏中的切换按钮,因为运行项目后会显示切换按钮列表

以下是我的列表框代码:

 <ListBox x:Name="lbname" ItemsSource="{Binding source}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <ToggleButton x:Name="btnitem" Content="{Binding Name}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ListBox>

以及我的观察收集:

  IQueryable<items> query = _context.items;
  ocitems = new ObservableCollection<items>(query);
IQueryable查询=\u context.items;
ocitems=新的ObservableCollection(查询);
简言之:如何将ObservableCollection项与listbox(按钮)进行比较?如果listbox中存在项,如何使表示该项的切换按钮处于选中状态

希望这是清楚的

------------------------------------------更多细节

我有一个显示所选项目选项的列表框,该列表框由OccesselectedChoice“OccesselectedChoice”填充:


当我想更改此项目选项时,我按下编辑按钮,将显示所有可用选项的列表框都由ObservableCollection填充的窗口,请参见照片,主要问题是如何在选项窗口中选中“选项1”(绿色选项):

    <ItemsControl x:Name="icItemGroup" ItemsSource="{Binding PagedSource, ElementName=rdpChoices}" Margin="26,79,0,0" FontWeight="Bold" HorizontalAlignment="Left" Width="506" Height="210" VerticalAlignment="Top" >
        <!-- ItemsPanelTemplate -->
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Columns="4" HorizontalAlignment="left" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <!-- ItemTemplate -->
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <ToggleButton x:Name="tbtnChoices" HorizontalAlignment="Left" VerticalAlignment="Top" FontFamily="tahoma" FontSize="12" Height="45" Width="120" FontWeight="Normal" Margin="0,0,0,5" Background="#FFE8E8E8" BorderBrush="#FFC1C1C1" Foreground="#FF6A6A6A"
                              Content="{Binding ChoiceName}" TabIndex="{Binding ChoicesID}" Click="tbtnChoices_Click">
                    <ToggleButton.IsChecked>
                        <MultiBinding Converter="{StaticResource Choices}">
                          <Binding Path="ocChoice" RelativeSource="{RelativeSource AncestorType={x:Type Window}}"/>
                        </MultiBinding>
                    </ToggleButton.IsChecked>
                </ToggleButton>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>


转换器:

    public class ChoicesConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter,
                          System.Globalization.CultureInfo culture)
    {
        Choice _choice = values[0] as Choice;
        ObservableCollection<Choice> ocChoices = values[1] as ObservableCollection<Choice>;
        return ocChoices.Contains(_choice);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
                                System.Globalization.CultureInfo culture)
    {
        return null;
    }
}
    public class IsSelectedChoiceConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool _check = false;
        if (value == null)
            return false;
        Item currentItem = (Item)value;
        if (currentItem.ChoicesinItem.Count == 0)
            _check = false;
        foreach (var _choicesinItem in currentItem.ChoicesinItem)
        {
            if (currentItem.CurrentChoiceId == _choicesinItem.ChoicesId)
                _check = true;
        }
        return _check;
    }
公共类选择转换器:IMultiValueConverter
{
公共对象转换(对象[]值,类型targetType,对象参数,
System.Globalization.culture(信息文化)
{
选项_Choice=值[0]作为选项;
ObservableCollection ocChoices=作为ObservableCollection的值[1];
返回ocChoices.Contains(_choice);
}
公共对象[]转换回(对象值,类型[]目标类型,对象参数,
System.Globalization.culture(信息文化)
{
返回null;
}
}

我试图这样做,但很抱歉我仍然不清楚,请帮助解决此问题,因为它对我的项目很重要。

您应该使用多值转换器来完成此操作,即bind ToggleButton被选中,如下所示:

      <ListBox x:Name="lbname" ItemsSource="{Binding source}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <ToggleButton x:Name="btnitem" Content="{Binding Name}">
                        <ToggleButton.IsChecked>
                            <MultiBinding Converter="{StaticResource MyConverter}">
                                <Binding />
                                <Binding Path="DataContext.ObservableCollectionToCompare" RelativeSource="{RelativeSource AncestorType={x:Type Window}}"/>
                            </MultiBinding>
                        </ToggleButton.IsChecked>
                    </ToggleButton>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ListBox>

在这里,我假设您要比较的observableCollectin是视图的DataContext中的一个属性。 MyConverter是您需要创建的多值转换器。 在转换器的Convert方法中,您可以比较集合中是否存在该项,并相应地返回true或false

public class MyConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
                              System.Globalization.CultureInfo culture)
        {

            Item listItem = values[0] as Item;
            ObservableCollection<Item> collection = values[1] as ObservableCollection<Item>;

            return collection.Contains(listItem);
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
                                    System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
公共类MyConverter:IMultiValueConverter
{
公共对象转换(对象[]值,类型targetType,对象参数,
System.Globalization.culture(信息文化)
{
Item listItem=作为项的值[0];
ObservableCollection集合=作为ObservableCollection的值[1];
返回集合。包含(列表项);
}
公共对象[]转换回(对象值,类型[]目标类型,对象参数,
System.Globalization.culture(信息文化)
{
返回null;
}
}

好的,我解决了,这是我的代码:

这是转换器:

    public class ChoicesConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter,
                          System.Globalization.CultureInfo culture)
    {
        Choice _choice = values[0] as Choice;
        ObservableCollection<Choice> ocChoices = values[1] as ObservableCollection<Choice>;
        return ocChoices.Contains(_choice);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
                                System.Globalization.CultureInfo culture)
    {
        return null;
    }
}
    public class IsSelectedChoiceConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool _check = false;
        if (value == null)
            return false;
        Item currentItem = (Item)value;
        if (currentItem.ChoicesinItem.Count == 0)
            _check = false;
        foreach (var _choicesinItem in currentItem.ChoicesinItem)
        {
            if (currentItem.CurrentChoiceId == _choicesinItem.ChoicesId)
                _check = true;
        }
        return _check;
    }
和xaml代码:

                    <ToggleButton x:Name="tbtnChoices" HorizontalAlignment="Left" VerticalAlignment="Top" FontFamily="tahoma" FontSize="12" Height="45" Width="120" FontWeight="Normal" Margin="0,0,0,5" Background="#FFE8E8E8" BorderBrush="#FFC1C1C1" Foreground="#FF6A6A6A"
                              IsChecked="{Binding Path=Item,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource IsSelectedChoice}}"
                              Content="{Binding Item.CurrentChoiceName}" TabIndex="{Binding Item.CurrentChoiceId}" Click="tbtnChoices_Click">
                </ToggleButton>


现在可以工作了,感谢所有帮助我的人。

请您帮助我创建转换器。更新我的问题,了解更多详细信息,请您帮助解决此问题。