Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I've been using a custom control that someone kindly provided. It's a dropdown checkbox with the combobox item template set to checkboxes. I want to know how I can programmatically iterate through the items in it and manually check or uncheck items.

I can manually amend the itemssource of the control, but it doesn't update the checkbox mark even though the binding states twoway.

It's bound to a list of Nodes:

C#
public class ObservableNodeList : ObservableCollection<Node>
    {
       public ObservableNodeList()
       {

       }
       public override string ToString()
       {
           StringBuilder outString = new StringBuilder();
           foreach (Node s in this.Items)
           {
               if (s.IsSelected == true)
               {
                   outString.Append(s.Title);
                   outString.Append(", ");
               }
           }

           return outString.ToString();
       }
    }

    public class Node
    {
        public Node(string n) { Title = n; }
        public string Title { get; set; }
        public bool IsSelected { get; set; }
    }


HTML
<UserControl x:Class="Stock_Control.Controls.DropDownCheckBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" Height="22" Width="120"
             d:DesignHeight="300" d:DesignWidth="300">
    <ComboBox
        x:Name="CheckableCombo"
        SnapsToDevicePixels="True"
        OverridesDefaultStyle="True"
        ScrollViewer.HorizontalScrollBarVisibility="Auto"
        ScrollViewer.VerticalScrollBarVisibility="Auto"
        ScrollViewer.CanContentScroll="True"
        IsSynchronizedWithCurrentItem="True"
        MinWidth="120"
        MinHeight="20"
        ItemsSource="{Binding ElementName=UserControl, Path=ItemsSource}"
        DataContext="{Binding ElementName=UserControl, Path=DataContext}"
        SelectedItem="{Binding ElementName=UserControl, Path=SelectedItems}"
        >
        <ComboBox.ItemTemplate>
            <HierarchicalDataTemplate>
                <CheckBox Content="{Binding Title}" x:Name="chkBox"
                          IsChecked="{Binding Path=IsSelected, Mode=TwoWay}"
                          Tag="{RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}"
                          Click="CheckBox_Click"
                          />
            </HierarchicalDataTemplate>
        </ComboBox.ItemTemplate>

        <ComboBox.Template>
            <ControlTemplate TargetType="ComboBox">
                <Grid>
                    <ToggleButton 
                        Name="ToggleButton" 
                        Template="{StaticResource ComboBoxToggleButton}" 
                        Grid.Column="2" 
                        Focusable="false"
                        IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                        ClickMode="Press">
                    </ToggleButton>
                    <ContentPresenter
                        x:Name="Presenter"
                        IsHitTestVisible="False" 
                        Margin="3,3,23,3"
                        VerticalAlignment="Center"
                        HorizontalAlignment="Left">
                        <ContentPresenter.Content>
                            <TextBlock TextTrimming="CharacterEllipsis"
                                       Text="{Binding Path=Text,Mode=TwoWay,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" />
                        </ContentPresenter.Content>
                    </ContentPresenter>
                    <!-- Content="{TemplateBinding SelectionBoxItem}" -->
                    <TextBox x:Name="EditableTextBox"
                        Style="{x:Null}" 
                        Template="{StaticResource ComboBoxTextBox}" 
                        HorizontalAlignment="Left" 
                        VerticalAlignment="Center" 
                        Margin="3,3,23,3"
                        Focusable="True" 
                        removed="Transparent"
                        Visibility="Hidden"
                        IsReadOnly="{TemplateBinding IsReadOnly}"/>
                    <Popup 
                        Name="Popup"
                        Placement="Bottom"
                        IsOpen="{TemplateBinding IsDropDownOpen}"
                        AllowsTransparency="True" 
                        Focusable="False"
                        PopupAnimation="Slide">
                        <Grid 
                                  Name="DropDown"
                                  SnapsToDevicePixels="True"                
                                  MinWidth="{TemplateBinding ActualWidth}"
                                  MaxHeight="{TemplateBinding MaxDropDownHeight}">
                            <Border 
                                    x:Name="DropDownBorder"
                                    removed="{StaticResource WindowBackgroundBrush}"
                                    BorderThickness="1"/>
                            <!--BorderBrush="{StaticResource SolidColorBrush}"-->
                            <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True" DataContext="{Binding}">
                                <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                            </ScrollViewer>
                        </Grid>
                    </Popup>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="HasItems" Value="false">
                        <Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                    </Trigger>
                    <Trigger Property="IsGrouping" Value="true">
                        <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                    </Trigger>
                    <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
                        <Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4"/>
                        <Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0"/>
                    </Trigger>
                    <Trigger Property="IsEditable"
                   Value="true">
                        <Setter Property="IsTabStop" Value="false"/>
                        <Setter TargetName="EditableTextBox" Property="Visibility"    Value="Visible"/>
                        <Setter TargetName="Presenter" Property="Visibility" Value="Hidden"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </ComboBox.Template>
    </ComboBox>
</UserControl>


I've removed most Resources, Styles and triggers.
Any help is greatly appreciated

Jib

Update

I've noticed problems with the twoway binding too and added this but it hasn't helped. It seems to update the text displayed, but doesn't check the items.

<pre lang="HTML">IsChecked="{Binding Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=Explicit}"</pre>
Posted
Updated 17-Feb-12 3:07am
v2
Comments
BobJanova 17-Feb-12 9:31am    
You will probably need to ask the person that wrote the control, as the most likely reason is that you are using it in a way they didn't expect and there is a bug in their binding code.

1 solution

C#
public class Node : INotifyPropertyChanged 
    {      
        public Node(string n) { Title = n; }
        public string Title { get; set; }
        //public bool IsSelected { get; set; }

        private bool isSelected;
        public bool IsSelected
        {
            get { return isSelected; }
            set
            {
                if (isSelected != value)
                {
                    isSelected = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("IsSelected"));
                    }
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged; 

    }


This sorted the binding out.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900