我创建了一个搜索用户控制器,当我使用用户控制器时,我不会使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事件可用?
答案 0 :(得分:1)
执行此操作的一种方法是使用RoutedEvent Handler delegate将委托放在usercontrol中,然后在MainWindow中访问它
在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"
您可以像在任何其他命令中一样在主窗口中访问此TestClick
委托,并创建它的导航处理程序。
<Grid>
<my:UserControl x:Name="testcontrol" TestClick="mycommand_click" />
</Grid>