可编辑的Combobox选择的项目未显示

时间:2018-04-19 04:22:10

标签: wpf

我有三个不同的类别(水果,蔬菜和家庭)系列显示为组合框。我试图使这个可编辑,当用户输入一个项目时,如果在集合中可用,则应该选择它,否则添加到集合中。所选项目应显示在组合框中,而item.name应显示在组合框旁边的文本块中。

另外,我有一个带有此组合框默认值的加载按钮,这些按钮没有加载。

有人可以帮忙吗?

<Window x:Class="WpfApp5.Views.Window2"
    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:WpfApp5.Views"
    mc:Ignorable="d"
    Title="Window2" Height="450" Width="800"
    DataContext="{DynamicResource myVMViewModel}">
<Window.Resources>

    <local:myVM x:Key="myVMViewModel"></local:myVM>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <TextBlock Grid.Row="1" Text="Window 2" FontSize="25" HorizontalAlignment="Center"/>

    <ItemsControl Grid.Row="3" ItemsSource="{Binding AttributeCollection}" HorizontalAlignment="Center">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding AttrName}" MinWidth="125" Margin="5"/>
                    <ComboBox ItemsSource="{Binding ItemsCollection}" 
                              MinWidth="125" 
                              Margin="5"
                              DisplayMemberPath="Name"
                              SelectedItem="{Binding SelectedAttr,UpdateSourceTrigger=LostFocus}"
                              IsEditable="True"
                              Text="{Binding NewItem,UpdateSourceTrigger=LostFocus}"/>

                    <TextBlock Text="{Binding SelectedAttr.Name}" MinWidth="125" Margin="5"/>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

    <StackPanel Grid.Row="5">
        <Button Command="{Binding LoadData}" Content="LoadData" HorizontalAlignment="Center" MinWidth="125"></Button>
    </StackPanel>


</Grid>
</Window>

namespace WpfApp5.Views
{
/// <summary>
/// Interaction logic for Window2.xaml
/// </summary>
public partial class Window2 : Window
{
    public Window2()
    {
        InitializeComponent();
    }
}

public class myVM : BindableBase
{
    public DelegateCommand LoadData { get; set; }

    private ObservableCollection<Attribute> attributeCollection;

    public ObservableCollection<Attribute> AttributeCollection
    {
        get { return attributeCollection; }
        set { SetProperty(ref attributeCollection, value); }
    }

    Attribute obj;
    Attribute obj2;
    Attribute obj3;

    public myVM() {

        LoadData = new DelegateCommand(LoadDataClick, CanLoadDataClick);

        AttributeCollection = new ObservableCollection<Attribute>();

        obj = new Attribute();
        obj.AttrName = "Fruits";
        obj.ItemsCollection = new ObservableCollection<itemData>();
        obj.ItemsCollection.Add(new itemData() { ID = 100, Name = "Apple" });
        obj.ItemsCollection.Add(new itemData() { ID = 101, Name = "Banana" });
        obj.ItemsCollection.Add(new itemData() { ID = 102, Name = "Mango" });
        obj.ItemsCollection.Add(new itemData() { ID = 103, Name = "Orange" });

        obj2 = new Attribute();
        obj2.AttrName = "Vegitable";
        obj2.ItemsCollection = new ObservableCollection<itemData>();
        obj2.ItemsCollection.Add(new itemData() { ID = 200, Name = "Potato" });
        obj2.ItemsCollection.Add(new itemData() { ID = 201, Name = "Tomato" });
        obj2.ItemsCollection.Add(new itemData() { ID = 202, Name = "Onion" });
        obj2.ItemsCollection.Add(new itemData() { ID = 203, Name = "Carrot" });


        obj3 = new Attribute();
        obj3.AttrName = "Household";
        obj3.ItemsCollection = new ObservableCollection<itemData>();
        obj3.ItemsCollection.Add(new itemData() { ID = 300, Name = "Towel" });
        obj3.ItemsCollection.Add(new itemData() { ID = 301, Name = "Mop" });
        obj3.ItemsCollection.Add(new itemData() { ID = 302, Name = "Mat" });


        AttributeCollection.Add(obj);
        AttributeCollection.Add(obj2);
        AttributeCollection.Add(obj3);

    }

    private bool CanLoadDataClick()
    {
        return true;
    }

    private void LoadDataClick()
    {
        itemData fruitItem = new itemData{ ID = 102, Name = "Mango" };
        itemData vegItem = new itemData { ID = 200, Name = "Potato" };
        itemData householdItem = new itemData { ID = 300, Name = "Towel" };

        obj.SelectedAttr = fruitItem;
        obj2.SelectedAttr = vegItem;
        obj3.SelectedAttr = householdItem;


    }
}

public class Attribute : BindableBase
{
    private string attrName;
    public string AttrName
    {
        get { return attrName; }
        set { SetProperty(ref attrName, value); }
    }

    private ObservableCollection<itemData> itemsCollection;
    public ObservableCollection<itemData> ItemsCollection
    {
        get { return itemsCollection; }
        set { SetProperty(ref itemsCollection, value); }
    }

    private object selectedAttr;
    public object SelectedAttr
    {
        get { return selectedAttr; }
        set { SetProperty(ref selectedAttr, value); }
    }

    private string newItem;
    public string NewItem
    {
        get { return newItem; }
        set
        {
            if (SelectedAttr != null)
            {
                return;
            }
            if (value != null)
            {
                itemData newValue = new itemData() { ID = 100, Name = value };
                ItemsCollection.Add(newValue);
                SelectedAttr = newValue;
            }

            SetProperty(ref newItem, value);
        }
    }



}

public class itemData : BindableBase
{
    private int id;
    public int ID
    {
        get { return id; }
        set { SetProperty(ref id, value); }
    }

    private string name;
    public string Name
    {
        get { return name; }
        set { SetProperty(ref name, value); }
    }

}
}

1 个答案:

答案 0 :(得分:0)

(我不确定这是您正在寻找的确切答案,但由于我的网站代表,新用户,我无法在您的问题下发表评论)

我可以看到你将ComboxBox绑定到ItemsCollection并显示这些的Name成员。

如果您将ComboBox的项目源加载为ItemsCollection,这是否有帮助?例如

YourComboBoxNameHere.ItemsSource = ItemsCollection;