如何在 ViewModel 中使用 RadDragAndDrop 事件

发布于 2025-01-04 10:56:41 字数 3618 浏览 5 评论 0原文

我正在将 telrik RadDragAndDrop 工具与 ListBox 一起使用。我正在使用 silverlight 和 mvvm light。我的问题是我应该如何在 ViewModel 中使用这段代码。这是一个代码隐藏文件。

 public Construtor()
    {
        InitializeComponent();
        RadDragAndDropManager.AddDragQueryHandler(this, OnDragQuery);
        RadDragAndDropManager.AddDragInfoHandler(this, OnDragInfo);
        RadDragAndDropManager.AddDropQueryHandler(this, OnDropQuery);
        RadDragAndDropManager.AddDropInfoHandler(this, OnDropInfo);
    }
    private void OnDropInfo(object sender, DragDropEventArgs e)
    {
        ItemsControl box = e.Options.Destination as ItemsControl;
        IList<Section> itemsSource = box.ItemsSource as IList<Section>;
        Section section = e.Options.Payload as Section;

        if (e.Options.Status == DragStatus.DropComplete)
        {
            if (!itemsSource.Contains(section))
            {
                itemsSource.Add(section);
            }
        }
    }

    void OnDropQuery(object sender, DragDropQueryEventArgs e)
    {
        ItemsControl box = e.Options.Destination as ItemsControl;
        IList<Section> itemsSource = box.ItemsSource as IList<Section>;
        Section section = e.Options.Payload as Section;
        e.QueryResult = section != null && !itemsSource.Contains(section);
    }

    void OnDragInfo(object sender, DragDropEventArgs e)
    {
        ListBoxItem listBoxItem = e.Options.Source as ListBoxItem;
        ListBox box = ItemsControl.ItemsControlFromItemContainer(listBoxItem) as ListBox;
        IList<Section> itemsSource = box.ItemsSource as IList<Section>;
        Section section = e.Options.Payload as Section;
        if (e.Options.Status == DragStatus.DragComplete)
        {
            if (section != null && itemsSource.Contains(section))
            {
                itemsSource.Remove(section);
            }
        }
    }

    protected virtual void OnDragQuery(object sender, DragDropQueryEventArgs e)
    {
        ListBoxItem listBoxItem = e.Options.Source as ListBoxItem;
        ListBox box = ItemsControl.ItemsControlFromItemContainer(listBoxItem) as ListBox;
        if (e.Options.Status == DragStatus.DragQuery && box != null)
        {
            e.Options.Payload = box.SelectedItem;
            ContentControl cue = new ContentControl();
            cue.Content = box.SelectedItem;
            e.Options.DragCue = cue;
        }
        e.QueryResult = true;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        SelectingQuestionsWindow window = new SelectingQuestionsWindow();
        window.Show();
        this.radExpander1.Visibility = System.Windows.Visibility.Visible;
    }

<*XAML*> 这是我的 Xaml。

        <ListBox x:Name="SectionListBoxMain" Height="165" Width="200" SelectedItem="{Binding SelectedSectionList}"
                        DisplayMemberPath="SectionName" ItemsSource="{Binding SectionList}" ItemContainerStyle="{StaticResource draggableItemStyle}">

            <telerik:RadDragAndDropManager.AllowDrop>
                true
            </telerik:RadDragAndDropManager.AllowDrop>

        </ListBox>

        <TextBlock Width="20" />

        <ListBox x:Name="SectionListBox" Height="167" Width="200" ItemsSource="{Binding SelectedSectionList}" telerik:RadDragAndDropManager.AllowDrop="True" DisplayMemberPath="SectionName" ItemContainerStyle="{StaticResource draggableItemStyle}">

            </ListBox>

    </StackPanel>

I am using telrik RadDragAndDrop Tool with the ListBox. I am using silverlight with mvvm light. My question is that how should I use this code in ViewModel. This is a code behind file.

 public Construtor()
    {
        InitializeComponent();
        RadDragAndDropManager.AddDragQueryHandler(this, OnDragQuery);
        RadDragAndDropManager.AddDragInfoHandler(this, OnDragInfo);
        RadDragAndDropManager.AddDropQueryHandler(this, OnDropQuery);
        RadDragAndDropManager.AddDropInfoHandler(this, OnDropInfo);
    }
    private void OnDropInfo(object sender, DragDropEventArgs e)
    {
        ItemsControl box = e.Options.Destination as ItemsControl;
        IList<Section> itemsSource = box.ItemsSource as IList<Section>;
        Section section = e.Options.Payload as Section;

        if (e.Options.Status == DragStatus.DropComplete)
        {
            if (!itemsSource.Contains(section))
            {
                itemsSource.Add(section);
            }
        }
    }

    void OnDropQuery(object sender, DragDropQueryEventArgs e)
    {
        ItemsControl box = e.Options.Destination as ItemsControl;
        IList<Section> itemsSource = box.ItemsSource as IList<Section>;
        Section section = e.Options.Payload as Section;
        e.QueryResult = section != null && !itemsSource.Contains(section);
    }

    void OnDragInfo(object sender, DragDropEventArgs e)
    {
        ListBoxItem listBoxItem = e.Options.Source as ListBoxItem;
        ListBox box = ItemsControl.ItemsControlFromItemContainer(listBoxItem) as ListBox;
        IList<Section> itemsSource = box.ItemsSource as IList<Section>;
        Section section = e.Options.Payload as Section;
        if (e.Options.Status == DragStatus.DragComplete)
        {
            if (section != null && itemsSource.Contains(section))
            {
                itemsSource.Remove(section);
            }
        }
    }

    protected virtual void OnDragQuery(object sender, DragDropQueryEventArgs e)
    {
        ListBoxItem listBoxItem = e.Options.Source as ListBoxItem;
        ListBox box = ItemsControl.ItemsControlFromItemContainer(listBoxItem) as ListBox;
        if (e.Options.Status == DragStatus.DragQuery && box != null)
        {
            e.Options.Payload = box.SelectedItem;
            ContentControl cue = new ContentControl();
            cue.Content = box.SelectedItem;
            e.Options.DragCue = cue;
        }
        e.QueryResult = true;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        SelectingQuestionsWindow window = new SelectingQuestionsWindow();
        window.Show();
        this.radExpander1.Visibility = System.Windows.Visibility.Visible;
    }

<*XAML*>
This is my Xaml.

        <ListBox x:Name="SectionListBoxMain" Height="165" Width="200" SelectedItem="{Binding SelectedSectionList}"
                        DisplayMemberPath="SectionName" ItemsSource="{Binding SectionList}" ItemContainerStyle="{StaticResource draggableItemStyle}">

            <telerik:RadDragAndDropManager.AllowDrop>
                true
            </telerik:RadDragAndDropManager.AllowDrop>

        </ListBox>

        <TextBlock Width="20" />

        <ListBox x:Name="SectionListBox" Height="167" Width="200" ItemsSource="{Binding SelectedSectionList}" telerik:RadDragAndDropManager.AllowDrop="True" DisplayMemberPath="SectionName" ItemContainerStyle="{StaticResource draggableItemStyle}">

            </ListBox>

    </StackPanel>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦忆晨望 2025-01-11 10:56:41

这是视图逻辑,并不真正属于您的 ViewModel。它可能更适合行为。

请参阅此示例: http://www.telerik.com/help/silverlight /raddraganddrop-within-radgridview.html

他们使用行为并将其附加到网格以启用行重新排序。您可以从以下内容开始:

public partial class ListDragDropBehavior : Behavior<ListBox>

您需要添加一些依赖属性以绑定到其他列表框。

然后,您可以通过简单地将其附加到列表框来在其他列表框中使用此行为(我使用混合来附加行为)

This is view logic that does not really belong in your ViewModel. It is probably better suited to a Behavior.

See this example: http://www.telerik.com/help/silverlight/raddraganddrop-within-radgridview.html

They use a Behavior and attach it to a grid to enable row reordering. You could start with something like:

public partial class ListDragDropBehavior : Behavior<ListBox>

You would need to add some dependency properties to bind to the other list box.

You can then use this Behavior on other list boxes by simply attaching it to the list box (i use blend to attach behaviors)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文