Gong WPF问题与列表框项目内的ComboBox

时间:2018-06-25 03:29:57

标签: c# wpf combobox

您好,我正在使用 Gong WPF对ListBox中的Items重新排序

<Window x:Class="ComboBoxIssue.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ComboBoxIssue"
    xmlns:dd="urn:gong-wpf-dragdrop"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<ListBox 
    dd:DragDrop.IsDragSource="True"
    dd:DragDrop.IsDropTarget="True"
    ItemsSource="{Binding Layers}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <local:UserControl1/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

GongWpf提供AttachedProperties来启用ListBox中的拖放:

dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True"

列表框ItemSource绑定到主LayerViewModel的ObservableCollection。

public class ViewModel
{
    public ViewModel()
    {
        Layers = new ObservableCollection<Layer> { new Layer(), new Layer(), new Layer(), new Layer() };
    }
    public ObservableCollection<Layer> Layers { get; }
}

目前,Layer只是一个空类,用于显示问题:

public class Layer
{

}

用作UserControl的{​​{1}}包含一个DataTemplate

ComboBox

现在,当我在<ComboBox Height="25" Width="100"> <ComboBoxItem>HELLO</ComboBoxItem> <ComboBoxItem>BONJOUR</ComboBoxItem> <ComboBoxItem>NIHAO</ComboBoxItem> </ComboBox> 中使用拖放功能对商品进行重新排序时,所放下的ListBox ComboBox不再可见。

为什么?

谢谢

1 个答案:

答案 0 :(得分:1)

  

为什么?

因为尚未将其绑定到Layer类的源属性。如果添加诸如属性:

public class Layer
{
    private string _selectedItem;
    public string SelectedItem
    {
        get => _selectedItem;
        set => _selectedItem = value;
    }
}

...并将SelectedItem的{​​{1}}属性绑定到它:

ComboBox

...对我来说很好。