自定义DropDown CheckBox遍历子级 [英] Custom DropDown CheckBox iterate through children
问题描述
大家好,
我一直在使用有人提供的自定义控件.这是一个下拉复选框,其中组合框项目模板设置为复选框.我想知道如何以编程方式遍历其中的项目并手动检查或取消选中项目.
我可以手动修改控件的itemssource,但是即使绑定状态为双向,它也不会更新复选框标记.
它绑定到节点列表:
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:
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; }
}
<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.
任何帮助将不胜感激
Jib
更新
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>
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>
推荐答案
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.
This sorted the binding out.
这篇关于自定义DropDown CheckBox遍历子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!