棱镜获得viewmodel属性[关闭]

问题描述 投票:0回答:1

我想从数据模板访问viewmodel Icommand(对于按钮的命令,Icommand属性的名称为ServerCommand)。这是xaml代码:

<Window x:Class="WpfApp2.Views.MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        xmlns:helpers="clr-namespace:WpfApp2.Helpers"
        xmlns:local="clr-namespace:WpfApp2.Views"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        mc:Ignorable="d"
        Title="MainView" Height="450" Width="800">


    <Window.Resources>

        <!--Button tamplate-->
        <DataTemplate x:Key="ButtonTemplate">
            <Button x:Name="OrderButton"
                        FontSize="10"
                        Height="20" Width="80"
                        Content="{Binding Value}"
                    CommandParameter ="{Binding Value}"
                    Command="{Binding DataContext.ServerCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
                    Margin="0">
                </Button>
        </DataTemplate>




        <helpers:IbuttonTemplateSelector x:Key="IbuttonTemplateSelector" 
                                         ButtonTemplate="{StaticResource ButtonTemplate}"
                                         RadioTemplate="{StaticResource RadioTemplate}"
                                         CheckTemplate="{StaticResource CheckTemplate}"
                                         NumericTemplate="{StaticResource NumericTemplate}"/>

    </Window.Resources>

    <!--OUR LIST OF SETTINGS TO DISPLAY-->
    <ItemsControl
        ItemsSource="{Binding Controls}"
            ItemTemplateSelector="{StaticResource IbuttonTemplateSelector}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</Window>

但是它不起作用...问题是Icommand属性在viewModel中,但是命令的参数在数据模板的项目中。

我希望从数据模板访问视图模型Icommand(对于按钮的命令,Icommand属性的名称为ServerCommand)。这是xaml代码:

c# wpf xaml mvvm prism
1个回答
0
投票

您仅初始化字段_serverCommand,但由于已将ServerCommand实现为自动属性,因此未使用该字段。将定义更改为:

© www.soinside.com 2019 - 2024. All rights reserved.