比较ObservableCollection和Toggle Button listBox?

时间:2013-09-08 10:11:40

标签: c# wpf listbox observablecollection togglebutton

我有listbox taht填写运行项目后的切换按钮列表,如果我有observablecollection并且我想将此ObservableCollection与列表框中的项目进行比较,如果ObservableCollection中的项目存在于listbox中我想要生成此项目(切换按钮)已选中,

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

这是我的列表框代码:

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

和我的observableCollection:

  IQueryable<items> query = _context.items;
  ocitems = new ObservableCollection<items>(query);

简而言之:如何将ObservableCollection项目与列表框(按钮)进行比较,如果列表框中存在项目,则会检查表示项目的切换按钮吗?

希望这清楚。

------------------------------------------更多详情

我有这个列表框显示所选项目的选项,此列表框由ObservableCollection“ocSelectedChoice”填充:

enter image description here

    <ListBox x:Name="lbChoices" ItemsSource="{Binding ocSelectedChoice}" DisplayMemberPath="ChoiceName" HorizontalAlignment="Left" Height="165" VerticalAlignment="Top" Width="186" Margin="567,50,0,0" BorderBrush="#FFC1C1C1" Background="#FFE3E3E3" SelectionMode="Extended">
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="Height" Value="30"/>
                <Setter Property="Background" Value="#FFc4d0df"/>
                <Setter Property="BorderBrush" Value="#FFC1C1C1"/>
                <Setter Property="BorderThickness" Value="0.8"/>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>

当我想要更改此项目选项时,我按下编辑按钮,并向我显示具有由ObservableCollection填充的列表框的所有可用选项的窗口,请参阅照片,主要问题如何使'选项1'锁定检查(绿色之一)在选择窗口中:

enter image description here

    <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;
    }
}

我试图做到这一点,但遗憾的是还有一些我不清楚的事情,请帮助解决这个问题,因为这对我的项目很重要。

2 个答案:

答案 0 :(得分:0)

你应该使用多值转换器来做到这一点,即绑定ToggleButton IsChecked,如下所示:

      <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>

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

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;
        }
    }

答案 1 :(得分:0)

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

这是转换器:

    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>

它现在正在工作,感谢任何人帮助我。