【问题标题】:WPF drag and drop only working when dropping on the items in the listviewWPF拖放仅在拖放列表视图中的项目时才有效
【发布时间】:2014-02-27 07:50:59
【问题描述】:

所以我有一个列表视图,我在其中设置拖放,但由于某种原因,它只允许我放入列表视图中的实际项目,而不是我覆盖的控制模板的任何部分。如何做到这一点,以便我也可以拖放到包含列标题的文本块上?

     <ListView
        Margin="0,4,0,0"
        Grid.Column="0"
        x:Name="NameListView"
        HorizontalContentAlignment="Stretch"
        VerticalAlignment="Stretch"
        ItemsSource="{Binding Path = AddedItems}"
        SelectionChanged="NameListView_SelectionChanged"
        AllowDrop="True"
        SelectionMode="Extended"
        VirtualizingStackPanel.VirtualizationMode="Standard"
        >

        <ListView.ItemContainerStyle>
          <Style TargetType="ListViewItem">
            <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
            <Setter Property="SnapsToDevicePixels" Value="true"/>
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="Template">
              <Setter.Value>
                <ControlTemplate TargetType="ListViewItem">
                  <Border 
                    Name="Border"
                    Padding="{TemplateBinding Padding}"
                    SnapsToDevicePixels="true"
                    Background="Transparent"
                    >
                    <ContentPresenter 
                      Content="{TemplateBinding Content}"
                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                      />
                  </Border>
                  <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="true">
                      <Setter TargetName="Border"
                              Property="Background" Value="{x:Static SystemColors.HighlightBrush}"
                              />
                    </Trigger>
                  </ControlTemplate.Triggers>
                </ControlTemplate>
              </Setter.Value>
            </Setter>


            <EventSetter Event="PreviewMouseDoubleClick" Handler="NameListView_PreviewMouseDoubleClick"/>
          </Style>
        </ListView.ItemContainerStyle>
        <!-- Until NET 4.0 Keygesture's cannot bind to a command, so the inputbindings must be set using static commands or with code behind-->



        <ListView.Template>
          <!--Template Defining the layout of this treeview-->
          <ControlTemplate>
            <Grid 
              Background="{TemplateBinding Background}"
              >
              <Grid.RowDefinitions>
                <RowDefinition 
                  Height="{Binding GraphHeight, Source={x:Static DaedalusGraphViewer:SettingsManager.AppSettings},
                          Converter={StaticResource GridLengthConverter}}"
                  />
                <RowDefinition Height="*"/>
                <RowDefinition Height="18" />
              </Grid.RowDefinitions>
              <Border
                Grid.ZIndex="1"                    
                Grid.Row="0"
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}"
                >
                <Grid>
                  <TextBlock 
                    Foreground="{TemplateBinding Foreground}"
                    VerticalAlignment="Center"
                    HorizontalAlignment="Center"
                    Text="Signal Names"
                    />
                </Grid>
              </Border>
              <Canvas>
                <Line 
                  Grid.ZIndex="2"
                  x:Name="SelectedItemUnderline"
                  Stroke="Black"
                  StrokeThickness="3"
                  Visibility="Collapsed"
                  />
              </Canvas>
              <ScrollViewer 
                Grid.ZIndex="1"                    
                x:Name="SignalNameScrollViewer" 
                Grid.Row="1" Grid.RowSpan="2" 
                CanContentScroll="False"
                VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Visible" 
                >
                <ItemsPresenter />
              </ScrollViewer>
            </Grid>
          </ControlTemplate>
        </ListView.Template>


      </ListView>

【问题讨论】:

    标签: wpf xaml drag-and-drop


    【解决方案1】:

    我第一次有机会在拖动操作中处理异常,所以当我修复该代码时,它允许我拖放到列表视图中的任何位置。基本上,如果在拖动处理程序中抛出异常,鼠标光标将显示不可放置和失败放置

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-02
      • 1970-01-01
      • 1970-01-01
      • 2019-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多