如何从DataTemplate Model Class调用ICommand

时间:2014-08-09 17:23:23

标签: xaml mvvm icommand

我正在使用MVVM模式构建应用程序。我有两个类SettingsDesignerItemViewModel& SettingsDesignerItem。我在ViewModel中实现了ICommand,但我想在SettingsDesignerItem中使用它。如何从SettingsDesignerItem调用ICommand。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:CampaignStudio">

    <x:Array x:Key="CriteriaTypeList" Type="local:ComboBoxItemString">
        <local:ComboBoxItemString CriteriaString = "SELECT"/>
        <local:ComboBoxItemString CriteriaString = "SINGLE"/>
        <local:ComboBoxItemString CriteriaString = "MULTIPLE"/>
    </x:Array>

    <x:Array x:Key="ConditionList" Type="local:ComboBoxItemString">
        <local:ComboBoxItemString ConditionString = "SELECT"/>
        <local:ComboBoxItemString ConditionString = "AND"/>
        <local:ComboBoxItemString ConditionString = "OR"/>
    </x:Array>

    <x:Array x:Key="OperatorList" Type="local:ComboBoxItemString">
        <local:ComboBoxItemString OperatorString = "SELECT"/>
        <local:ComboBoxItemString OperatorString = "LESS THAN"/>
        <local:ComboBoxItemString OperatorString = "LESS THAN OR EQUAL"/>
        <local:ComboBoxItemString OperatorString = "GREATER THAN"/>
        <local:ComboBoxItemString OperatorString = "GREATER THAN OR EQUAL"/>
        <local:ComboBoxItemString OperatorString = "EQUAL"/>
        <local:ComboBoxItemString OperatorString = "NOT EQUAL"/>
    </x:Array>

    <x:Array x:Key="TargetCriteriaRuleList" Type="local:ComboBoxItemString">
        <local:ComboBoxItemString TargetCriteriaRuleString = "SELECT"/>
        <local:ComboBoxItemString TargetCriteriaRuleString = "SELECT CUSTOMER WHERE ANY VALUES MEET THESE CRITERIA"/>
        <local:ComboBoxItemString TargetCriteriaRuleString = "CREATE SUPPRESSION FLAG BASED ON CRITERIA"/>
    </x:Array>

    <!-- DataTemplate for DesignerCanvas look and feel -->
    <DataTemplate DataType="{x:Type local:SettingsDesignerItemViewModel}">
            <Grid>
            <Image IsHitTestVisible="False"
                    Stretch="Fill"
                    Source="../../Images/TargetCriteria.png"
                    Tag="TargetCriteria" />
            <Button HorizontalAlignment="Right"
                    VerticalAlignment="Bottom"
                    Margin="5"
                    Template="{StaticResource infoButtonTemplate}"
                    Command="{Binding ShowDataChangeWindowCommand}" />
        </Grid>
    </DataTemplate>

    <!-- DataTemplate for Popup look and feel -->
    <DataTemplate DataType="{x:Type local:SettingsDesignerItemData}">
        <Grid>
            <DataGrid Name="CriteriaGrid" HorizontalAlignment="Left" Margin="20,195,0,0" VerticalAlignment="Top" Height="383" Width="900" AutoGenerateColumns="False" CanUserReorderColumns="False" FontSize="13" CanUserAddRows="True" ColumnHeaderHeight="40" RowHeight="30" ItemsSource="{Binding TargetCriteriaList}">
                <DataGrid.Columns>
                    <DataGridComboBoxColumn Header="CriteriaType" Width="1*" ItemsSource="{StaticResource CriteriaTypeList}" DisplayMemberPath ="CriteriaString"  SelectedValuePath="CriteriaString" SelectedItemBinding="{Binding CriteriaType, Mode=TwoWay}"/>
                    <DataGridTextColumn Header="Priority" Width="1*" Binding="{Binding Priority, Mode=TwoWay}" />
                    <DataGridComboBoxColumn Header="Condition" Width="1*" ItemsSource="{StaticResource ConditionList}" DisplayMemberPath ="ConditionString"  SelectedValuePath="ConditionString" SelectedItemBinding="{Binding Condition, Mode=TwoWay}"/>
                    <DataGridTextColumn Header="[" Width="1*" Binding="{Binding OpenBracket, Mode=TwoWay}"/>
                    <DataGridTextColumn Header="Data Item" Width="3*" Binding="{Binding DataItem, Mode=TwoWay}"/>
                    <DataGridComboBoxColumn Header="Operator" Width="2*" ItemsSource="{StaticResource OperatorList}" DisplayMemberPath ="OperatorString"  SelectedValuePath="OperatorString" SelectedItemBinding="{Binding Operator, Mode=TwoWay}"/>
                    <DataGridTextColumn Header="Values" Width="1*" Binding="{Binding Values, Mode=TwoWay}"/>
                    <DataGridTextColumn Header="]" Width="1*" Binding="{Binding CloseBracket, Mode=TwoWay}"/>
                </DataGrid.Columns>
            </DataGrid>
            <Label Content="Name:" HorizontalAlignment="Left" Margin="20,34,0,0" VerticalAlignment="Top"/>
            <TextBox HorizontalAlignment="Left" Height="23" Margin="121,37,0,0" TextWrapping="Wrap" Text="{Binding TargetCriteriaName}" VerticalAlignment="Top" Width="799"/>
            <Label Content="Description:" HorizontalAlignment="Left" Margin="20,69,0,0" VerticalAlignment="Top"/>
            <TextBox HorizontalAlignment="Left" Height="80" Margin="121,77,0,0" TextWrapping="Wrap" Text="{Binding TargetCriteriaDesc}" VerticalAlignment="Top" Width="799"/>
            <Label Content="Criteria:" HorizontalAlignment="Left" Margin="20,167,0,0" VerticalAlignment="Top"/>
            <Label Content="Rule:" HorizontalAlignment="Left" Margin="20,601,0,0" VerticalAlignment="Top"/>
            <ComboBox HorizontalAlignment="Left" Margin="64,605,0,0" VerticalAlignment="Top" Width="567" ItemsSource="{StaticResource TargetCriteriaRuleList}" DisplayMemberPath="TargetCriteriaRuleString" SelectedIndex="{Binding TargetCriteriaRule}"/>
            <Button HorizontalAlignment="Left" Margin="933,195,0,0" VerticalAlignment="Top" Width="30" Height="30" RenderTransformOrigin="-2.821,1.96">
                <Image Source="../../Images/RowAdd.png"></Image>
            </Button>
            <Button HorizontalAlignment="Left" Margin="934,236,0,0" VerticalAlignment="Top" Width="30" Height="30" RenderTransformOrigin="-2.821,1.96">
            <Image Source="../../Images/RowDelete.png"></Image>
            </Button>
        </Grid>
    </DataTemplate>

</ResourceDictionary>

1 个答案:

答案 0 :(得分:0)

据我了解,您想要从模型SettingsDesignerItemViewModel触发ViewModel SettingsDesignerItem上的命令?您不应该这样做,因为它违反了MVVM设计的关注点分离。该模型与ViewModel无关。您需求的典型设计是:

ViewModel包含一个命令,该命令调用模型上的函数。如果要从模型而不是ViewModel触发此函数,模型可以只调用它自己的函数。您应该使用INotifyPropertyChangedICollectionChanged通知ViewModel受影响的属性中的更改,而不是通知ViewModel 方法已执行方法的执行