WPF ComboBox重新加载和设置displayite,它是一个对象的对象

时间:2016-05-23 11:11:18

标签: c# wpf

我一直在使用基于WPF ComboBox的定制控件

下面的代码

enter code here<UserControl x:Class="wpfColorCombo.ColorPicker"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="32" d:DesignWidth="190">
<UserControl.Resources>
    <DataTemplate x:Key="LoadedValue">
        <StackPanel Orientation="Horizontal">
            <TextBlock Width="10"
                       Height="10"
                       Margin="5"
                       Background="Aqua"/>
            <TextBlock Text="Aqua"
                       Margin="5"/>
        </StackPanel>
    </DataTemplate>
</UserControl.Resources>
<Grid>
    <ComboBox x:Name="colorPickerCombo"
              ItemsSource="{Binding Path=FontColors}"
              DropDownClosed="colourPickerCombo_DropDownClosed"
              SelectedValue="{Binding Path=SelectedFontColor, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
              IsSynchronizedWithCurrentItem="True"
              Loaded="colourPickerCombo_Loaded">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Width="10"
                               Height="10"
                               Margin="5"
                               Background="{Binding Name}"/>
                    <TextBlock Text="{Binding Name}"
                               Margin="5"/>
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox> 
</Grid>

背后的控制代码:

    public partial class ColorPicker : UserControl
{

    public ColorPicker()
    {
        InitializeComponent();
        DataContext = new viewModelColorPicker();
    }
}

除非我使用已保存的数据重新加载控件,否则此处没有任何异常。我似乎无法找到获得显示的方法。我已经找了一个解决方案的最后半天,而我最近的一个是基于Grid控件中的类似问题。解决方案是将Windows资源(在这种情况下为LoadedValue)分配给内容。在ComboBox中我有文字。我试图利用TextBlock,但没有堆栈面板来帮助布局我被卡住了。

任何可能让我更接近的帮助都很好:)

干杯愤怒的鲍勃

3 个答案:

答案 0 :(得分:0)

我见过这样的问题,当你覆盖有界实例时,大多会发生这些问题。

例如,当您加载数据时,(例如使用xmlserializer) FontColors的新实例将覆盖旧实例。组合框仍然与旧实例绑定,因此新的实例不会出现。

因此,您可能需要在加载后重新分配/绑定ItemsSource Combobox

答案 1 :(得分:0)

这是

背后的代码
public class viewModelColorPicker : ViewModelBase 
{
    modelFont colours = new modelFont();

    public viewModelColorPicker()
    {
    }

    private ObservableCollection<FontColor> fontColours;
    public ObservableCollection<FontColor> FontColours
    {
        get 
        {
            fontColours = colours.AvalibleColours;
            return fontColours;
        }
    }

    private FontColor selectedColour = new FontColor("Black",Brushes.Black); 
    public FontColor SelectedColour
    {
        get 
        {
            selectedColour = colours.SelectedColor.Coalesce(new FontColor("Black",Brushes.Black));                
            return selectedColour; 
        }
        set 
        { 
            SetProperty(ref selectedColour, value, "SelectedColour");
            colours.SelectedColor = value;
        }
    }

}

这三个类是模型

    public class modelFont : ViewModelBase 
{

    ObservableCollection<FontColor> availableColours = new ObservableCollection<FontColor>();

    public modelFont()
    {
        AvailableColors AvCol = new AvailableColors();
        availableColours = AvCol.GetFontColorsList().ToObservableCollection();
    }

    public ObservableCollection<FontColor> AvalibleColours
    {
        get { return availableColours; }
        set { SetProperty(ref this.availableColours, value, "AvalibleColours"); }
    }

    private FontColor selectedColor;
    public FontColor SelectedColor
    {
        get { return selectedColor; }
        set { SetProperty(ref this.selectedColor, value, "SelectedColor"); }
    }
}


public class FontColor
    {
        public string Name { get; set; }
        public SolidColorBrush Brush { get; set; }

        public FontColor(string name, SolidColorBrush brush)
        {
            Name = name;
            Brush = brush;
        }

        public override bool Equals(System.Object obj)
        {
            if (obj == null)
            {
                return false;
            }

            FontColor p = obj as FontColor;
            if ((System.Object)p == null)
            {
                return false;
            }

            return (this.Name == p.Name) && (this.Brush.Equals(p.Brush));
        }

        public bool Equals(FontColor p)
        {
            if ((object)p == null)
            {
                return false;
            }

            return (this.Name == p.Name) && (this.Brush.Equals(p.Brush));
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override string ToString()
        {
            return "FontColor [Color=" + this.Name + ", " + this.Brush.ToString() + "]";
        }
    }
}



class AvailableColors : List<FontColor>
    {
        #region Conversion Utils Static Methods
        public static FontColor GetFontColor(SolidColorBrush b)
        {
            AvailableColors brushList = new AvailableColors();
            return brushList.GetFontColorByBrush(b);
        }

        public static FontColor GetFontColor(string name)
        {
            AvailableColors brushList = new AvailableColors();
            return brushList.GetFontColorByName(name);
        }

        public static FontColor GetFontColor(Color c)
        {
            return AvailableColors.GetFontColor(new SolidColorBrush(c));
        }

        public static int GetFontColorIndex(FontColor c)
        {
            AvailableColors brushList = new AvailableColors();
            int idx = 0;
            SolidColorBrush colorBrush = c.Brush;

            foreach (FontColor brush in brushList)
            {
                if (brush.Brush.Color.Equals(colorBrush.Color))
                {
                    break;
                }

                idx++;
            }

            return idx;
        }
        #endregion

        public AvailableColors()
            : base()
        {
            this.Init();
        }

        public FontColor GetFontColorByName(string name)
        {
            FontColor found = null;
            foreach (FontColor b in this)
            {
                if (b.Name == name)
                {
                    found = b;
                    break;
                }
            }
            return found;
        }

        public FontColor GetFontColorByBrush(SolidColorBrush b)
        {
            FontColor found = null;
            foreach (FontColor brush in this)
            {
                if (brush.Brush.Color.Equals(b.Color))
                {
                    found = brush;
                    break;
                }
            }
            return found;
        }

        private void Init()
        {
            Type brushesType = typeof(Colors);
            var properties = brushesType.GetProperties(BindingFlags.Static | BindingFlags.Public);

            foreach (var prop in properties)
            {
                string name = prop.Name;
                SolidColorBrush brush = new SolidColorBrush((Color)(prop.GetValue(null, null)));
                this.Add(new FontColor(name, brush));
            }

        }
    }

这些是辅助类

    public static class Ext_CollectionExtensions
{
    public static ObservableCollection<DataRow> ToObservableCollection(this DataTable coll)
    {
        var c = new ObservableCollection<DataRow>();
        foreach (DataRow e in coll.Rows)
            c.Add(e);
        return c;
    }

    public static ObservableCollection<T> ToObservableCollection<T>(this List<T> ListOfT)
    {
        ObservableCollection<T> returned = new ObservableCollection<T>();
        foreach (var itm in ListOfT)
        {
            returned.Add(itm);
        }
        return returned;
    }
}

    public static class Ext_Object
{
    public static T Coalesce<T>(this T obj, params T[] args)
    {
        if (obj != null || Convert.IsDBNull(obj))
            return obj;

        foreach (T arg in args)
        {
            if (arg != null || Convert.IsDBNull(obj))
                return arg;
        }

        return default(T);
    }
}

我已将代码修改为更简洁。我没有写出我在解决这个问题上没有帮助的原文,因为它过于复杂和冗长。

答案 2 :(得分:0)

经过多次放屁后的答案是

this.SelectedColor = this.FontColors.Where(a => a.Name == "Black").FirstOrDefault();

全部因为选择是在供应集合中选择值而不是设置要显示的值。