从WPF中的DataTemplate绑定到命令

时间:2016-01-11 14:32:54

标签: c# wpf xaml data-binding datatemplate

首先感谢您的任何帮助。

我有一个应用程序,我想添加导航菜单。我创建了一个DataTemplate,它包含我的所有按钮和样式,我目前在样式表中有这个。

<DataTemplate x:Key="Navigation">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="4*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <UniformGrid Grid.Row="0" Columns="1">
            <Button Visibility="{Binding ElementName=Menu, Path=IsChecked, Converter={StaticResource VisibilityConverter}}" Style="{StaticResource MenuButtonStyle}" >Page 1</Button>
            <Button Visibility="{Binding ElementName=Menu, Path=IsChecked, Converter={StaticResource VisibilityConverter}}" Style="{StaticResource MenuButtonStyle}" >Page 2</Button>
            <Button Visibility="{Binding ElementName=Menu, Path=IsChecked, Converter={StaticResource VisibilityConverter}}" Style="{StaticResource MenuButtonStyle}" >Page 3</Button>
            <Button Visibility="{Binding ElementName=Menu, Path=IsChecked, Converter={StaticResource VisibilityConverter}}" Style="{StaticResource MenuButtonStyle}" >Page 4</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>

目前,我能够实现这一目标的唯一方法是将其从DataTemplate中删除并在每个页面上实现网格。 有没有办法从我的样式表中存储的DataTemplate绑定到我的NavigationViewModel? 如果这个问题措辞严重,我很抱歉,我是WPF的新手,可以做基本的数据绑定,但我在这里迷失了。

由于

编辑

带有我想要链接的命令的ViewModel看起来像这样

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" Content="{Binding}" ContentTemplate="{StaticResource Navigation }" />

0 个答案:

没有答案