2015-05-13 50 views
0

我有一個UserControl的窗口,該UserControl有一個元素。 如果我將鼠標移動到UserControl內部的元素上,我希望窗口內的其他元素能夠作出反應(例如:隱藏它們)。綁定到或公開IsMouseOver或UserControl中的元素的任何其他ReadOnly DependencyProperty

enter image description here

<Window x:Class="WpfApplication4.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525" 
    xmlns:local="clr-namespace:WpfApplication4" 
    > 
<DockPanel> 
    <Grid Width="50" Height="50" Background="Yellow"> 
     <Grid.Style> 
      <Style TargetType="Grid"> 
       <Style.Triggers> 

        <!-- When hovering innerControl inside userControl this grid should become invisible, of course this doesn't work, it hides the element if I move the mouse over any part of the usercontrol--> 

        <DataTrigger Binding="{Binding userControl.IsMouseOver, ElementName=userControl}" Value="True"> 
         <Setter Property="Visibility" Value="Collapsed" /> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
     </Grid.Style> 
    </Grid> 
    <local:userControl x:Name="userControl" Width="300" Height="300"/> 
</DockPanel> 

用戶控件

<UserControl x:Class="WpfApplication4.userControl" 
     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" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300" 
     Background="Red" 
     > 
    <Grid> 
    <Grid x:Name="innerControl" Background="Blue" Width="100" Height="100"/> 
</Grid> 

public partial class userControl : UserControl 
{ 
    public userControl() 
    { 
     InitializeComponent(); 
    } 

    //This is far from working 

    public new bool IsMouseOver 
    { 
     get { return (bool)GetValue(IsMouseOverProperty); } 
     set { SetValue(IsMouseOverProperty, value); } 
    } 

    public new static readonly DependencyProperty IsMouseOverProperty = 
     Border.IsMouseOverProperty.AddOwner(typeof(Border)); 
} 

我知道我必須使用依賴屬性,但我無法適應恩我已經發現這個問題。

+0

你爲什麼要自己製作「IsMouseOver」屬性?我很確定所有的FrameworkElement都有這個屬性。 – Falgantil

+0

是的,該代碼是錯誤的。 (//這是遠遠沒有工作) – Tuco

+0

關於你的原始問題,關閉我的頭頂,我會說這可能是不可能的,除非你創建所有標準的「IsMouseOver」代碼,並且對內部做出反應控制,而不是整個UserControl。這看起來像很多工作,所以我建議只是在原始的xaml視圖中進行工作。 – Falgantil

回答

1

萬一有人關心,這裏的解決方案

從這個答案改編:https://stackoverflow.com/a/317699/3596441

顯露子控件在用戶控件的屬性:

public partial class userControl : UserControl 
{ 
    public userControl() 
    { 
     InitializeComponent(); 
     DependencyPropertyDescriptor descriptor = DependencyPropertyDescriptor.FromProperty(FrameworkElement.IsMouseOverProperty, typeof(FrameworkElement)); 
     descriptor.AddValueChanged(this.innerControl, new EventHandler(OnIsMouseOverChanged)); 

    } 

    // Dependency Property Declaration 
    private static DependencyPropertyKey ElementIsMouseOverPropertyKey = DependencyProperty.RegisterReadOnly("ElementIsMouseOver", typeof(bool),typeof(userControl), new PropertyMetadata()); 
    public static DependencyProperty ElementIsMouseOverProperty = ElementIsMouseOverPropertyKey.DependencyProperty; 


    public bool ElementIsMouseOver 
    { 
     get { return (bool)GetValue(ElementIsMouseOverProperty); } 
    } 
    private void SetIsMouseOver(bool value) 
    { 
     SetValue(ElementIsMouseOverPropertyKey, value); 
    } 

    // Dependency Property Callback 
    // Called when this.MyElement.ActualWidth is changed 
    private void OnIsMouseOverChanged(object sender, EventArgs e) 
    { 
     this.SetIsMouseOver(this.innerControl.IsMouseOver); 
    } 


} 

使用這樣的:

<Window x:Class="WpfApplication4.MainWindow" 
    ... 
    xmlns:local="clr-namespace:WpfApplication4" 
    > 
<DockPanel> 
    <Grid DockPanel.Dock="Left" Width="100" Height="100" Background="Yellow"> 
     <Grid.Style> 
      <Style TargetType="Grid"> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding ElementIsMouseOver, ElementName=userControl}" Value="True"> 
         <Setter Property="Visibility" Value="Collapsed"/> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
     </Grid.Style> 
    </Grid> 

    <local:userControl x:Name="userControl" Width="300" Height="300"/> 

</DockPanel> 

相關問題