ComboBoxes共享Observable Collection不断打破 [英] ComboBoxes sharing Observable Collection keeps breaking

查看:134
本文介绍了ComboBoxes共享Observable Collection不断打破的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有四个组合框经历一个奇怪的怪癖。我很难解释究竟发生了什么...所以我把它放在一个视频:

I have four comboboxes that are experiencing a weird quirk. Its hard for me to explain exactly whats going on... so I put it in a video:

https://www.youtube.com/watch?v=OuB-kSGMMSo

<ComboBox x:Name="images1" Grid.Row="0" Grid.Column="0" Margin="3"/>
<ComboBox x:Name="images2" Grid.Row="0" Grid.Column="1" Margin="3"/>
<ComboBox x:Name="images3" Grid.Row="0" Grid.Column="2" Margin="3"/>
<ComboBox x:Name="images4" Grid.Row="0" Grid.Column="3" Margin="3"/>

代码

public ObservableCollection<ComboBoxItem> images =
    new ObservableCollection<ComboBoxItem>();

public ImageSelect(MainWindow mainWindow, string tab_name)
{
    InitializeComponent();

    Generic.ImportImages(tab_name, images1, images);

    images2.ItemsSource = images;
    images3.ItemsSource = images;
    images4.ItemsSource = images;
}

我的班级

public static void ImportImages(string tabID, ComboBox combo, ObservableCollection<ComboBoxItem> list)
{
    try
    {
        string root = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
        var files = Directory.GetFiles(Path.Combine(root, "input\\" + tabID), "*.png");

        foreach (var file in files)
        {
            string[] paths = file.Split('\\');
            ComboBoxItem item = new ComboBoxItem();
            item.Tag = paths.Last();

            var stack = new StackPanel() { Orientation = Orientation.Horizontal };
            stack.Children.Add(new Image() { Source = new BitmapImage(new Uri(file, UriKind.Absolute)), Height = 44 });
            stack.Children.Add(new Label() { Content = paths.Last(), VerticalContentAlignment = VerticalAlignment.Center });
            item.Content = stack;

            list.Add(item);
        }
    }
    catch (Exception) { }

    combo.ItemsSource = list;
}


推荐答案

基本WPF示例。请查看MVVM模式,WPF数据绑定和数据模板。

Basic WPF example. Please have a look at MVVM patter, WPF data binding and data templates.

查看定义

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">

<Window.Resources>

    <DataTemplate x:Key="ComboBoxItemTemplate">
        <StackPanel Orientation="Horizontal">
            <Image Height="44" Source="{Binding ImageUrl}"/>
            <Label Content="{Binding Name}" VerticalAlignment="Center"/>
        </StackPanel>
    </DataTemplate>

</Window.Resources>

<StackPanel Orientation="Horizontal">

    <ComboBox x:Name="cbOne" ItemTemplate="{StaticResource ComboBoxItemTemplate}"/>
    <ComboBox x:Name="cbTwo" ItemTemplate="{StaticResource ComboBoxItemTemplate}"/>
    <ComboBox x:Name="cbThree" ItemTemplate="{StaticResource ComboBoxItemTemplate}"/>
    <ComboBox x:Name="cbFour" ItemTemplate="{StaticResource ComboBoxItemTemplate}"/>

</StackPanel>

CodeBehind

CodeBehind

 public partial class MainWindow : Window
{
    private ObservableCollection<ImageInfo> _images;

    public MainWindow()
    {
        InitializeComponent();

        _images = new ObservableCollection<ImageInfo>(GetImages());

        cbOne.ItemsSource = _images;
        cbTwo.ItemsSource = _images;
        cbThree.ItemsSource = _images;
        cbFour.ItemsSource = _images;
    }

    public IEnumerable<ImageInfo> GetImages()
    {
        const string path = @"C:\_D\_Log\";

        var items = from x in Directory.GetFiles(path, "*.png")
                    select new ImageInfo
                    {
                        ImageUrl = x,
                        Name = System.IO.Path.GetFileName(x)
                    };
        return items;
    }
}

public class ImageInfo
{
    public string ImageUrl { get; set; }
    public string Name { get; set; }
}

这篇关于ComboBoxes共享Observable Collection不断打破的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆