2017-10-10 4 views
1

wpf 및 mvvm을 배우고 연습하기 위해 Soundboard를 작성하기로 결정했으며 지금까지 꽤 잘 진행되었습니다. 이제 datatemplate을 만들었습니다. 지정된 디렉토리에서 프로그램이 찾은 모든 파일에 대해 파일 이름이있는 버튼을 만들고이를 클릭하여 재생할 수 있습니다. 여태까지는 그런대로 잘됐다. 그러나 이제 ContextMenu를 만들려고했는데 목록에서 파일을 제거하려고 할 때 마우스 오른쪽 단추를 클릭하고 제거를 선택할 수 있지만 일반 단추와 똑같은 명령 구조가 있어도이 명령이 작동하지 않습니다.C# wpf 데이터 바인딩 명령이 컨텍스트 메뉴에서 작동하지 않습니다.

저는 RelativeSource의 모든 일과 정말로 혼동을 느낍니다. 그리고 나의 일반적인 'play'명령은 버튼에서 잘 작동했습니다.

누군가가 올바른 방향으로 나를 가리킬 수 있다면 좋을 것입니다. 저는 항상 제 특정 문제에 대한 설명을 사용할 수 있습니다. 그 이유는 어떻게 든 제네릭 예제를 더 많이 도왔을 것입니다. 나는 관련된 모든 질문을 읽으려고 노력했지만 거기에서 그것을 알아내는 것만은 아닙니다.

내 ItemsControl에 :

<ItemsControl x:Name="MySounds" ItemsSource="{Binding Sounds}"> 

ItemTemplate을 :

<ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <StackPanel> 
         <Button Style="{StaticResource mainButton}" 
           Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.PlaySound}" 
           CommandParameter="{Binding Path=Tag, RelativeSource={RelativeSource Self}}" 
           Tag="{Binding Path=Name}"> 
          <TextBlock Text="{Binding Path=NormalizedName}" TextWrapping="Wrap" Height="auto" /> 
          <Button.ContextMenu> 
           <ContextMenu> 
            <MenuItem Header="{Binding Path=Name}" 
               Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.RemoveSound}" 
               CommandParameter="{Binding Path=Tag, RelativeSource={RelativeSource Self}}"> 
             <MenuItem.Icon> 
              <Image Source="\WpfPractice;component\Images\CoffeeArt.png" Width="20" VerticalAlignment="Center"/> 
             </MenuItem.Icon> 
            </MenuItem> 
           </ContextMenu> 
          </Button.ContextMenu> 
         </Button> 
        </StackPanel> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 

내 뷰 모델에서 일반적인 RelayCommand이 모든 작품 것으로, 문제는 정말 그냥 바인딩입니다.

+0

'RelativeSource' 대신'ElementName '을 사용해 볼 수 있습니까? ItemsControl에 이름을 설정하고 (예 : x : Name = "myIC") 바인딩을 업데이트하십시오. – Atlasmaybe

+0

나는 그것을 시도했다. 그러나 내가 시도한 것에 상관없이 작동하지 않는 것처럼 보인다. 내 MenuItem 헤더에서'{binding path = Name} '으로 내 버튼과 같은 값을 얻었지만 같은 명령이 작동하지 않는 것 같습니다. –

답변

0

당신이 ItemsControlButtonTag 속성을 결합하는 경우의 PlacementTarget 속성을 사용하여 명령에 바인딩 할 수 있습니다 ContextMenu :

<ItemsControl.ItemTemplate> 
    <DataTemplate> 
     <StackPanel> 
      <Button Style="{StaticResource mainButton}" 
           Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.PlaySound}" 
           CommandParameter="{Binding Path=Name}" 
           Tag="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}}"> 
       <TextBlock Text="{Binding Path=NormalizedName}" TextWrapping="Wrap" Height="auto" /> 
       <Button.ContextMenu> 
        <ContextMenu> 
         <MenuItem Header="{Binding Path=Name}" 
            Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.Tag.DataContext.RemoveSound}" 
            CommandParameter="{Binding Path=Name}"> 
          <MenuItem.Icon> 
           <Image Source="\WpfPractice;component\Images\CoffeeArt.png" Width="20" VerticalAlignment="Center"/> 
          </MenuItem.Icon> 
         </MenuItem> 
        </ContextMenu> 
       </Button.ContextMenu> 
      </Button> 
     </StackPanel> 
    </DataTemplate> 
</ItemsControl.ItemTemplate> 
+0

그 트릭을 한 것 같습니다. –

+0

당신은 아마 왜이 작품을 만들었습니까? 태그가 RelativeSource ItemsControl로 설정되고 PlacementTarget이 Tag로 설정되고 DataContext로 설정 되었기 때문입니까? RelativeSource에 대한 나의 이해가 아주 미미하기 전에 내가 말했듯이. –

+0

ContextMenu는 자체 시각적 트리에 있으므로 조상이 없습니다. 단추는 그러나 가지고있다. – mm8

0
당신이하여 MenuItem에 당신의 명령 문자열을 대체하기 위해 시도 할 수

:

Command="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.RemoveSound}" 
관련 문제