locked
about the ComboBox's textChanged Event? RRS feed

  • Question

  •  

    now, I add a ComboBox to a Grid:IsEditable ="True" ;

    the displaying text is from either keyboard input or selecteditem;

     

    when the displaying text is changed, I want to get the displaying text.

     

    now, the keyboard  input event is "PreviewTextInput", and the selection-changed event is "SelectionChanged";

     

    I must add two event function as follows:

     

            void PreviewTextInput(Object sender, TextCompositionEventArgs e)
            {

            }
            void SelectChanged(Object sender, SelectionChangedEventArgs e)
            {

            }

     

    is there a shared-event to respose when the displaying text is changing?

    help me please!

    Wednesday, August 8, 2007 2:10 AM

Answers

  • Now, we can bind one TextBox to ComboBox and detect TextChanged event of TextBox like this:

     

    <ComboBox Name="myComboBox" SelectedIndex="0">

            <ComboBoxItem>1</ComboBoxItem>

            <ComboBoxItem>2</ComboBoxItem>

            <ComboBoxItem>3</ComboBoxItem>

    </ComboBox>

    <TextBox Text="{Binding ElementName=myComboBox, Path=Text}"

    TextChanged="OnComboBoxTextChanged"/>

     

    void OnComboBoxTextChanged(...)

    {

    }

    Wednesday, August 8, 2007 4:43 AM

All replies

  • I know that ComboBox is made up by TextBox, Button and Popup, so it seems that we can add handler for TextBox.TextChanged event. But now I can't obtain the TextBox in ComboBox. ......

    Wednesday, August 8, 2007 4:20 AM
  • Now, we can bind one TextBox to ComboBox and detect TextChanged event of TextBox like this:

     

    <ComboBox Name="myComboBox" SelectedIndex="0">

            <ComboBoxItem>1</ComboBoxItem>

            <ComboBoxItem>2</ComboBoxItem>

            <ComboBoxItem>3</ComboBoxItem>

    </ComboBox>

    <TextBox Text="{Binding ElementName=myComboBox, Path=Text}"

    TextChanged="OnComboBoxTextChanged"/>

     

    void OnComboBoxTextChanged(...)

    {

    }

    Wednesday, August 8, 2007 4:43 AM
  • Hello

    You can access to the original textbox by this way, but only after the rendering

            protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
            {
                base.OnRender(drawingContext);
                TextBox txt = this.GetTemplateChild("PART_EditableTextBox") as TextBox;
                txt.TextChanged += new TextChangedEventHandler(txt_TextChanged);
            }

    You need to add this code on your combobox C# part

    and IsReadOnly="false" on the xaml part

    Serge
    Friday, February 15, 2008 11:31 PM
  • so strange why microsoft's ComboBox in WPF is not provided with simple TextChanged property...
    Monday, March 3, 2008 4:18 PM
  • vapkse said:

    Hello

    You can access to the original textbox by this way, but only after the rendering

            protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
            {
                base.OnRender(drawingContext);
                TextBox txt = this.GetTemplateChild("PART_EditableTextBox") as TextBox;
                txt.TextChanged += new TextChangedEventHandler(txt_TextChanged);
            }

    You need to add this code on your combobox C# part

    and IsReadOnly="false" on the xaml part

    Serge

    Vapkse,

    The texbox is returned as null, so I can't attached the event handler.

    Can you please show me what the xaml is supposed to look like?


    my xaml looks like this:

    <Style TargetType="{x:Type local:CustomControl1}">

    <Setter Property="Template">

    <Setter.Value>

    <ControlTemplate TargetType="{x:Type local:CustomControl1}">

    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">

    <Grid>

    <ComboBox 
        
    x:Name="PART_EditableTextBox"
        Height="30"
        Width="100"
        IsReadOnly="False"
        IsEditable="True">

    <Label x:Name="txttestitem1" Content="test item 1"/>
    <Label x:Name="lbltestitem2" Content="test item 2"/>

    </ComboBox>

     

    </Grid>

    </Border>

    </ControlTemplate>

    </Setter.Value>

    </Setter>

    </Style>




     

    Monday, January 12, 2009 1:23 PM

  • What you probably want to do is bind to the Text attribute in XAML:

    <ComboBox Text={Bind PartialSearchTerm} />

    Your data context will then be updated whenever a user types in the combo box then.  This fits nicely with the DM-V-VM model proposed here (http://blogs.msdn.com/dancre/archive/2006/10/11/datamodel-view-viewmodel-pattern-series.aspx), though you could have your code behind be the DataContext just as well.  If you needed to broker an event then you can just use normal C# events:

    In DataContext:

    public delegate void TextChangedEventHandler(object sender, EventArgs e);<br> 
    public event TextChangedEventHandler TextChanged; 
     
    private string _partialSearch; 
    public string PartialSearchTerm 
      get 
      { 
        return _partialSearch; 
      } 
      set 
      { 
        _partialSearch = value
        TextChanged(this, EventArgs.Empty); 
      } 
     

    • Proposed as answer by WineNCheese Friday, January 23, 2009 9:22 PM
    Friday, January 16, 2009 5:41 AM
  • vapkse said:

    Hello

    You can access to the original textbox by this way, but only after the rendering

            protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
            {
                base.OnRender(drawingContext);
                TextBox txt = this.GetTemplateChild("PART_EditableTextBox") as TextBox;
                txt.TextChanged += new TextChangedEventHandler(txt_TextChanged);
            }

    You need to add this code on your combobox C# part

    and IsReadOnly="false" on the xaml part

    Serge

    Vapkse,

    The texbox is returned as null, so I can't attached the event handler.

    Can you please show me what the xaml is supposed to look like?


     

    Public Overrides Sub OnApplyTemplate()

     

    If Not IsEditable Then Exit Sub

     

    MyBase.OnApplyTemplate()

     

    Dim tb = Template.FindName("PART_EditableTextBox", Me)

     

    End Sub

    This will work. Note, that the TextBox is only used when IsEditable = true, otherwise it uses a semi-button template, not the PART_EditableTextBox.

    Shimmy
    Thursday, February 11, 2010 11:35 AM
  • Had the same problem. This is my solution.

    ComboBox has a TextProperty depedencyobject. You can attach an PropertyChanged event handler on that.

            static MyComboBox()
            {
                TextProperty.OverrideMetadata(typeof(MyComboBox), new FrameworkPropertyMetadata(new PropertyChangedCallback(TextChanged)));
            }
    


    Note that your TextChanged() method is also called when the user changes his selection in the combobox, so you could perhaps consider a boolean to set if the event is already handled or not.

    SelectionChanged is called before TextChanged.

    edit:
    Here is some more code

            private void MyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if (this.SelectedIndex != -1)
                {
                    this.selectionChanged = true;
                }
    	
    		// do stuff
    	}

            private static void TextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
            {
                MyComboBox combo = sender as MyComboBox ;
    
                if (combo != null)
                {
                    if (combo.selectionChanged)
                    {
                        combo.selectionChanged = false;
                        return;
                    }
                }
    
                log.DebugFormat("Text changed.");
            }
    Hope that helps
    • Proposed as answer by JP.LA.Douche Saturday, February 20, 2010 11:28 PM
    Saturday, February 20, 2010 11:22 PM
  • Right. I am deleting my previous post.
    Shimmy
    Sunday, February 21, 2010 6:55 PM
  • Hi,

    Here is another approach to solving this problem that is a little bit cleaner:

     

    <ComboBox x:Name="ScopeComboBox" IsEditable="True">
      <ComboBox.Style>
        <Style>
          <EventSetter Event="TextBox.TextChanged" Handler="OnScopeTextChanged" />
        </Style>
      </ComboBox.Style>
    </ComboBox>
    
    

    Regards,
    Logan

     

    Friday, August 6, 2010 8:40 PM
  • Indeed sounds more elegant, but did you test it?

    Are you sure the ComboBox refers to the right TextChanged event? are you sure the internal event is not marked e.Handler = True before it reaches the EventTrigger?


    Shimmy
    Saturday, August 7, 2010 8:44 PM
  • This is the easiest way I've found:

    <

     

    ComboBox IsEditable="True" TextBoxBase.TextChanged="ComboBox_TextChanged"/>

    • Proposed as answer by Thibault Wednesday, July 6, 2011 4:29 AM
    Thursday, August 12, 2010 4:12 PM
  • Hi i have problem. But i can't solve it my self. Could you help me please.

    I am using Visual Studio 2008, so i used combobox.

    Then i linked database to combobox but combobox text will not change after select text on combobox. Although selected value is working correct, text will not change? what i do now?

    Regards Baljaa

     

    Thursday, October 27, 2011 11:50 AM
  • Hi i have problem. But i can't solve it my self. Could you help me please.

    I am using Visual Studio 2008, so i used combobox.

    Then i linked database to combobox but combobox text will not change after select text on combobox. Although selected value is working correct, text will not change? what i do now?

    Regards Baljaa

     

    Thursday, October 27, 2011 11:50 AM
  • Hi i have problem. But i can't solve it my self. Could you help me please.

    I am using Visual Studio 2008, so i used combobox.

    Then i linked database to combobox but combobox text will not change after select text on combobox. Although selected value is working correct, text will not change? what i do now?

    Regards Baljaa

     

    Thursday, October 27, 2011 11:51 AM
  • Hi me again. I have a another problem with Visual studio?

    I want to open excel file on RichTextbox in Visual Studio and edit excel file from visual studio. So first of all, i want to know it is possible. If it is possible could you help me or suggestion to me.

    Regards Baljaa

    Thursday, October 27, 2011 11:53 AM
  • @bruce.du

    I had a problem which I think might be the underlying problem to your question: When I try to use comboBox.Text from within SelectChanged, it still has the old value.  Here is what I came up with to fix that:

    private void cmbbox_selectionChanged(object sender, SelectionChangedEventArgs e)
    {
       cmb_installedFonts.Text = (string)e.AddedItems[0];
    }
    
    - Paul Schrum

    Tuesday, February 14, 2012 12:43 AM