2009-01-21 102 views
8

我有一個帶有文本框和一些樣式的按鈕的數據模板。當焦點位於旁邊的文本框時,我想讓按鈕顯示鼠標懸停狀態。這可能嗎?僞造WPF鼠標懸停可能嗎?

我想它會涉及到這樣的事情。我可以通過使用FindVisualChild和FindName來獲取文本框。然後,我可以在文本框上設置GotFocus事件來執行某些操作。

_myTextBox.GotFocus += new RoutedEventHandler(TB_GotFocus); 

這裏在TB_GotFocus我卡住了。我可以得到我想要顯示鼠標懸停狀態的按鈕,但我不知道發送給它的事件。 MouseEnterEvent是不允許的。

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

    } 
+0

你可以發佈你的控制模板給我們看嗎? – 2009-01-23 19:02:56

回答

2

我不認爲這是可能的假事件,但你可以強制按鈕來表現自己彷彿它有鼠標懸停。

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

需要引用PresentationFramework.Aero.dlll這個工作,然後它會在Vista的Aero主題才起作用。

如果您希望它適用於其他主題,則應爲您想要支持的每個主題製作自定義控件模板。

小費

http://blogs.msdn.com/llobo/archive/2006/07/12/663653.aspx
+0

這對大多數我懷疑的人都有效,但我使用的是自定義按鈕模板,所以默認的Windows主題不適用於我。 – Jippers 2009-01-21 19:18:04

0

作爲後續行動,以jesperll的評論,我覺得你可以通過周圍的樣式動態設置爲你想要/空了一個讓每個主題自定義模板。

這是我的窗口,風格定義(但沒有設置任何東西)。

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

而是在模板觸發器通過設置樣式,你可以用你的.cs文件中的事件,像這樣:

...

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

你得到在構造函數中引用樣式,然後動態設置/取消設置。這樣,你就可以定義你希望你的樣式在Xaml中看起來像什麼,而且你不必依賴任何新的依賴關係。