WPF ListView删除获取单元格

时间:2016-10-01 16:10:13

标签: wpf listview

我正在尝试创建WPF对话框以配置研讨会时间表。 在左侧有一个列表视图,其中一列为教师提供的。名。 在右侧有一个列表视图,有六列,一个用于研讨会, 以及周一至周五的其他五列。

|老师| |工作坊|周一|星期二|周三|周四|星期五|

A             WS-1           A   B
B             WS-2       C
C             WS-3                          B

用户应通过拖放来配置计划,例如老师C教 研讨会WS-2周一举行。

在WinForms中获取drop cell是一项简单的工作,但我找不到方法 在WPF中得到这个。

这是xaml代码的一部分。

<ListView Name="_LV_Teacher"  
                SelectionMode="Single" 
                ItemsSource="{Binding}">
    <ListView.View>
        <GridView>
          <GridViewColumn  
              DisplayMemberBinding="{Binding Path=Id}"          
              Width="0"/>
          <GridViewColumn  
              DisplayMemberBinding="{Binding Path=TeachersName}"    Header="Teacher"/>
        </GridView>
    </ListView.View>
</ListView>

<ListView Name="_LV_Schedule" 
          ItemsSource="{Binding}" 
          AlowDrop="True" 
          Drop="_LV_Teacher_Drop" >
    <ListView.View>
        <GridView>
          <GridViewColumn  
             DisplayMemberBinding="{Binding Path=WorkshopId}" Width="0"/>
          <GridViewColumn  Header="Monday"/>
          <GridViewColumn  Header="Tuesday"/>
          <GridViewColumn  Header="Wednesday"/>
          <GridViewColumn  Header="Thursday"/>
          <GridViewColumn  Header="Friday"/>
        </GridView>
    </ListView.View>
</ListView> 

2 个答案:

答案 0 :(得分:0)

DragDrop HowTo

Challange要弄清楚应该在DragDrop.DoDragDrop()中使用哪个属性。由于我们有不同的ListView,因此使用Teacher实体本身会很有意义。我使用Teacher从左侧ListView获取来源TextBlock.DataContext。并将其设置为DataContext Drop target的{​​{1}}。

问题是,TextBlock使用DataFormats值,因此我使用了DataContext

输出:

Appointment Schedule Output

请参阅下面的示例应用程序。您可以简单地复制/粘贴。

<强> Window1.xaml

IDataObject.GetFormats()

<强> Window1.xaml.cs

<Window x:Class="WpfStackOverflow.MyDragDrop.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfStackOverflow.MyDragDrop"
        Title="Window1" Height="472.557" Width="675.564">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="123*"/>
            <ColumnDefinition Width="380*"/>
        </Grid.ColumnDefinitions>

        <ListView Margin="10,31,10,226" ItemsSource="{Binding TeacherData}">
            <ListView.Resources>
                <Style x:Key="TbKey" TargetType="TextBlock">
                    <EventSetter Event="MouseLeftButtonDown" Handler="Teacher_LeftButtonDown"/>
                    <EventSetter Event="DragEnter" Handler="Teacher_DragEnter"/>

                    <Setter Property="Background" Value="Yellow"/>
                    <Setter Property="AllowDrop" Value="True"/>
                </Style>
            </ListView.Resources>
            <ListView.View>
                <GridView>
                    <GridView.Columns>
                        <GridViewColumn Header="Teachers" Width="75">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Name}" Width="{Binding View.Columns[0].Width, RelativeSource={RelativeSource AncestorType=ListView, Mode=FindAncestor}}"
                                               Style="{StaticResource TbKey}" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView.Columns>
                </GridView>
            </ListView.View>
        </ListView>
        <ListView ItemsSource="{Binding AppointmentData}" Margin="13,31,10,226" Grid.Column="1" ScrollViewer.CanContentScroll="False">
            <ListView.Resources>
                <Style x:Key="TbKey" TargetType="TextBlock">
                    <EventSetter Event="MouseLeftButtonDown" Handler="WeekDay_LeftButtonDown"/>
                    <EventSetter Event="DragEnter" Handler="WeekDay_DragEnter"/>
                    <EventSetter Event="Drop" Handler="WeekDay_Drop"/>
                    <EventSetter Event="MouseRightButtonDown" Handler="WeekDay_MouseRightButtonDown"/>

                    <Setter Property="Background" Value="Yellow"/>
                    <Setter Property="AllowDrop" Value="True"/>
                    <Setter Property="DataContext" Value="{x:Null}"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding DataContext, RelativeSource={RelativeSource Self}}" Value="{x:Null}">
                            <Setter Property="Background" Value="{x:Null}"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ListView.Resources>
            <ListView.View>
                <GridView>                    
                    <GridView.Columns>
                        <GridViewColumn Header="Workshop" Width="75">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Workshop}"/>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn x:Name="ColMon" Header="Mon" Width="45">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock DataContext="{Binding Mon, Mode=TwoWay}" Text="{Binding Name}" Width="{Binding View.Columns[1].Width, RelativeSource={RelativeSource AncestorType=ListView, Mode=FindAncestor}}"
                                               Style="{StaticResource TbKey}" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn  Header="Tue" Width="45">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock DataContext="{Binding Tue, Mode=TwoWay}" Text="{Binding Name}" Width="{Binding View.Columns[1].Width, RelativeSource={RelativeSource AncestorType=ListView, Mode=FindAncestor}}"
                                               Style="{StaticResource TbKey}" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn  Header="Wed" Width="45">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock DataContext="{Binding Wed, Mode=TwoWay}" Text="{Binding Name}" Width="{Binding View.Columns[1].Width, RelativeSource={RelativeSource AncestorType=ListView, Mode=FindAncestor}}"
                                               Style="{StaticResource TbKey}" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn  Header="Thu" Width="45">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock DataContext="{Binding Thu, Mode=TwoWay}" Text="{Binding Name}" Width="{Binding View.Columns[1].Width, RelativeSource={RelativeSource AncestorType=ListView, Mode=FindAncestor}}"
                                               Style="{StaticResource TbKey}" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn  Header="Fri" Width="45">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock DataContext="{Binding Fri, Mode=TwoWay}" Text="{Binding Name}" Width="{Binding View.Columns[1].Width, RelativeSource={RelativeSource AncestorType=ListView, Mode=FindAncestor}}"
                                               Style="{StaticResource TbKey}" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView.Columns>
                </GridView>
            </ListView.View>
        </ListView>
        <DataGrid IsReadOnly="True" AutoGenerateColumns="False" ItemsSource="{Binding AppointmentData}" Grid.Column="1" HorizontalAlignment="Stretch" Margin="13,243,0,0" VerticalAlignment="Top" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="Worskhop" Binding="{Binding Workshop}" Width="*"/>
                <DataGridTextColumn Header="Mon" Binding="{Binding Mon.Name}" Width="*"/>
                <DataGridTextColumn Header="Tue" Binding="{Binding Tue.Name}" Width="*"/>
                <DataGridTextColumn Header="Wed" Binding="{Binding Wed.Name}" Width="*"/>
                <DataGridTextColumn Header="Thu" Binding="{Binding Thu.Name}" Width="*"/>
                <DataGridTextColumn Header="Fri" Binding="{Binding Fri.Name}" Width="*"/>
            </DataGrid.Columns>
        </DataGrid>
        <TextBlock Grid.Column="1" HorizontalAlignment="Left" Margin="13,222,0,0" TextWrapping="Wrap" Text="Real time display of workshop schedules" VerticalAlignment="Top" Width="325"/>
        <TextBlock Grid.Column="1" HorizontalAlignment="Left" Margin="13,10,0,0" TextWrapping="Wrap" Text="Right Click to remove appointment" VerticalAlignment="Top" Width="325"/>
    </Grid>
</Window>

答案 1 :(得分:0)

感谢AnjumSKhan的出色答案! 我已将它实现到我的应用程序中,并且工作正常。不幸的是,我遇到了另一个问题 因为我的数据被检索并存储在数据库中,所以我添加了两个变量:
- int iTeacherId to class TeacherRecord(老师的Listview中第一列宽度为0),以及
- int iWorkShopId to class AppointmentRecord(AppointmentData-Listview中宽度为0的第一列)
因此,除了教师的名字,WeekDay_Drop中的e.Data现在也包含他的Id。 但是如何获得相应的研讨会ID以便在数据库中的Tab_Workshop中保存约会。 (教师的姓名和身份证在Tab_Teacher中,是一个不同的数据库表。)