Browse Source

CheckListBox: still working on it

import/raw
brianlagunas_cp 15 years ago
parent
commit
209e9da17f
  1. 64
      Main/Source/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBox.cs
  2. 67
      Main/Source/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBoxItem.cs
  3. 10
      Main/Source/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Themes/Generic.xaml

64
Main/Source/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBox.cs

@ -19,11 +19,18 @@ namespace Microsoft.Windows.Controls
{ {
public class CheckListBox : MultiSelector public class CheckListBox : MultiSelector
{ {
private bool _surpressSelectionChanged;
static CheckListBox() static CheckListBox()
{ {
DefaultStyleKeyProperty.OverrideMetadata(typeof(CheckListBox), new FrameworkPropertyMetadata(typeof(CheckListBox))); DefaultStyleKeyProperty.OverrideMetadata(typeof(CheckListBox), new FrameworkPropertyMetadata(typeof(CheckListBox)));
} }
public CheckListBox()
{
}
#region Properties #region Properties
public static readonly DependencyProperty CheckedMemberPathProperty = DependencyProperty.Register("CheckedMemberPath", typeof(string), typeof(CheckListBox), new UIPropertyMetadata(null)); public static readonly DependencyProperty CheckedMemberPathProperty = DependencyProperty.Register("CheckedMemberPath", typeof(string), typeof(CheckListBox), new UIPropertyMetadata(null));
@ -41,49 +48,6 @@ namespace Microsoft.Windows.Controls
set { SetValue(CommandProperty, value); } set { SetValue(CommandProperty, value); }
} }
//public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(CheckListBox), new UIPropertyMetadata(null, new PropertyChangedCallback(OnItemsSourceChanged), new CoerceValueCallback(OnCoerceItemsSource)));
//private static object OnCoerceItemsSource(DependencyObject o, object value)
//{
// CheckListBox checkListBox = o as CheckListBox;
// if (checkListBox != null)
// return checkListBox.OnCoerceItemsSource((IEnumerable)value);
// else
// return value;
//}
//private static void OnItemsSourceChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
//{
// CheckListBox checkListBox = o as CheckListBox;
// if (checkListBox != null)
// checkListBox.OnItemsSourceChanged((IEnumerable)e.OldValue, (IEnumerable)e.NewValue);
//}
//protected virtual IEnumerable OnCoerceItemsSource(IEnumerable value)
//{
// // TODO: Keep the proposed value within the desired range.
// return value;
//}
//protected virtual void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
//{
// // TODO: Add your property changed side-effects. Descendants can override as well.
//}
//public IEnumerable ItemsSource
//{
// // IMPORTANT: To maintain parity between setting a property in XAML and procedural code, do not touch the getter and setter inside this dependency property!
// get
// {
// return (IEnumerable)GetValue(ItemsSourceProperty);
// }
// set
// {
// SetValue(ItemsSourceProperty, value);
// }
//}
#endregion //Properties #endregion //Properties
#region Base Class Overrides #region Base Class Overrides
@ -100,16 +64,24 @@ namespace Microsoft.Windows.Controls
protected override void PrepareContainerForItemOverride(DependencyObject element, object item) protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{ {
var checkListBoxItem = element as FrameworkElement; _surpressSelectionChanged = true;
var checkListBoxItem = element as FrameworkElement;
if (!String.IsNullOrEmpty(CheckedMemberPath)) if (!String.IsNullOrEmpty(CheckedMemberPath))
{ {
Binding isCheckedBinding = new Binding(CheckedMemberPath); Binding isCheckedBinding = new Binding(CheckedMemberPath);
isCheckedBinding.Source = item; isCheckedBinding.Source = item;
checkListBoxItem.SetBinding(CheckListBoxItem.IsCheckedProperty, isCheckedBinding); checkListBoxItem.SetBinding(CheckListBoxItem.IsSelectedProperty, isCheckedBinding);
} }
base.PrepareContainerForItemOverride(element, item); base.PrepareContainerForItemOverride(element, item);
_surpressSelectionChanged = false;
}
protected override void OnSelectionChanged(SelectionChangedEventArgs e)
{
if (!_surpressSelectionChanged)
base.OnSelectionChanged(e);
} }
#endregion //Base Class Overrides #endregion //Base Class Overrides

67
Main/Source/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBoxItem.cs

@ -11,6 +11,7 @@ using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using System.Windows.Navigation; using System.Windows.Navigation;
using System.Windows.Shapes; using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
namespace Microsoft.Windows.Controls namespace Microsoft.Windows.Controls
{ {
@ -21,11 +22,67 @@ namespace Microsoft.Windows.Controls
DefaultStyleKeyProperty.OverrideMetadata(typeof(CheckListBoxItem), new FrameworkPropertyMetadata(typeof(CheckListBoxItem))); DefaultStyleKeyProperty.OverrideMetadata(typeof(CheckListBoxItem), new FrameworkPropertyMetadata(typeof(CheckListBoxItem)));
} }
public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool), typeof(CheckListBoxItem), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); #region Properties
public bool IsChecked
public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(CheckListBoxItem), new UIPropertyMetadata(false, OnIsSelectedChanged));
public bool IsSelected
{
get { return (bool)GetValue(IsSelectedProperty); }
set { SetValue(IsSelectedProperty, value); }
}
private static void OnIsSelectedChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
CheckListBoxItem checkListBoxItem = o as CheckListBoxItem;
if (checkListBoxItem != null)
checkListBoxItem.OnIsSelectedChanged((bool)e.OldValue, (bool)e.NewValue);
}
protected virtual void OnIsSelectedChanged(bool oldValue, bool newValue)
{
if (newValue)
OnSelected(new RoutedEventArgs(Selector.SelectedEvent, this));
else
OnUnselected(new RoutedEventArgs(Selector.UnselectedEvent, this));
}
#endregion //Properties
#region Events
public static readonly RoutedEvent SelectedEvent = Selector.SelectedEvent.AddOwner(typeof(CheckListBoxItem));
public event RoutedEventHandler Selected
{
add { base.AddHandler(SelectedEvent, value); }
remove { base.RemoveHandler(SelectedEvent, value); }
}
public static readonly RoutedEvent UnselectedEvent = Selector.UnselectedEvent.AddOwner(typeof(CheckListBoxItem));
public event RoutedEventHandler Unselected
{
add { base.AddHandler(UnselectedEvent, value); }
remove { base.RemoveHandler(UnselectedEvent, value); }
}
#endregion
#region Methods
protected virtual void OnSelected(RoutedEventArgs e)
{
this.OnIsSelectedChanged(true, e);
}
protected virtual void OnUnselected(RoutedEventArgs e)
{ {
get { return (bool)GetValue(IsCheckedProperty); } this.OnIsSelectedChanged(false, e);
set { SetValue(IsCheckedProperty, value); } }
}
private void OnIsSelectedChanged(bool newValue, RoutedEventArgs e)
{
base.RaiseEvent(e);
}
#endregion //Methods
} }
} }

10
Main/Source/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Themes/Generic.xaml

@ -34,7 +34,7 @@
<ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*" /> <ColumnDefinition Width="*" />
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<CheckBox Name="PART_CheckBox" IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" <CheckBox Name="PART_CheckBox" IsChecked="{Binding IsSelected, RelativeSource={RelativeSource TemplatedParent}}"
Command="{Binding Command, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CheckListBox}}" Command="{Binding Command, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CheckListBox}}"
CommandParameter="{Binding}" VerticalAlignment="Center" Focusable="False" Margin="1,1,5,1"/> CommandParameter="{Binding}" VerticalAlignment="Center" Focusable="False" Margin="1,1,5,1"/>
<Border Grid.Column="1"> <Border Grid.Column="1">
@ -43,16 +43,16 @@
</Grid> </Grid>
</Border> </Border>
<ControlTemplate.Triggers> <!--<ControlTemplate.Triggers>
<!--<Trigger Property="IsChecked" Value="true"> <Trigger Property="IsChecked" Value="true">
<Setter TargetName="_background" Property="Background" Value="Blue"/> <Setter TargetName="_background" Property="Background" Value="Blue"/>
<Setter Property="Foreground" Value="White"/> <Setter Property="Foreground" Value="White"/>
</Trigger>--> </Trigger>
<Trigger Property="IsMouseOver" Value="true"> <Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="_background" Property="Background" Value="Blue"/> <Setter TargetName="_background" Property="Background" Value="Blue"/>
<Setter Property="Foreground" Value="White"/> <Setter Property="Foreground" Value="White"/>
</Trigger> </Trigger>
</ControlTemplate.Triggers> </ControlTemplate.Triggers>-->
</ControlTemplate> </ControlTemplate>
</Setter.Value> </Setter.Value>
</Setter> </Setter>

Loading…
Cancel
Save