0

i have listbox taht fill with list of toggle button in after run the project, if i have observablecollection and i want to compare this ObservableCollection with items in list box where if the item in ObservableCollection exist in listbox i want to make this item (toggle button) checked,

i have tryed to do that but i cant access to toggle button in code behind, becouse the list of toggle buttons show after run the project.

here's my listbox code :

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

and my observableCollection :

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

In short : How can i compare ObservableCollection items with listbox (Buttons) and if item exist in listbox make the Toggle button that represent the item is checked?

hope this clear.

------------------------------------------ More Detail

i have this list box that show choices for selected item, this listBox filled by 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>

when i want to change this item choices, i press the edit button,and will show me windows that has listbox filled by ObservableCollection for all available choices, see the photo, the main problem how to make 'choices 1' lock checked (green one) in choices windows:

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>

Converter :

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

i tried to make this, but sorry still there something unclear for me, please help to solved this issue because it's important for my project.

4

2 回答 2

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

和xml代码:

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

现在可以了,感谢任何人帮助我。

于 2013-09-21T12:16:53.810 回答
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 或 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;
        }
    }
于 2013-09-08T12:08:56.783 回答