8

メニュー項目の動的に作成されたリストを正しくバインドするにはどうすればよいですか。私はいくつかのことを試しましたが、どれもうまくいかないようです。名前の適切なリストを取得しましたが、ViewSwitchCommand が正しく起動していないようです。

<MenuItem Foreground="White" Header="Names" ItemsSource="{Binding Player.ToonNames}" Command="{Binding ViewSwitchCommand}" CommandParameter="{Binding Header}"/>

ただし、動的に実行せず、このように実行すると、すべてが正常に機能し、機能させることができます

<MenuItem Foreground="White" Header="Names">
<MenuItem Foreground="Black" Header="Chat" Command="{Binding ViewSwitchCommand}"     CommandParameter="player1" />
<MenuItem Foreground="Black" Header="Craft" Command="{Binding ViewSwitchCommand}" CommandParameter="player2" />
</MenuItem>

コマンドパラメータは文字列を期待しています..それがそれであるかどうかわかりません...うまくいけば、これは私が見落としている単純なものです

4

1 に答える 1

16

このコードは私のために働きます:

<MenuItem Header="Names" ItemsSource="{Binding Player.ToonNames}">
    <MenuItem.ItemContainerStyle>
        <Style TargetType="MenuItem">
            <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=MenuItem}, Path=DataContext.ViewSwitchCommand}" />
            <Setter Property="CommandParameter" Value="{Binding}" />
        </Style>
    </MenuItem.ItemContainerStyle>
</MenuItem>
于 2011-06-06T00:21:19.983 に答える