0

I have a problem with my binding. Everything works except that the initial value displayed in the combo box of the selected is blank. The drop down has the two values below the blank that is originally display. Any help would be fantastic.

Main Class

public partial class MainWindow : Window
{
    public MainWindow()
   {
        InitializeComponent();
        public Data myData = new Data(new LocationSite("There", 9.81234));  
        Binding b = new Binding();
        b.Source = MainWindow.Data.Location;
        b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        b.Path = new PropertyPath("Gravity");
        MainWindow.mainWindow.Gravity.SetBinding(TextBox.TextProperty, b);
        Binding b = new Binding() { Source = MainWindow.Data.LocationSelection };
        MainWindow.mainWindow.LocationComboBox.DisplayMemberPath = "Name";
        MainWindow.mainWindow.LocationComboBox.SetBinding(ComboBox.ItemsSourceProperty, b);
       //bind selection 
        MainWindow.mainWindow.LocationComboBox.DataContext = MainWindow.Data;
        Binding selectedItemBinding = new Binding() { Source = MainWindow.Data, Path = new PropertyPath("Location"), Mode = BindingMode.TwoWay} 
        MainWindow.mainWindow.LocationComboBox.SetBinding(ComboBox.SelectedValueProperty, selectedItemBinding);

        MainWindow.mainWindow.LocationComboBox.SelectedIndex = 0; // always index 0 but might need index 1 how do I make it use whatever location is?


}

}

Data class with a list of Locations and one location that is the selected. Somehow I need to tell the combo box that the one to select is the location that matched the list. Any Help????

public class Data : INotifyPropertyChanged
{
    private LocationSite location;
    private List<LocationSite> locationSelection;

    public Location(LocationSite useLocation)
    {
       location = useLocation; // can either be "Here" or "There" need start index either 0 or 1
       locationSelection = new List<LocationSite>();
       locationSelection.Add(new LocationSite("Here", 9.795884));
       locationSelection.Add(new LocationSite("There", 9.81234));

    }

    public LocationSite Location
    {
       get { return location; }
       set {
            if (location == null)
            {
              location = new LocationSite();
            }
            Location.Gravity = value.Gravity;
            Location.Name = value.Name;
          }
   }

/// <summary>
/// Getter/Setter of a list of LocationSites
/// </summary>
public List<LocationSite> LocationSelection
{
  get { return locationSelection; }
  set { locationSelection = value; }
}

public event PropertyChangedEventHandler PropertyChanged;

void OnPropertyChanged(string propName)
{
    if (this.PropertyChanged != null)
        this.PropertyChanged(
            this, new PropertyChangedEventArgs(propName));
}

}

The object that I have a list of

public class LocationSite : INotifyPropertyChanged
  {
private string name;
private double gravity;

public LocationSite(string siteName, double siteGravity)
{
  Name = siteName;
  Gravity = siteGravity;
}
public string Name
{
  get { return name; }
  set { name = value;
  this.OnPropertyChanged("Name");
  }
}

public double Gravity
{
  get { return gravity; }
  set { gravity = value;
  this.OnPropertyChanged("Gravity");
  }
}
public event PropertyChangedEventHandler PropertyChanged;

void OnPropertyChanged(string propName)
{
    if (this.PropertyChanged != null)
        this.PropertyChanged(
            this, new PropertyChangedEventArgs(propName));
}

 }
}

The XAML file

<Window x:Class="Data.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Needs to be updated" Height="820" Width="1280"  HorizontalAlignment="Left">
 <Grid Name="MainScreenGrid">

    <TextBox x:Name="Gravity" Grid.Column="8" HorizontalAlignment="Left" Height="23" Grid.Row="3" TextWrapping="NoWrap" Text="0.0" VerticalAlignment="Top" Width="140" IsHitTestVisible="False" IsReadOnly="True"/>
    <ComboBox x:Name="LocationComboBox" Grid.Column="6" HorizontalAlignment="Left" Grid.Row="1" VerticalAlignment="Top" Width="140" Height="22"/>
</Grid>
</Window>

2 Answers 2

1

in your constructor try this

LocationComboBox.SelectedIndex = 0;
Sign up to request clarification or add additional context in comments.

6 Comments

I had tried that but I had been putting it after the line MainWindow.mainWindow.LocationComboBox.DisplayMemberPath = "Name"; and I just put it after the last line MainWindow.mainWindow.LocationComboBox.SetBinding(ComboBox.SelectedValueProperty, selectedItemBinding); } and it worked Thanks!
Glad I could be of help.
This makes the index always the first one. Is there a way to set it so that it is set by what ever the location is set to?
When the screen \ form is initialized this will set it to the first item in the index. Re setting of by what the location is set to how do you mean? Is this another combobox or are you wanting to set this by something else earlier?
I changed the code to pass in the location into the constructor of Data. how can I set the initial value to whatever location is.
|
0

In your Data Class Try this

private LocationSite location;

public LocationSite Location
{
   get 
      { 
        return location;
      }

   set 
      {
        location=value;
        OnPropertyChanged("Location")
      }
}

And in MainWindowConstructor Set the Value Like This

MainWindow.Data.Location=MainWindow.Data.LocationSelection.FirstOrDefault();

In this method By default It will Take the First Item of LocationSelection as Location. And You need to Use System.Linq NameSpace for FirstOrDefault().

Set the Location Value Before You Set the Binding.

1 Comment

Try but now get the following error: System.Windows.Data Error: 23 : Cannot convert 'Name' from type 'String' to type LocationSite' for 'en-US' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.