More work on Issue #22 - added remove item button to items in the EditableListBoxControl on mouseover.

This commit is contained in:
Ben Wallis 2015-12-19 11:51:03 +00:00
parent 7d2e20e9e1
commit d014a93b70
4 changed files with 80 additions and 27 deletions

View File

@ -172,6 +172,7 @@
<DependentUpon>BlockItemControl.xaml</DependentUpon> <DependentUpon>BlockItemControl.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="UserControls\CrossButton.cs" /> <Compile Include="UserControls\CrossButton.cs" />
<Compile Include="UserControls\DesignTime\DesignTimeEditableListBoxControl.cs" />
<Compile Include="UserControls\EditableListBoxControl.xaml.cs"> <Compile Include="UserControls\EditableListBoxControl.xaml.cs">
<DependentUpon>EditableListBoxControl.xaml</DependentUpon> <DependentUpon>EditableListBoxControl.xaml</DependentUpon>
</Compile> </Compile>

View File

@ -0,0 +1,28 @@
using System.Collections.Generic;
using GalaSoft.MvvmLight.CommandWpf;
namespace Filtration.UserControls.DesignTime
{
internal class DesignTimeEditableListBoxControl : IEditableListBoxControl
{
public RelayCommand AddItemCommand { get; }
public RelayCommand<string> DeleteItemCommand { get; }
public IEnumerable<string> AutoCompleteItemsSource { get; set; }
ICollection<string> IEditableListBoxControl.ItemsSource { get; set; }
public string Label
{
get { return "Base Types"; }
set { }
}
public string AddItemText { get; set; }
public ICollection<string> ItemsSource
{
get { return new List<string> {"Test Item 1", "Blah", "Another Item"}; }
}
public string SelectedItem { get { return "Blah"; } }
}
}

View File

@ -5,10 +5,11 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:userControls="clr-namespace:Filtration.UserControls" xmlns:userControls="clr-namespace:Filtration.UserControls"
xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit" xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
xmlns:designTime="clr-namespace:Filtration.UserControls.DesignTime"
mc:Ignorable="d" mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=userControls:EditableListBoxControl}"> d:DataContext="{d:DesignInstance Type=designTime:DesignTimeEditableListBoxControl, IsDesignTimeCreatable=True}">
<Grid VerticalAlignment="Top"> <Grid VerticalAlignment="Top" Name="LayoutRoot">
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition></RowDefinition> <RowDefinition></RowDefinition>
<RowDefinition></RowDefinition> <RowDefinition></RowDefinition>
@ -21,8 +22,11 @@
Grid.Column="0" Grid.Column="0"
Grid.ColumnSpan="2" Grid.ColumnSpan="2"
ItemsSource="{Binding ItemsSource}" ItemsSource="{Binding ItemsSource}"
SelectedItem="{Binding SelectedItem}"
BorderThickness="1" BorderThickness="1"
Height="120" Height="120"
HorizontalContentAlignment="Stretch"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
VerticalAlignment="Stretch" SelectionMode="Single" x:Name="ControlListBox" BorderBrush="#CCCCCC"> VerticalAlignment="Stretch" SelectionMode="Single" x:Name="ControlListBox" BorderBrush="#CCCCCC">
<ListBox.Resources> <ListBox.Resources>
<Style TargetType="ListBoxItem"> <Style TargetType="ListBoxItem">
@ -37,6 +41,31 @@
</Style.Resources> </Style.Resources>
</Style> </Style>
</ListBox.Resources> </ListBox.Resources>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch">
<Grid.Resources>
<Style TargetType="Grid" x:Key="MouseoverStyle">
<Setter Property="Visibility" Value="Hidden" />
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsMouseOver}" Value="True">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Grid.ColumnSpan="2" Text="{Binding}" ToolTip="{Binding}" />
<Grid Grid.Column="1" Style="{StaticResource MouseoverStyle}">
<userControls:CrossButton Height="12" Command="{Binding ElementName=LayoutRoot, Path=DataContext.DeleteItemCommand}" CommandParameter="{Binding}" />
</Grid>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.InputBindings> <ListBox.InputBindings>
<KeyBinding Key="Delete" Command="{Binding Path=DeleteItemCommand}" CommandParameter="{Binding ElementName=ControlListBox, Path=SelectedValue}" /> <KeyBinding Key="Delete" Command="{Binding Path=DeleteItemCommand}" CommandParameter="{Binding ElementName=ControlListBox, Path=SelectedValue}" />
</ListBox.InputBindings> </ListBox.InputBindings>

View File

@ -2,14 +2,22 @@
using System.ComponentModel; using System.ComponentModel;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Windows; using System.Windows;
using System.Windows.Controls;
using System.Windows.Input; using System.Windows.Input;
using Filtration.Annotations; using Filtration.Annotations;
using GalaSoft.MvvmLight.CommandWpf; using GalaSoft.MvvmLight.CommandWpf;
namespace Filtration.UserControls namespace Filtration.UserControls
{ {
public partial class EditableListBoxControl : INotifyPropertyChanged public interface IEditableListBoxControl
{
RelayCommand AddItemCommand { get; }
RelayCommand<string> DeleteItemCommand { get; }
IEnumerable<string> AutoCompleteItemsSource { get; set; }
ICollection<string> ItemsSource { get; set; }
string AddItemText { get; set; }
}
public partial class EditableListBoxControl : INotifyPropertyChanged, IEditableListBoxControl
{ {
private string _addItemText; private string _addItemText;
@ -23,8 +31,8 @@ namespace Filtration.UserControls
DeleteItemCommand = new RelayCommand<string>(OnDeleteItemCommand); DeleteItemCommand = new RelayCommand<string>(OnDeleteItemCommand);
} }
public RelayCommand AddItemCommand { get; private set; } public RelayCommand AddItemCommand { get; }
public RelayCommand<string> DeleteItemCommand { get; private set; } public RelayCommand<string> DeleteItemCommand { get; }
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register( public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register(
"ItemsSource", "ItemsSource",
@ -33,13 +41,6 @@ namespace Filtration.UserControls
new FrameworkPropertyMetadata(OnItemsSourcePropertyChanged) new FrameworkPropertyMetadata(OnItemsSourcePropertyChanged)
); );
public static readonly DependencyProperty LabelProperty = DependencyProperty.Register(
"Label",
typeof (string),
typeof (EditableListBoxControl),
new FrameworkPropertyMetadata()
);
public static readonly DependencyProperty AutoCompleteItemsSourceProperty = DependencyProperty.Register( public static readonly DependencyProperty AutoCompleteItemsSourceProperty = DependencyProperty.Register(
"AutoCompleteItemsSource", "AutoCompleteItemsSource",
typeof (IEnumerable<string>), typeof (IEnumerable<string>),
@ -59,12 +60,6 @@ namespace Filtration.UserControls
set { SetValue(ItemsSourceProperty, value); } set { SetValue(ItemsSourceProperty, value); }
} }
public string Label
{
get { return (string)GetValue(LabelProperty); }
set { SetValue(LabelProperty, value); }
}
public string AddItemText public string AddItemText
{ {
get { return _addItemText; } get { return _addItemText; }
@ -100,6 +95,14 @@ namespace Filtration.UserControls
control?.OnPropertyChanged(nameof(ItemsSource)); control?.OnPropertyChanged(nameof(ItemsSource));
} }
private void AutoCompleteBox_OnPreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
AddItemCommand.Execute(null);
}
}
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator] [NotifyPropertyChangedInvocator]
@ -108,13 +111,5 @@ namespace Filtration.UserControls
var handler = PropertyChanged; var handler = PropertyChanged;
handler?.Invoke(this, new PropertyChangedEventArgs(propertyName)); handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
} }
private void AutoCompleteBox_OnPreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
AddItemCommand.Execute(null);
}
}
} }
} }