I have an combobox where I provide the options and the value to set from my data context. The xaml looks like this:
<ComboBox HorizontalAlignment="Left" Width="120"
ItemsSource="{Binding Options}"
DisplayMemberPath="Text"
SelectedValuePath="OptionValue"
SelectedValue="{Binding Value, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
If Value and OptionValue is an int in my datacontext everything works fine. Example:
public class MyDataContext : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public class TextValue
{
private string _Text;
private int _Value;
public TextValue(string text, int value)
{
_Text = text;
_Value = value;
}
public int OptionValue
{
get
{
return _Value;
}
}
public string Text
{
get
{
return _Text;
}
}
}
private TextValue[] _Options = new TextValue[]
{
new TextValue("One", 1),
new TextValue("Two", 2),
new TextValue("Three", 3)
};
public TextValue[] Options
{
get
{
return _Options;
}
}
private int _Value = 1;
public int Value
{
get
{
return _Value;
}
set
{
if (_Value != value)
{
System.Diagnostics.Debug.WriteLine("New value set: " + value);
_Value = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Value"));
}
}
}
}
}
But if Value and OptionValue instead is an enum no default value is set in the combo box. If I change option in the combobox Value is then set so I assume the databinding works. Why is it like this? This strange behaviour is only in Windows Store apps, if the run the same in an WPF application things are working fine. Enum-code:
public class MyDataContext : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public enum Number { One, Two, Three }
public class TextValue
{
private string _Text;
private Number _Value;
public TextValue(string text, Number value)
{
_Text = text;
_Value = value;
}
public Number OptionValue
{
get
{
return _Value;
}
}
public string Text
{
get
{
return _Text;
}
}
}
private TextValue[] _Options = new TextValue[]
{
new TextValue("One", Number.One),
new TextValue("Two", Number.Two),
new TextValue("Three", Number.Three)
};
public TextValue[] Options
{
get
{
return _Options;
}
}
private Number _Value = Number.One;
public Number Value
{
get
{
return _Value;
}
set
{
if (_Value != value)
{
System.Diagnostics.Debug.WriteLine("New value set: " + value);
_Value = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Value"));
}
}
}
}
}