Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented ComboBox #1070

Closed
wants to merge 8 commits into from
Closed

Implemented ComboBox #1070

wants to merge 8 commits into from

Conversation

jp2masa
Copy link
Contributor

@jp2masa jp2masa commented Jul 15, 2017

Closes #205

@codecov
Copy link

codecov bot commented Jul 21, 2017

Codecov Report

Merging #1070 into master will decrease coverage by 0.07%.
The diff coverage is 19.35%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1070      +/-   ##
==========================================
- Coverage   43.41%   43.33%   -0.08%     
==========================================
  Files         542      543       +1     
  Lines       20718    20780      +62     
  Branches     3018     3029      +11     
==========================================
+ Hits         8994     9006      +12     
- Misses      10877    10927      +50     
  Partials      847      847
Impacted Files Coverage Δ
src/Avalonia.Controls/ComboBox.cs 19.35% <19.35%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 36310f1...040ff40. Read the comment docs.

@codecov
Copy link

codecov bot commented Jul 21, 2017

Codecov Report

❗ No coverage uploaded for pull request base (master@7e1f119). Click here to learn what that means.
The diff coverage is 32.89%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master    #1070   +/-   ##
=========================================
  Coverage          ?   44.84%           
=========================================
  Files             ?      539           
  Lines             ?    21178           
  Branches          ?     3183           
=========================================
  Hits              ?     9498           
  Misses            ?    10778           
  Partials          ?      902
Impacted Files Coverage Δ
src/Avalonia.Controls/ComboBoxItem.cs 0% <0%> (ø)
src/Avalonia.Controls/ComboBox.cs 34.24% <34.24%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7e1f119...4722924. Read the comment docs.

@grokys
Copy link
Member

grokys commented Jul 31, 2017

Thanks @jp2masa! I've been on vacation and just catching up now. I will review this asap!

Copy link
Member

@grokys grokys left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a ComboBoxPage to the ControlCatalog with a simple ComboBox:

<UserControl xmlns="https://github.com/avaloniaui">
  <StackPanel Orientation="Vertical" Gap="4">
    <TextBlock Classes="h1">ComboBox</TextBlock>
    <TextBlock Classes="h2">A combination of a drop down list and a text box control</TextBlock>

    <StackPanel Orientation="Vertical"
                Margin="0,16,0,0"
                HorizontalAlignment="Center"
                Gap="16">
      <ComboBox>
        <ComboBoxItem>Item 1</ComboBoxItem>
        <ComboBoxItem>Item 2</ComboBoxItem>
        <ComboBoxItem>Item 3</ComboBoxItem>
      </ComboBox>
    </StackPanel>    
  </StackPanel>
</UserControl>

And when I run I get the following exception:

System.ArgumentException occurred
  HResult=0x80070057
  Message=Value does not fall within the expected range.
  Source=Avalonia.Base
  StackTrace:
   at Avalonia.Contract.Requires[TException](Boolean condition) in D:\projects\Avalonia\src\Avalonia.Base\Contract.cs:line 33

Could you add a page for the control to the ControlCatalog (or I can push the page I added to your branch if you like) and fix the exception?

Copy link
Member

@grokys grokys left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing that!

Found another problem - this test fails:

        [Fact]
        public void Text_Is_Selected_String_Item()
        {
            var items = new[] { "A", "B", "C", "D" };
            var target = new ComboBox
            {
                Items = items,
                SelectedIndex = 2
            };

            var text = target.GetValue(ComboBox.TextProperty);
            Assert.True(text == "C");
        }

You'll see that items can be of any type, they don't have to be ComboBoxItems.

Secondly, could you merge the latest master into your branch? I would do it myself but you don't seem to have granted me write access to your fork branch.

@jp2masa
Copy link
Contributor Author

jp2masa commented Aug 3, 2017

I think that ComboBox is only supposed to deal with text, specified in ComboBoxItems. Also, how would var items = new[] { "A", "B", "C", "D" }; be declared in xaml?

@grokys
Copy link
Member

grokys commented Aug 4, 2017

No, that's not quite how it works :) For example you can bind the ComboBox.Items. Here's an example:

class ViewModel
{
   public string[] Items { get; } = new[] { "A", "B", "C", "D" }
}
    <ComboBox Items="{Binding Items}"/>

This is a very common scenario (more common than using ComboBoxItem itself in my experience).

Copy link
Member

@grokys grokys left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few more things, sorry :(

If I create a ComboBox like this:

      <ComboBox MinWidth="128" Text="Custom Text">
        <ComboBoxItem>
          <Panel>
            <Rectangle Fill="{StyleResource ThemeAccentBrush}"/>
            <TextBlock Margin="8">Control Items</TextBlock>
          </Panel>
        </ComboBoxItem>
        <ComboBoxItem>Item 2</ComboBoxItem>
        <ComboBoxItem>Item 3</ComboBoxItem>
      </ComboBox>

And select the first item, I get a System.NotSupportedException exception thrown, however this works in WPF - the combo box text is set to System.Windows.Controls.ComboBoxItem.

Also in WPF if I type the text of an item into the ComboBox, that item becomes selected, which doesn't happen here.

Contract.Requires<NotSupportedException>(!(item is IControl));
}

Text = Convert.ToString(item);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can just use item?.ToString() here.


foreach (var item in Items)
{
if (item.ToString() == _textBox.Text)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ToString() may not represent the item correctly. You need to check MemberSelector. Also, item may be null.


foreach (var item in Items)
{
if (item.ToString() == _textBox.Text)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String comparison should be case insensitive


foreach (var item in Items)
{
if (item.ToString() == _textBox.Text)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there's no exact match, the first item that starts with the text should be selected. For example, if the list contains "Item1", "Item2", "Item33", , "Item34", when typing "I", "Item1" would be selected, and when typing "Item3", "Item 33" would be selected. If possible, please unit test those.

/// <inheritdoc/>
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
if (!IsDropDownOpen && ((IVisual)e.Source).GetVisualRoot() != typeof(PopupRoot))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The drop down should be opened by the toggle button, not when clicking anywhere else, so I think this check is redundant.


private void UpdateSelectionBoxText(object item)
{
Text = item?.ToString();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, use MemberSelector instead of ToString()

@@ -0,0 +1,26 @@
<Styles xmlns="https://github.com/avaloniaui">
<Style Selector="ComboBoxItem">
<Setter Property="Padding" Value="2"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add <Setter Property="Background" Value="Transparent"/>

@grokys
Copy link
Member

grokys commented Aug 26, 2017

@jp2masa thanks for this PR, but judging from the fact that there have been multiple problems, I'm going to make a guess and say that you didn't implement it because you needed the functionality? It's just I'm wondering if an old-style ComboBox such as this is even needed in a modern day application?

@jp2masa
Copy link
Contributor Author

jp2masa commented Aug 26, 2017

I don't need the functionality. I was just trying to help, so I was looking for up for grabs issues and I decided to try this one.

@grokys
Copy link
Member

grokys commented Aug 27, 2017

Hmm yeah, we probably shouldn't have marked that issue as up-for-grabs - it's not a simple thing to implement and tbh we probably should have had a conversation about whether it's actually needed. To be fair we do say in our contributing.md:

Before You Start

Drop into our gitter chat room and let us know what you're thinking of doing. We might be able to give you guidance or let you know if someone else is already working on the feature.

Do you want to keep going ahead with this PR? It's probably going to be a long slog unless you have an application that actually needs the functionality as the back-and-forth here has shown. In addition, as I mentioned, personally I'm not sure the control is actually needed. Some kind of Autocomplete control would be far more useful to a 2017 application.

@grokys
Copy link
Member

grokys commented Oct 21, 2017

Ok, I'm going to close this, thanks for your work on it!

@grokys grokys closed this Oct 21, 2017
@grokys grokys added this to the Beta 1 milestone Jan 26, 2018
@almightyju almightyju mentioned this pull request Jan 31, 2025
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants