Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- _myTextBox.GotFocus += new RoutedEventHandler(TB_GotFocus);
- void TB_GotFocus(object sender, RoutedEventArgs e)
- {
- ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(this.DataTemplateInstance);
- DataTemplate template = myContentPresenter.ContentTemplate;
- Button _button= template.FindName("TemplateButton", myContentPresenter) as Button;
- _button.RaiseEvent(new RoutedEventArgs(Button.MouseEnterEvent));
- }
- private void tb_GotFocus(object sender, RoutedEventArgs e)
- {
- // ButtonChrome is the first child of button
- DependencyObject chrome = VisualTreeHelper.GetChild(button, 0);
- chrome.SetValue(Microsoft.Windows.Themes.ButtonChrome.RenderMouseOverProperty, true);
- }
- private void tb_LostFocus(object sender, RoutedEventArgs e)
- {
- // ButtonChrome is the first child of button
- DependencyObject chrome = VisualTreeHelper.GetChild(button, 0);
- chrome.ClearValue(Microsoft.Windows.Themes.ButtonChrome.RenderMouseOverProperty);
- }
- <Window x:Class="WpfApplication.Window1"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:WpfApplication"
- Title="Window1" Height="300" Width="300">
- <Window.Resources>
- <Style TargetType="{x:Type Button}" x:Key="MouseOverStyle">
- <Setter Property="Background">
- <Setter.Value>Green</Setter.Value>
- </Setter>
- </Style>
- </Window.Resources>
- <Grid Height="30">
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="3*"/>
- <ColumnDefinition Width="*"/>
- </Grid.ColumnDefinitions>
- <TextBox x:Name="MyTextBox" Grid.Column="0" Text="Some Text" Margin="2" GotFocus="TextBox_GotFocus" LostFocus="MyTextBox_LostFocus"/>
- <Button x:Name="MyButton" Grid.Column="1" Content="Button" Margin="2" MouseEnter="Button_MouseEnter" MouseLeave="Button_MouseLeave" />
- </Grid>
- public partial class Window1 : Window
- {
- Style mouseOverStyle;
- public Window1()
- {
- InitializeComponent();
- mouseOverStyle = (Style)FindResource("MouseOverStyle");
- }
- private void TextBox_GotFocus(object sender, RoutedEventArgs e) { MyButton.Style = mouseOverStyle; }
- private void MyTextBox_LostFocus(object sender, RoutedEventArgs e) { MyButton.Style = null; }
- private void Button_MouseEnter(object sender, MouseEventArgs e) { ((Button)sender).Style = mouseOverStyle; }
- private void Button_MouseLeave(object sender, MouseEventArgs e) { ((Button)sender).Style = null; }
- }
Add Comment
Please, Sign In to add comment