2009-06-28 4 views
0

여기 WPF 초보자는 간단한 질문을 변명합니다. 해당 UserControl 외부에서 컨트롤에서 UserControl 트리거를 발생 시키려면 어떻게합니까? 다음은 내가하고 싶은 것입니다 ...외국 컨트롤에서 트리거 활성화

IsMouseOver가 True 일 때 배경색 변경을 표시하도록 설정된 트리거가있는 UserControl이 있습니다. UserControl 위로 마우스를 놓으면 예상대로 트리거가 실행됩니다. 내가 뭘하고 싶은 UserControl 및 단추가 포함 된 창을 만들고 단추 위로 마우스를 때 UserControl 트리거를 발생시키는 것입니다. 같은 뭔가 :

<Window x:Class="WpfApplication1.SimpleUCTry1.Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:WpfApplication1.SimpleUCTry1" 
Title="Window1" Height="300" Width="300"> 
<StackPanel> 
    <local:Simple /> 
    <Button Content="Foo" /> 
</StackPanel> 
는 그래서 "푸"버튼을 통해 사용자가 마우스를 올리면에서, "단순"UserControl을의 트리거가 발생한다면.

이것이 가능합니까?

가 당신의 통제 때문에, 앤디

답변

2

감사하는 버튼을 "외부", 당신은 속성 트리거를 사용할 수 없거나 데이터가 배경 플립 트리거합니다. 필요한 것은 Window 레벨의 EventTrigger입니다.

는 MouseEnter에 0 기간이 키 프레임 또는 개별 컬러 애니메이션을 시작하는 MouseLeave에 언급 된 스토리 보드 제거

: 수 없습니다 당신이 뭔가를해야 할 경우,

<Window.Triggers> 
    <EventTrigger RoutedEvent="Mouse.MouseEnter" SourceName="button"> 
     <BeginStoryboard x:Name="Change_Control_Background_Start" 
      Storyboard="{StaticResource Change_Control_Background}"/> 
    </EventTrigger> 
    <EventTrigger RoutedEvent="Mouse.MouseLeave" SourceName="button"> 
     <RemoveStoryboard 
      BeginStoryboardName="Change_Control_Background_Start"/> 
    </EventTrigger> 
</Window.Triggers> 
+0

감사합니다. EventSetter 또는 RoutedUICommand를 사용하여이를 수행 할 수도 있습니까? –

+0

EventSetter로 할 수는 있지만 코드 숨김 처리기가 필요합니다 (이 경우 마우스 이벤트를 단추로 직접 처리 할 수도 있습니다). RoutedUICommand는 좋은 일치가 아닙니다. 명령은 사용자가 의도적으로 수행하는 항목이며 호버링이이 범주에 속하지 않습니다. –

1

세르게이의 대답은 그것을 할 수있는 방법입니다 당신은 항상 DataTemplate이 당신의 컨트롤을 포장하고 보여 ContentPresenter에 사용할 수 있습니다 EventTrigger으로 수행 : 그것을 (할

<Window x:Class="WpfApplication1.SimpleUCTry1.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:WpfApplication1.SimpleUCTry1" 
    Title="Window1" Height="300" Width="300"> 
    <ContentPresenter Content="{Binding}"> 
     <ContentPresenter.ContentTemplate> 
      <DataTemplate> 
       <StackPanel> 
        <local:Simple Name="Ctrl1" /> 
        <Button Name="Ctrl2" Content="Foo" /> 
       </StackPanel> 
       <DataTemplate.Triggers> 
        <Trigger SourceName="Ctrl2" Property="IsMouseOver" Value="True"> 
         <Setter TargetName="Ctrl1" Property="Background" Value="Blue"/> 
        </Trigger> 
       </DataTemplate.Triggers> 
      </DataTemplate> 
     </ContentPresenter.ContentTemplate> 
    </ContentPresenter> 
</Window> 
+0

이것은 앤디가 이미 UserControl을 가지고 있기 때문에 좋은 방법입니다. 언급 된 버튼을 추가하면 (정의에 따라 이미 시각적으로 이미 묶여 ​​있음) 언급 된 ContentPresenter 트릭을 사용하십시오. –

1

그러나 또 하나의 방법은 뒤에 몇 가지 코드가 필요하지만, 아마도 가장 깨끗한}

입니다
<StackPanel> 
    <local:Simple Background="{Binding ElementName=bnFoo, 
        Path=IsMouseOver, 
        Converter={StaticResource boolToBackgroundConv}}"/> 
    <Button Name="bnFoo" Content="Foo" /> 
</StackPanel> 
관련 문제