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이 모든 작품 것으로, 문제는 정말 그냥 바인딩입니다.
'RelativeSource' 대신'ElementName '을 사용해 볼 수 있습니까? ItemsControl에 이름을 설정하고 (예 : x : Name = "myIC") 바인딩을 업데이트하십시오. – Atlasmaybe
나는 그것을 시도했다. 그러나 내가 시도한 것에 상관없이 작동하지 않는 것처럼 보인다. 내 MenuItem 헤더에서'{binding path = Name} '으로 내 버튼과 같은 값을 얻었지만 같은 명령이 작동하지 않는 것 같습니다. –