0

Let's assume i have the following class:

class SimpleData
{
    public string DisplayName { get; set; }
    public bool IsDefault { get; set; }
}

Now i've stored a list of SimpleData in a list and set the list as DataContext. I have binded to the lit like this:

<ComboBox x:Name="layoutComboBox" VerticalAlignment="Center" Padding="3,0,3,0" Height="20" Margin="3,0,0,0" MinWidth="100"
ItemsSource="{Binding Path=GridConfigurationProfiles}"
DisplayMemberPath="DisplayName"
SelectedValuePath="IsDefault"
/>

Which works pretty well for the DisplayMemberPath, but i don't get the item selected as default, which has IsDefault = True.

The question: how to bind SelectedValue to True so the item will be selected, which has IsDefault = True. If the condition is for more than one item true, the system should take what ever it want's to.

Thank you.

BendEg
  • 20,098
  • 17
  • 57
  • 131

3 Answers3

1

Think about it: IsDefault is not unique for every item. So, when you bind SelectedValue, what should ComboBox do if there are several items with IsDefault == true?

Also, SelectedValue is just value of selected ComboBox item that is determined by SelectedValuePath.

I'd recommend you to bind SelectedItem rather than SelectedValue:

<ComboBox x:Name="layoutComboBox" VerticalAlignment="Center" Padding="3,0,3,0" Height="20" Margin="3,0,0,0" MinWidth="100"
ItemsSource="{Binding Path=GridConfigurationProfiles}"
DisplayMemberPath="DisplayName"
SelectedValuePath="IsDefault"
SelectedItem="{Binding SelectedConfigurationProfile, Mode=TwoWay}"
/>

In ViewModel:

private _selectedConfigurationProfile;
public SimpleData SelectedConfigurationProfile
{
    get { return _selectedConfigurationProfile; }
    set
    {
        if (_selectedConfigurationProfile != value)
        {
            _selectedConfigurationProfile = value;
            //NotifyPropertyChanged("SelectedConfigurationProfile"); if needed
        }
    }
}

public void MethodThatSetsDefault()
{
    SelectedConfigurationProfile = GridConfigurationProfiles.FirstOrDefault(q => q.IsDefault);
}
keymusicman
  • 1,281
  • 1
  • 10
  • 20
  • You are right, have a property like `SelectedProfile` is more straight forward. I've just tried to use `IsDefault`, becuase it is a column in our database. I've changed it to a normal property. – BendEg Aug 21 '15 at 08:13
  • Works perfectly, but i do not need this: `SelectedValuePath="IsDefault"` any mode. – BendEg Aug 21 '15 at 08:28
1

From MSDN

The SelectedValuePath property specifies the path to the property that is used to determine the value of the SelectedValue property.

This basically means that if you set the property like you did SelectedValuePath="IsDefault" the SelectedValue property will be the value of IsDefault property of the selected item.

What you still have to do is to provide a value for the SelectedValue so that it initializes the SelectedItem. You can use a binding or just set the property to true.

<ComboBox x:Name="layoutComboBox"
          VerticalAlignment="Center"
          Padding="3,0,3,0" 
          Height="20"
          Margin="3,0,0,0" MinWidth="100"
          ItemsSource="{Binding Path=GridConfigurationProfiles}"
          DisplayMemberPath="DisplayName"
          SelectedValuePath="IsDefault" 
          SelectedValue="True"/>
Novitchi S
  • 3,711
  • 1
  • 31
  • 44
-1

Try to use converter:

public class Converter:IValueConverter
{
    public object Convert(object value, Type targetType,
                    object parameter, CultureInfo culture)
    {
        IEnumerable<SimpleData> data=value as IEnumerable<SimpleData>;
        if(data==null) return null;
        else return data.First(x=>x.IsDefault);
    }

    public object ConvertBack(object value, Type targetType, 
                  object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

and Binding:

<local:Converter x:Key="converter"/>
<ComboBox SelectedItem="{Binding 
          Converter="{StaticResource converter}}"/>

or something similar to this one.

bakala12
  • 368
  • 2
  • 15
  • This doesn't work and even if it did it is bad form... Value converters are used in Data Conversions. That said, you don't do default selections or Business logic in converters, you have the ViewModel and Models for that. – Novitchi S Aug 21 '15 at 08:42
  • Thanks for your comment. I'm new and quite inexperienced in wpf, especially in MVVM. Sorry for bad advice. – bakala12 Aug 21 '15 at 08:47