Wpf DataTemplate中的CheckBox命令

时间:2015-09-01 07:47:12

标签: c# wpf listview datatemplate

我有一个像这样的DataTemplate:

<DataTemplate x:Key="CheckBoxDataTemplate">
    <CheckBox IsChecked="{Binding 
              Path=IsSelected, Mode=TwoWay}" 
              HorizontalAlignment="Center" 
              VerticalAlignment="Center" Name="aDM_LEVEL_FIELDListView" Path="SelectedItem">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Checked">
                <WPFCtlr:EventToCommand Command="{Binding DataContext.UnCheckCommand, RelativeSource={RelativeSource FindAncestor,ListView,1}}" CommandParameter="{Binding ElementName=_thisChk}" />
                <!--<i:InvokeCommandAction Command="{Binding Path=SelectItemRelayCommand}" />-->
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </CheckBox>
    <!--Command="{Binding Path=DataContext.UnCheckCommand}" 
              CommandParameter="{Binding Path=IsChecked, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />-->
</DataTemplate>

这是templateSelector:

<TemplateSelector:PropertyDataTemplateSelector x:Key="templateSelector"
      BooleanDataTemplate="{StaticResource CheckBoxDataTemplate}" 
      IsEnum="IsExclusive"/>

这是引用模板的ListView

<ListView   Grid.Row="1"
    ItemsSource="{Binding Path=FilteredLevelFields}"
    Margin="5,10,5,5" 
    Name="aDM_LEVEL_FIELDListView" 
    SelectionMode="Single"
    FontSize="13"
    Background="AliceBlue"
    BorderBrush="AliceBlue" 
    SelectedItem="{Binding Path=CurrentElement}">

    <!--Style of items-->
        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <!--Properties-->
                <Setter Property="Control.HorizontalContentAlignment" Value="Stretch" />
                <Setter Property="Control.VerticalContentAlignment" Value="Center" />
                <!--Trigger-->
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="{x:Null}" />
                        <Setter Property="BorderBrush" Value="{x:Null}" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ListView.ItemContainerStyle>

        <ListView.View>
            <GridView ColumnHeaderContainerStyle="{StaticResource GridViewColumnHiddenHeaderStyle}">
                <GridViewColumn Header="ID LEVEL FIELD" CellTemplate="{StaticResource FatherTemplate}" Width="300"/>
                <GridViewColumn Header="Value" CellTemplateSelector="{StaticResource templateSelector}" Width="80" />
                <GridViewColumn Header="ID LEVEL FIELD" CellTemplate="{StaticResource DetailIdenTemplate}" Width="300"/>
            </GridView>
        </ListView.View>
</ListView>

这是我的ViewModel中的命令:

RelayCommand _unCheckCommand;
public ICommand UnCheckCommand
{
    get
    {
        if (_unCheckCommand == null)
        {
            _unCheckCommand = new RelayCommand(
                param => this.UnCheck(param),
                param => this.CanUnCheck
            );
        }
        return _unCheckCommand;
    }
}

我的目标是传递给Command,作为参数,我在listview中选择的元素。我该怎么做?

1 个答案:

答案 0 :(得分:0)

我自己做了:

<DataTemplate x:Key="CheckBoxDataTemplate">
    <CheckBox IsChecked="{Binding 
              Path=IsSelected, Mode=TwoWay}" 
              HorizontalAlignment="Center" 
              VerticalAlignment="Center" Name="aDM_LEVEL_FIELDListView" Path="SelectedItem" >
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Checked">
                <WPFCtlr:EventToCommand Command="{Binding DataContext.UnCheckCommand, RelativeSource={RelativeSource FindAncestor,ListView,1}}" CommandParameter="{Binding ElementName=aDM_LEVEL_FIELDListView, Path=SelectedItem}" />
                <!--<i:InvokeCommandAction Command="{Binding Path=SelectItemRelayCommand}" />-->
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </CheckBox>
    <!--Command="{Binding Path=DataContext.UnCheckCommand}" 
              CommandParameter="{Binding Path=IsChecked, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />-->
</DataTemplate>