diff options
author | Eric Maupin <ermaup@microsoft.com> | 2018-09-18 00:19:10 +0300 |
---|---|---|
committer | Eric Maupin <ermaup@microsoft.com> | 2018-09-18 00:28:04 +0300 |
commit | 06c76b699655fd789a9c2c9ef3610d028b07cda9 (patch) | |
tree | 5f0005e59c60346ceb093811ce4e984a9cd08bed | |
parent | 02f43060012c53246f0bb5cd848b96e45bdc59ef (diff) |
[Win] Support unconstrained values in combobox
- Ensures values submit correctly when out of bounds but unconstrained.
- Ensures pressing enter submits such values.
3 files changed, 65 insertions, 1 deletions
diff --git a/Xamarin.PropertyEditing.Windows/ComboBoxEx.cs b/Xamarin.PropertyEditing.Windows/ComboBoxEx.cs new file mode 100644 index 0000000..d4369ce --- /dev/null +++ b/Xamarin.PropertyEditing.Windows/ComboBoxEx.cs @@ -0,0 +1,60 @@ +using System.Windows; +using System.Windows.Controls; +using System.Windows.Input; + +namespace Xamarin.PropertyEditing.Windows +{ + internal class ComboBoxEx + : ComboBox + { + static ComboBoxEx () + { + DefaultStyleKeyProperty.OverrideMetadata (typeof (ComboBoxEx), new FrameworkPropertyMetadata (typeof (ComboBoxEx))); + } + + public static readonly DependencyProperty EnableSubmitProperty = DependencyProperty.Register ( + "EnableSubmit", typeof (bool), typeof (ComboBoxEx), new PropertyMetadata (true)); + + public bool EnableSubmit + { + get { return (bool)GetValue (EnableSubmitProperty); } + set { SetValue (EnableSubmitProperty, value); } + } + + protected override void OnSelectionChanged (SelectionChangedEventArgs e) + { + base.OnSelectionChanged (e); + Submit(); + } + + protected override void OnKeyDown (KeyEventArgs e) + { + if (e.Key == Key.Enter) { + Submit (); + e.Handled = true; + } + + base.OnKeyDown (e); + } + + protected override void OnLostKeyboardFocus (KeyboardFocusChangedEventArgs e) + { + base.OnLostKeyboardFocus (e); + Submit (); + } + + protected virtual void OnSubmit () + { + var expression = GetBindingExpression (TextProperty); + expression?.UpdateSource (); + } + + private void Submit () + { + if (!EnableSubmit) + return; + + OnSubmit(); + } + } +} diff --git a/Xamarin.PropertyEditing.Windows/Themes/Resources.xaml b/Xamarin.PropertyEditing.Windows/Themes/Resources.xaml index 75b5b92..3f61759 100644 --- a/Xamarin.PropertyEditing.Windows/Themes/Resources.xaml +++ b/Xamarin.PropertyEditing.Windows/Themes/Resources.xaml @@ -65,7 +65,7 @@ <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:EnumEditorControl}"> - <ComboBox x:Name="ComboBox" ItemsSource="{Binding PossibleValues,Mode=OneTime}" SelectedItem="{Binding ValueName}" IsEditable="{Binding IsConstrainedToPredefined,Mode=OneTime,Converter={StaticResource OppositeBoolConverter}}" VerticalContentAlignment="Center" AutomationProperties.Name="{Binding Property.Name,Mode=OneTime}" /> + <local:ComboBoxEx x:Name="ComboBox" ItemsSource="{Binding PossibleValues,Mode=OneTime}" IsEditable="{Binding IsConstrainedToPredefined,Mode=OneTime,Converter={StaticResource OppositeBoolConverter}}" Text="{Binding ValueName,Mode=TwoWay,UpdateSourceTrigger=Explicit}" VerticalContentAlignment="Center" AutomationProperties.Name="{Binding Property.Name,Mode=OneTime}" /> </ControlTemplate> </Setter.Value> </Setter> @@ -2864,4 +2864,7 @@ </Trigger> </Style.Triggers> </Style> + + <Style TargetType="{x:Type local:ComboBoxEx}" BasedOn="{StaticResource {x:Type ComboBox}}"> + </Style> </ResourceDictionary> diff --git a/Xamarin.PropertyEditing.Windows/Xamarin.PropertyEditing.Windows.csproj b/Xamarin.PropertyEditing.Windows/Xamarin.PropertyEditing.Windows.csproj index 29b4c67..0f9563f 100644 --- a/Xamarin.PropertyEditing.Windows/Xamarin.PropertyEditing.Windows.csproj +++ b/Xamarin.PropertyEditing.Windows/Xamarin.PropertyEditing.Windows.csproj @@ -71,6 +71,7 @@ <DependentUpon>CollectionEditorWindow.xaml</DependentUpon> </Compile> <Compile Include="CombinablePredefinedValuesEditorControl.cs" /> + <Compile Include="ComboBoxEx.cs" /> <Compile Include="CommonBrushToBrushConverter.cs" /> <Compile Include="ByteToPercentageConverter.cs" /> <Compile Include="ChoiceControl.cs" /> |