【问题标题】:Binding an action in an itemtemplate to an event将 itemtemplate 中的操作绑定到事件
【发布时间】:2013-05-16 22:09:54
【问题描述】:

我在一个列表框中有列表框项,这些列表框项是从一个可观察的集合中动态填充的,我正在尝试对列表框项的“选定”事件使用交互触发器来启动一个操作,以便我可以绑定到来自可观察集合的项目。

我遇到的问题是我似乎可以使用事件触发器访问“选定”事件。

代码如下:

        <ListBox x:Name="AssocitedLayerListControl" Background="Transparent" BorderBrush="Transparent" ItemContainerStyle="{StaticResource ListBoxItemStyle}"
                ScrollViewer.VerticalScrollBarVisibility="Hidden" ScrollViewer.HorizontalScrollBarVisibility="Auto" 
                VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="10,0,10,0" Padding="0" Height="25" Width="Auto" >

            <!-- Attribute Table Item Template-->
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <ContentControl>
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0">
                            <TextBlock Text="{Binding Path=Key}"
                                    Foreground="#FFFFFFFF"
                                    FontSize="10" LineHeight="20"  
                                    HorizontalAlignment="Left" VerticalAlignment="Center"
                                    Margin="5,0,5,0" Padding="0" />
                            <Button BorderBrush="#FFFFFFFF" BorderThickness="1" Background="#FF000000"
                                HorizontalAlignment="Left" VerticalAlignment="Top"
                                Padding="1" Margin="0,1,0,1" Style="{StaticResource CloseItemStyle}">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Click">
                                        <actions:RemoveLayer LayerName="{Binding Path=Key}" TargetGraphicsLayer="{Binding Path=Value}" />
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </Button>
                        </StackPanel>
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Selected" SourceObject="{Binding RelativeSource={RelativeSource Mode=TemplatedParent, AncestorType=ListBoxItem}}">
                                <actions:SetGraphicLayerInGrid LayerName="{Binding Path=Key}" TargetGraphicsLayer="{Binding Path=Value}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </ContentControl>
                </DataTemplate>
            </ListBox.ItemTemplate>

            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>

        </ListBox>

【问题讨论】:

    标签: wpf silverlight events listboxitem itemtemplate


    【解决方案1】:

    尝试用DataTrigger 监听ListBoxItem 的IsSelected 属性:

    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
    ...
    <i:Interaction.Triggers>
        <ei:DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}}" Value="True">
            <actions:SetGraphicLayerInGrid LayerName="{Binding Path=Key}" TargetGraphicsLayer="{Binding Path=Value}" />
        </ei:DataTrigger>
    </i:Interaction.Triggers>
    

    【讨论】:

    • 我正要发布这个,因为它为我修复了它。感谢您的贡献。
    猜你喜欢
    • 2019-02-03
    • 1970-01-01
    • 2013-10-09
    • 2017-11-27
    • 2013-10-11
    • 2010-12-03
    • 1970-01-01
    • 2012-10-30
    • 1970-01-01
    相关资源
    最近更新 更多