0

コマンドを使用してページ間を移動するナビゲーション メニューを持つアプリケーションがあります。

ナビゲーション メニューは Xaml で作成され、Navigation.xaml に保存されています。以下を参照してください。

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:Cirdan.Wpf.Navigation"
xmlns:infrastructure="clr-namespace:Cirdan.Wpf.Infrastructure">

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/Cirdan.Wpf;component/Resources/Styles/Stylesheet.xaml" />
</ResourceDictionary.MergedDictionaries>

<DataTemplate x:Key="Navigation" >        
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="4*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <UniformGrid Grid.Row="0" Columns="1">
            <Button DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type vm:NavigationViewModelBase}}}" Visibility="{Binding ElementName=Menu, Path=IsChecked, Converter={StaticResource VisibilityConverter}}" Command="{Binding ViewerPageCmd}" >Viewer Screen</Button>
            <Button DataContext="{Binding NavigationViewModel, Source={x:Static infrastructure:MainWindow.LocatorX}}" Visibility="{Binding ElementName=Menu, Path=IsChecked, Converter={StaticResource VisibilityConverter}}" Command="{Binding}" >Acquisition Screen</Button>
            <Button DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type vm:NavigationViewModelBase}}}" Visibility="{Binding ElementName=Menu, Path=IsChecked, Converter={StaticResource VisibilityConverter}}" Command="{Binding WorklistPageCmd}" >Worklist Screen</Button>
        </UniformGrid>
        <ToggleButton Grid.Row="1" Style="{StaticResource ToggleBtnToolStyle}" x:Name="Menu" IsChecked="true" Background="Transparent" BorderThickness="0" >
            <StackPanel Orientation="Horizontal">
                <ContentPresenter Margin="5" Height="50" Content="{StaticResource MenuIcon}"></ContentPresenter>
                <Viewbox>
                    <TextBlock Margin="5" Style="{StaticResource TxtToolStyle}">Menu</TextBlock>
                </Viewbox>
            </StackPanel>
        </ToggleButton>
    </Grid>
</DataTemplate>

これらのボタン コマンドをバインドしようとしている ViewModel は、NavigationViewModelBase.cs と呼ばれます。

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;

namespace Cirdan.Wpf.Navigation
{
public abstract class NavigationViewModelBase : ViewModelBase
{
    private List<DicomMetadataModel> _dicomMetadata;

    //Navigation Cmd
    public ICommand AcquisitionPageCmd { get; private set; }
    public ICommand ManualEntryWindowCmd { get; private set; }
    public ICommand SessionWindowCmd { get; private set; }
    public ICommand SettingsWindowCmd { get; private set; }
    public ICommand StudyInfoPageCommandCmd { get; private set; }
    public ICommand ViewerPageCmd { get; private set; }
    public ICommand WorklistPageCmd { get; private set; }


    protected NavigationViewModelBase()
    {
        AcquisitionPageCmd = new RelayCommand(() => Messenger.Default.Send(new GoToPageMessage(Pages.AcquisitionScreen)));
        ManualEntryWindowCmd = new RelayCommand(() => Messenger.Default.Send(new ShowDialogMessage(Pages.ManualEntry, DicomMetadata)));
        SessionWindowCmd = new RelayCommand(() => Messenger.Default.Send(new ShowDialogMessage(Pages.Session)));
        SettingsWindowCmd = new RelayCommand(() => Messenger.Default.Send(new ShowDialogMessage(Pages.Settings)));
        ViewerPageCmd = new RelayCommand(() => Messenger.Default.Send(new GoToPageMessage(Pages.Viewer)));
        WorklistPageCmd = new RelayCommand(() => Messenger.Default.Send(new GoToPageMessage(Pages.Worklist)));
    }
}
}

各ページで、次のコードを使用してナビゲーションを追加します

<ContentControl Grid.Column="2" Grid.Row="2" ContentTemplate="{StaticResource Navigation }" />

現時点ではエラーは発生していません。上記のボタンの 1 つで DataContext を設定すると、コマンドをバインドするときに、その Viewmodel のすべてのプロパティが表示されるので、そのビットは正しく機能しますが、プログラムを実行し、これらのボタンをクリックしても何も起こりません。

助けてくれてありがとう

4

2 に答える 2

1

Ancestor Binding は、visualtree/view 内の要素または単にページ内の要素に対してのみ機能します。viewModel は、コントロールとしてページ内のどこにもありません。したがって、viewModel の代わりに、NavigationViewModelBaseを持つビューのタイプを datacontext.writeバインディングとして指定します (ビュー/コントロールが NavigationViewの場合、バインディングは次のようになります)。そして、パスにvm:NavigationViewModelBase クラスのプロパティを書き込みます:

<Button DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type v:NavigationView}}, path= PropertyYouWantToBind}"

階層内の他のコントロールに対して DataContext を定義していない場合は、ボタンがビューと同じ DataContext を取得することを確認してから、コマンドをバインドするだけです。あなたはすでに正しく行っています。

ビュー内のどのコントロールにも DataContext を渡していない場合は、それを contentControl または Grid に渡してください。単純に

<Grid>
  <Grid.DataContext>
      <vm:NavigationViewModelBase />
  </Grid.DataContext>
<Grid.RowDef.......

次に、コマンドに単純なバインドを使用します。

于 2016-01-13T10:23:15.613 に答える