当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - wpf CheckBox Command in a DataTemplate

问题描述:

I have a DataTemplate like this:

<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>

This is the templateSelector:

<TemplateSelector:PropertyDataTemplateSelector x:Key="templateSelector"

BooleanDataTemplate="{StaticResource CheckBoxDataTemplate}"

IsEnum="IsExclusive"/>

And this is the ListView where the template is referenced:

<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>

This is the command in my ViewModel:

RelayCommand _unCheckCommand;

public ICommand UnCheckCommand

{

get

{

if (_unCheckCommand == null)

{

_unCheckCommand = new RelayCommand(

param => this.UnCheck(param),

param => this.CanUnCheck

);

}

return _unCheckCommand;

}

}

My goal is to pass to the Command, as parameter, the element I select in my listview. How do I do that?

网友答案:

I did on my own:

<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>
分享给朋友:
您可能感兴趣的文章:
随机阅读: