2016-09-23 2 views
0

내 응용 프로그램에서 hardcodet의 WPF NotifyIcon을 사용하고 있는데, 이는 MVVM Light를 사용하여 구현됩니다. 아이콘에 대한 사용자 지정 TrayPopup을 만들려고합니다. 이 팝업에는 ItemsSource가 있습니다.이 ItemsSource는 내 ViewModel의 "NumberList"개체 목록입니다. 내 DataTemplate 들어, 내 소스 개체의 "ID"라는 속성 및 단추를 표시하는 표를 사용하고 있습니다. 내 ViewModel의 RelayCommand에 다시 연결하려고하는 버튼입니다. 나는 RelativeSource를 사용하여 Binding에 의해 과거에 ItemsControls와 유사한 작업을 수행하여 Window Ancestor의 DataContext를 가져 왔습니다. 이번에는 비슷한 접근 방식을 시도했지만 앱을 실행하면 TrayPopup을 열고 아무 것도 클릭하지 않고 버튼을 클릭합니다. 내 명령이 내 ViewModel에서 해고되지 않습니다.WPF의 ItemsControl에있는 버튼에 대한 명령 바인딩 NotifyIcon

테스트로서 나는 다른 버튼을 내 TrayPopup에 추가하고 그 명령을 내 ViewModel의 동일한 명령에 바인딩하려고 시도했습니다. 그 구속력은 훌륭하게 작동하기 때문에 내가 말할 수있는 한 명령이 아닙니다.

내가 말했듯이이 기술은 NotifyIcon에서 이전과는 전혀 다른 방식으로 사용되었습니다. 단추의 ElementName을 내 Window의 이름에 바인딩하려고 시도했는데 작동하지 않았습니다. 또한 RelativeSource를 부모 TaskbarIcon으로 변경해 보았습니다. 성공하지 못했습니다.

<tb:TaskbarIcon PopupActivation="LeftOrRightClick"> 
     <tb:TaskbarIcon.TrayPopup> 
      <Grid Background="White"> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="200"/> 
        <RowDefinition Height="30"/> 
       </Grid.RowDefinitions> 
       <ScrollViewer Grid.Row="0"> 
        <ItemsControl ItemsSource="{Binding NumberList, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"> 
         <ItemsControl.ItemTemplate> 
          <DataTemplate> 
           <Grid> 
            <Grid.ColumnDefinitions> 
             <ColumnDefinition Width="200"/> 
             <ColumnDefinition/> 
            </Grid.ColumnDefinitions> 

            <Label Grid.Column="0" Content="{Binding Id, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/> 
            <Button Grid.Column="1" Content="Test" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.TestCommand}"/> <!--This binding doesn't work!--> 
           </Grid> 
          </DataTemplate> 
         </ItemsControl.ItemTemplate> 
         <ItemsControl.ItemsPanel> 
          <ItemsPanelTemplate> 
           <StackPanel/> 
          </ItemsPanelTemplate> 
         </ItemsControl.ItemsPanel> 
        </ItemsControl> 
       </ScrollViewer> 

       <Button Grid.Row="1" Content="Test2" Command="{Binding TestCommand}"/> <!--This works--> 
      </Grid> 
     </tb:TaskbarIcon.TrayPopup> 
    </tb:TaskbarIcon> 

나는 내가 쓴이 응용 프로그램의 이전 버전을 사용하고있는 것처럼 뷰 모델이 정확한지에 상당히 확신 모든 해요 : 여기

같은 문제로 NotifyIcon 무엇의 예를 구현 한 것입니다 얼마 전에. 지금까지이 NotifyIcon 이외의 문제는 실행하지 않았습니다. 어떤 아이디어?

답변

0

잠시 동안 두드려서 나는 내 문제에 대한 답을 발견했다. 내 ItemsPanelTemplate의 StackPanel에 내 Button을 바인딩하려는 명령이 포함 된 DataContext가 있는데, 이는 주 ViewModel입니다. StackPanel의 이름을 지정하면 버튼의 ElementName을 해당 이름으로 설정할 수 있었고 바인딩이 작동했습니다. 왜이 방법은 StackPanel과 함께 작동하지만 메인 윈도우가 아닌지 잘 모르겠습니다.

<tb:TaskbarIcon PopupActivation="LeftOrRightClick"> 
    <tb:TaskbarIcon.TrayPopup> 
     <Grid Background="White"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="200"/> 
       <RowDefinition Height="30"/> 
      </Grid.RowDefinitions> 
      <ScrollViewer Grid.Row="0"> 
       <ItemsControl ItemsSource="{Binding NumberList, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"> 
        <ItemsControl.ItemTemplate> 
         <DataTemplate> 
          <Grid> 
           <Grid.ColumnDefinitions> 
            <ColumnDefinition Width="200"/> 
            <ColumnDefinition/> 
           </Grid.ColumnDefinitions> 

           <Label Grid.Column="0" Content="{Binding Id, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/> 
           <Button Grid.Column="1" Content="Test" Command="{Binding ElementName=trayMainStack, Path=DataContext.TestCommand}"/> <!--Updated binding works!--> 
          </Grid> 
         </DataTemplate> 
        </ItemsControl.ItemTemplate> 
        <ItemsControl.ItemsPanel> 
         <ItemsPanelTemplate> 
          <StackPanel Name="trayMainStack"/> 
         </ItemsPanelTemplate> 
        </ItemsControl.ItemsPanel> 
       </ItemsControl> 
      </ScrollViewer> 

      <Button Grid.Row="1" Content="Test2" Command="{Binding TestCommand}"/> <!--This works--> 
     </Grid> 
    </tb:TaskbarIcon.TrayPopup> 
</tb:TaskbarIcon> 
:

어쨌든

사람이 같은 문제에 실행하는 경우, 여기에 업데이트 된 코드는