文章分類/

Infragistics|Ultimate UI for WPF|使用拖放框架交換使用者控制項的位置

344 瀏覽人次
2023-12-02 更新

infragistics log

使用 Infragistics 拖放框架允許透過拖放來重新定位標籤控制項。

將 Infragistics 拖放框架合併到標籤控制項的父元素 Grid 中。

<ItemsControl ItemsSource="{Binding Tasks}" Width="650" Margin="17,10,17,10">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Border BorderBrush="LightGray" Background="White" BorderThickness="2" Margin="0,0,-1,2" >
                <Grid>
                    ...
                    <Label Content="{Binding Id}" Grid.RowSpan="2"  />
                    <Label Content="{Binding Title}" Grid.Column="1"  Grid.Row="0"/>
                    <Label Content="{Binding Assignee}" Grid.Column="1" Grid.Row="1" />
                </Grid>
                <!--追加-->
                <ig:DragDropManager.DragSource>
                    <ig:DragSource IsDraggable="True" DragChannels="ChannelA" Drop="DragSource_Drop">
                    </ig:DragSource>
                </ig:DragDropManager.DragSource>
                <ig:DragDropManager.DropTarget>
                    <ig:DropTarget IsDropTarget="True" DropChannels="ChannelA">
                    </ig:DropTarget>
                </ig:DragDropManager.DropTarget>
                <!--追加-->
            </Border>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

處理 DragSource.Drop 事件並交換資料來源的位置。

public partial class MainWindow : Window
{
    ...

    private void DragSource_Drop(object sender, Infragistics.DragDrop.DropEventArgs e)
    {
        var sourcePanel = e.DragSource as Border;
        var sourceTask = sourcePanel.DataContext as Task;

        var targetPanel = e.DropTarget as Border;
        var targetTask = targetPanel.DataContext as Task;

        var vm = this.DataContext as TaskViewModel;
        var tasks = vm.Tasks;

        var sourceIndex = tasks.IndexOf(sourceTask);
        var targetIndex = tasks.IndexOf(targetTask);

        if(sourceIndex < targetIndex)
        {
            tasks.Insert(targetIndex + 1, sourceTask);
            tasks.Remove(sourceTask);
        }
        else
        {
            tasks.Remove(sourceTask);
            tasks.Insert(targetIndex, sourceTask);
        }
    }
}

執行結果

快速跳轉目錄

✦ 群昱 AccessSoft 你的全面軟體解決方案 ✦

身為全球眾多知名軟體在台灣合作夥伴,歡迎諮詢你需要的軟體服務!

Picture of 軟體專家
軟體專家

群昱作為全球知名軟體推薦合作夥伴,致力於提供更多軟體解決方案給你!

更多軟體新知

立即詢價

請留下完整資訊,以便我們提供精確的服務內容給你。

詢價資訊