0

From a tutorial I have the following combobox:

<ComboBox Name="comboBox_warnColor" Margin="5,0,5,0" SelectionChanged="comboBox_warnColor_SelectionChanged">
        <ComboBox.ItemTemplate>
                <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                                <Rectangle Fill="{Binding Name}" Width="12" Height="12" Margin="0,2,5,2" />
                                <TextBlock Name="PART_ColorName" Text="{Binding Name}" />
                        </StackPanel>
                </DataTemplate>
        </ComboBox.ItemTemplate>
</ComboBox>

I populate it like this:

comboBox_warnColor.ItemsSource = typeof(Colors).GetProperties();

I want to save the selected item in a text file to load it to the combobox later again.

What value could I save to use it for that scenario? or What would be a good ID value for default defined colors?

SelectedIndex aside I couldn't find one.

Marco Frost
  • 780
  • 3
  • 12
  • 25

2 Answers2

1

You can work with SelectedValuePath and SelectedValue like follows:

<ComboBox SelectedValuePath="Name" SelectedValue="{Binding SelectedName}" Name="comboBox_warnColor" Margin="5,0,5,0">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Rectangle Fill="{Binding Name}" Width="12" Height="12" Margin="0,2,5,2" />
                <TextBlock Name="PART_ColorName" Text="{Binding Name}" />
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

In Code behind:

public class VeryUnrefinedViewmodel
{
    public string SelectedName { get; set; }
}

// ...

VeryUnrefinedViewmodel _DataContextInstance = new VeryUnrefinedViewmodel();
// your initialization code
comboBox_warnColor.ItemsSource = typeof(Colors).GetProperties();
comboBox_warnColor.DataContext = _DataContextInstance;
// _DataContextInstance.SelectedName will contain the name of the selected color

You can programmatically pre-select a color by setting an initial value for SelectedName. If you want to change the selection dynamically, you need to implement INotifyPropertyChanged for the viewmodel.

 VeryUnrefinedViewmodel _DataContextInstance = new VeryUnrefinedViewmodel() { SelectedName = "Yellow" };
grek40
  • 13,113
  • 1
  • 24
  • 50
  • It works, but I think it's a bit inconvenient. On the other hand that may also apply to my program as well. – Marco Frost Aug 08 '16 at 11:31
  • 1
    @MarcoFrost I guess it is inconvenient because your application is not in MVVM design, so you have different boilerplate code. By the way (I didn't even think of that before): you could just access the `comboBox_warnColor.SelectedValue` in code behind instead of working with datacontext and viewmodel... Maybe thats more in line with the rest of your code. – grek40 Aug 08 '16 at 12:18
  • Well, that is just too easy. I didn't know the combo box would accept a string as a value to select an item. There are two child controls in that combo box after all. – Marco Frost Aug 08 '16 at 12:32
  • @MarcoFrost its the awesomeness of `SelectedValuePath` - working like a charm as long as each item has some sort of unique value property. – grek40 Aug 08 '16 at 13:51
  • Ah, I see. So without `SelectedValuePath` there wouldn't be the easy way of `comboBox_warnColor.SelectedValue = "Aqua"`? – Marco Frost Aug 08 '16 at 14:26
  • 1
    @MarcoFrost you can think of `SelectedValue` as `SelectedItem.` for the getter and `SelectedItem = ItemsSource.FirstOrDefault(x => x. == value)` for the setter. Of course, it's a bit more complicated in the end. – grek40 Aug 08 '16 at 14:46
0

RGBA can in unique mode determine a color, so maybe you would want to store that as ID. And you can convert your color to/from RGBA using this question.

Community
  • 1
  • 1
3615
  • 3,787
  • 3
  • 20
  • 35
  • I already thought of that. Problem is, with the mentioned way of populating the ComboBox, I cannot directly select the item via rgba value. – Marco Frost Aug 08 '16 at 11:17
  • You can have a converter in your binding to translate RGBA to Color. – 3615 Aug 08 '16 at 11:19