WPF在带有combobox.Template的组合框中找到文本框控件

时间:2018-08-14 13:05:28

标签: c# wpf combobox

此问题是WPF ComboBox with checkboxes and textbox with search field的扩展名

如下所示在窗口中添加UserControl

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

我的combobox中有如下的combobox.Template

<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>

我试图在TextBoxUserControl的文本框更改事件中找到ComboBox内的ComboBox.ItemTemplate控件。我尝试如下,但我得到空

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());
    }

1 个答案:

答案 0 :(得分:2)

您可以投射OriginalSource的{​​{1}}属性:

TextChangedEventArgs