代码之家  ›  专栏  ›  技术社区  ›  lecloneur

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

  •  -1
  • lecloneur  · 技术社区  · 7 年前

    你好,我在用 Gong WPF 重新排序 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 要在列表框中启用拖放:

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

    列表框 ItemSource 绑定到 Layer 总的来说 ViewModel .

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

    现在,层只是一个空类,用于显示问题:

    public class Layer
    {
    
    }
    

    这个 UserControl 用作 DataTemplate 包含一个 ComboBox 以下内容:

    <ComboBox Height="25" Width="100">
        <ComboBoxItem>HELLO</ComboBoxItem>
        <ComboBoxItem>BONJOUR</ComboBoxItem>
        <ComboBoxItem>NIHAO</ComboBoxItem>
    </ComboBox>
    

    现在,当我使用拖放在 ListBox ,坠落的 组合框 SelectedItem 不再可见。

    为什么?

    谢谢

    1 回复  |  直到 7 年前
        1
  •  1
  •   mm8    7 年前

    为什么?

    因为您没有将它绑定到 Layer 类。如果添加如属性:

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

    …并将 SelectedItem 的属性 ComboBox 致:

    <ListBox 
        dd:DragDrop.IsDragSource="True"
        dd:DragDrop.IsDropTarget="True"
        ItemsSource="{Binding Layers}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <ComboBox Height="25" Width="100" SelectedItem="{Binding SelectedItem}"
                          xmlns:s="clr-namespace:System;assembly=mscorlib">
                    <s:String>HELLO</s:String>
                    <s:String>BONJOUR</s:String>
                    <s:String>NIHAO</s:String>
                </ComboBox>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    …对我来说很好。