2016-05-17 20 views
0

,和我不会使TextChanged事件提供UM使用用户控制器TextChanged事件时不可使用我创建了一个搜索用户控制器的用户控制

<UserControl x:Class="VSOft.WpfControls.VSeachTextBox.VSearchTextBox" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:local="clr-namespace:VSOft.WpfControls.VSeachTextBox" 
     xmlns:resx="clr-namespace:VSOft.WpfControls.Properties" 
     xmlns:VFontAwsome="clr-namespace:VSOft.WpfControls.VFontAwsome" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300" Height="25" MouseLeftButtonDown="UserControl_MouseLeftButtonDown"> 
<Viewbox> 
    <Grid> 
     <TextBox 
      x:Name="SearchText" 
      Margin="0" 
      TextWrapping="Wrap" 
      Text="" Height="25" 
      GotFocus="SearchText_GotFocus" 
      TextInput="SearchText_TextInput" 
      TextChanged="SearchText_TextChanged" /> 
     <Label x:Name="SearchHelperLabel" Content="Search" Height="25" Foreground="#FF9E9292" GotFocus="SearchHelperLabel_GotFocus" /> 
     <TextBlock x:Name="SearchTextIcon" TextWrapping="Wrap" Margin="275,0,0,0" Height="25" Foreground="{x:Null}" GotFocus="SearchTextIcon_GotFocus" HorizontalAlignment="Right"> 
     <VFontAwsome:VFontAwsome VForeground="{DynamicResource SideBar.Icon.Normal}" VFontSize="20" VFontsType="search" Height="25" Width="25" HorizontalAlignment="Right" /> 
     </TextBlock> 
    </Grid> 
</Viewbox> 

时,这是我的实现。如何在使用用户控件时使Textchanged Event可用?要做到这一点

+1

是你的事件没有被解雇或你想从mainwindow调用它? – Rohit

+0

嗨凯尔它触发我想从mainwindow调用它。谢谢 – Kalanamith

+1

你想在mainwindow中为你的函数添加不同的实现,还是使用现有的实现?如果你想使用现有的,那么你可以通过创建它的对象并访问它的属性来直接调用usercontrol事件,比如'myUsercontrol.SearchText_GotFocus(this,null)' – Rohit

回答

1

一个方法是通过使用RoutedEvent Handler delegate在其中放置一个代表你的用户控件,然后访问它的主窗口

在你UserControl.xaml.cs

public event RoutedEventHandler TestClick; 

    void onSearchFocus(object sender, RoutedEventArgs e) 
    { 
     if (this.TestClick != null) 
     { 
      this.TestClick(this, e); 
     } 
    } 

,并在XAML

<Grid> 
     <TextBox 
     x:Name="SearchText" 
     Margin="0" 
     TextWrapping="Wrap" 
     Text="" Height="25" 
     GotFocus="onSearchFocus" 

您可以像访问其他任何命令一样访问此代码,并创建其导航处理程序。

<Grid> 
     <my:UserControl x:Name="testcontrol" TestClick="mycommand_click" /> 
    </Grid>