0

This question is extension to WPF ComboBox with checkboxes and textbox with search field

Adding the UserControl in my window as follows

<Usercontrols:MultiSelectComboBox x:Name="multiCombo" HorizontalAlignment="Left" Height="28" VerticalAlignment="Top" Width="235" Margin="81,27,0,0"/>

I am having my combobox.Template as follows in my combobox

<ComboBox>
<ComboBox.Template>
        <ControlTemplate TargetType="ComboBox">
            <Grid Name="control" >

                <ToggleButton 
                    x:Name="ToggleButton" 
                   Grid.Column="2" IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                    Focusable="false"                           
                    ClickMode="Press" HorizontalContentAlignment="Left" >
                    <ToggleButton.Template>
                        <ControlTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="18"/>
                                </Grid.ColumnDefinitions>
                                <Border
              x:Name="Border" 
              Grid.ColumnSpan="2"
              CornerRadius="2"
              Background="White"
              BorderBrush="Silver"
              BorderThickness="1,1,1,1" />
                                <Border 
                x:Name="BorderComp" 
              Grid.Column="0"
              CornerRadius="2" 
              Margin="1" 
             Background="White"
              BorderBrush="Black"
              BorderThickness="0,0,0,0" >
                                    <TextBlock Text="{Binding Path=Text,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" 
                                           Background="White" Padding="3" />
                                </Border>
                                <Path 
              x:Name="Arrow"
              Grid.Column="1"     
              Fill="Black"
              HorizontalAlignment="Center"
              VerticalAlignment="Center"
              Data="M 0 0 L 4 4 L 8 0 Z"/>
                            </Grid>
                        </ControlTemplate>
                    </ToggleButton.Template>
                </ToggleButton>
                <Popup 
Name="Popup"
Placement="Bottom"                        
AllowsTransparency="True" 
Focusable="False"  IsOpen="{TemplateBinding IsDropDownOpen}"
PopupAnimation="Slide">
                    <Grid 
    Name="DropDown"
    SnapsToDevicePixels="True"  
    MinWidth="{TemplateBinding ActualWidth}"
    MaxHeight="{TemplateBinding MaxDropDownHeight}">
                        <Border 
        x:Name="DropDownBorder"
        BorderThickness="1" Background="White"
        BorderBrush="Black"/>
                        <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True" DataContext="{Binding}">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions>
                                <TextBox Name="TextBox"  Text="{Binding Path=Text, Mode=TwoWay}" TextChanged="TextBox_TextChanged" />
                                <StackPanel Grid.Row="2" IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                            </Grid>
                        </ScrollViewer>
                    </Grid>
                </Popup>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="HasItems" Value="false">
                    <Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
                </Trigger>
                <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
                    <Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4"/>
                    <Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </ComboBox.Template>
</ComboBox>

I am trying to find the TextBox control inside UserControl with ComboBox and ComboBox.ItemTemplate on my text box changed event. I tried as below but i am getting null

private void multiCombo_TextChange(object sender, EventArgs e)
    {
        Grid TxtBox = (Grid)multiCombo.Template.FindName("control", multiCombo);
        //TextBox textBox = sender as TextBox;
        MessageBox.Show(TxtBox.ToString());
    }
mm8
  • 163,881
  • 10
  • 57
  • 88
Learner
  • 351
  • 7
  • 25
  • It is the user control name – Learner Aug 14 '18 at 13:10
  • There is no TextBox control created until the Popup is opened. Where is the multiCombo_TextChange event handler hooked up? – mm8 Aug 14 '18 at 13:12
  • Ok but once after opening the popup I will enter some text in the text box based on that it should filter which I can write – Learner Aug 14 '18 at 13:13

1 Answers1

2

You could cast the OriginalSource property of the TextChangedEventArgs:

private void multiCombo_TextChange(object sender, TextChangedEventArgs e)
{
    TextBox textBox = e.OriginalSource as TextBox;
    //...
}
mm8
  • 163,881
  • 10
  • 57
  • 88
  • sender I am getting as comobox – Learner Aug 14 '18 at 13:17
  • `+ sender {WpfApp1.usercontrol.MultiSelectComboBox} object {WpfApp1.usercontrol.MultiSelectComboBox} ` – Learner Aug 14 '18 at 13:18
  • Here is my code https://drive.google.com/file/d/1VjIkceNVfbis109074ERlYO2R2Q4Die1/view?usp=sharing – Learner Aug 14 '18 at 13:22
  • See my edit. You should be able to cast the OriginalSource of the TextChangedEventArgs. – mm8 Aug 14 '18 at 13:25
  • Thanks in the user control I have written the event as follows `private void TextBox_TextChanged(object sender, TextChangedEventArgs e) { //MessageBox.Show("hi"); TextBox textBox = e.OriginalSource as TextBox; this.TextBoxClick(textBox, e); }` – Learner Aug 14 '18 at 13:29