【问题标题】:Button in ListBox item's ItemsControl doesn't fire in WPF XAMLListBox 项的 ItemsControl 中的按钮不会在 WPF XAML 中触发
【发布时间】:2014-07-08 16:35:43
【问题描述】:

我在 wpf 中为我的 ListBox 设置了以下设置:

  • ListBoxItemsPanelStackPanel(方向水平)
  • ItemTemplateListBoxGridTextBoxItemsControl
  • Items 控件包含带有ItemPanelWrapPanel 的按钮。

不知何故,当我单击ItemsPanel 中的一个按钮时,ICommand 不会在绑定到 View.xaml 的 ViewModel 类中触发。 看起来我正在选择 ListBox 项目,而不是 ItemsControl 中的项目。

这是 xaml 代码

<ListBox 
   Background="Transparent" 
   ItemTemplate="{StaticResource ProductGroupTemplate}" 
   FocusVisualStyle="{x:Null}" 
   VerticalContentAlignment="Top" 
   ItemsSource="{Binding NodeHeaders}" 
   Grid.Row="1" 
   Grid.ColumnSpan="3" 
   Grid.Column="0" 
   Grid.RowSpan="2" 
   SelectedItem="{Binding CurrentItem}" 
   BorderBrush="Transparent" 
   ScrollViewer.HorizontalScrollBarVisibility="Auto" 
   ScrollViewer.VerticalScrollBarVisibility="Disabled" 
   Margin="30,0,55,0" 
   VerticalAlignment="Top">
   <ListBox.Style>
      <Style TargetType="ListBox">
         <Setter Property="Visibility" Value="Collapsed" />
         <Style.Triggers>
            <DataTrigger Binding="{Binding HasParent}" Value="True">
               <Setter Property="Visibility" Value="Visible"/>
            </DataTrigger>
         </Style.Triggers>
      </Style>
   </ListBox.Style>
   <ListBox.ItemsPanel>
      <ItemsPanelTemplate>
         <StackPanel Orientation="Horizontal" IsItemsHost="True"/>
      </ItemsPanelTemplate>
   </ListBox.ItemsPanel>
</ListBox>

还有数据模板

<DataTemplate x:Key="ProductGroupTemplate">
    <Grid Margin="0,0,20,0">          
        <Grid.RowDefinitions>
            <RowDefinition Height="40"/>
            <RowDefinition Height="1*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>


        <TextBlock Text="{Binding Description}" FontSize="20" Grid.Row="0" Grid.Column="0" Foreground="{StaticResource DefaultLabelColor}" />
        <ItemsControl ItemsSource="{Binding Nodes}" Grid.Row="1" Grid.Column="0">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Vertical"  IsItemsHost="True"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Text="{Binding Description}" Height="75" Width="150" Padding="5" Margin="5" Background="{StaticResource SalesItemsBackground}" 
                                Foreground="{StaticResource SalesItemsForeground}" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Left" Command="{Binding RelativeSource=
        {RelativeSource FindAncestor, 
        AncestorType={x:Type ContentControl}}, 
        Path=DataContext.Select}" CommandParameter="{Binding}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</DataTemplate>

【问题讨论】:

    标签: wpf xaml mvvm binding listbox


    【解决方案1】:

    这是因为您的数据模板的 DataContext 设置为列表框中的一个项目。如果您将 List 绑定到列表框,则每个列表框项的 DataContext 都将是 Person,这就是为什么您可以编写类似“{Binding Description}”的内容。您有两种方法可以从 DataTemplate 执行命令,其中一种是“Sheridan”告诉您的。作为替代方案,您可以将元素的 DataContext 设置为您的父 View 或 ViewModel。

    <DataTemplate>
        <Grid DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type View/ViewModel}}">
        </Grid>
    </DataTemplate>
    

    【讨论】:

      【解决方案2】:

      如果您有一个声明ICommand 实例并设置为视图的DataContextUserControlWindow)的视图模型,那么您可以使用该属性来访问您的ICommand实例。从您的DataTemplate 中的Button,您可以使用这个RelativeSource Binding

      <Button Text="{Binding Description}" Height="75" Width="150" Padding="5" Margin="5" 
          Background="{StaticResource SalesItemsBackground}" TextAlignment="Left" 
          Foreground="{StaticResource SalesItemsForeground}" HorizontalAlignment="Center" 
          VerticalAlignment="Center" Command="{Binding DataContext.Select, RelativeSource={
          RelativeSource AncestorType={x:Type YourViewsPrefix:YourView}}}"
          CommandParameter="{Binding}"/>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-16
        • 1970-01-01
        • 1970-01-01
        • 2011-06-06
        • 2015-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多