C#绑定列表到ComboBox

时间:2015-08-25 21:05:15

标签: c# wpf combobox

我的绑定有问题。除了所选组合框中显示的初始值为空白外,一切正常。下拉列表中的两个值低于最初显示的空白。任何帮助都会很棒。

主类

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?


}

}

包含位置列表和一个选定位置的数据类。不知何故,我需要告诉组合框,要选择的是与列表匹配的位置。任何帮助????

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));
}

}

我有

列表的对象
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));
}

 }
}

XAML文件

<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 个答案:

答案 0 :(得分:1)

在你的构造函数中试试这个

LocationComboBox.SelectedIndex = 0;

答案 1 :(得分:0)

在您的数据类中试试这个

private LocationSite location;

public LocationSite Location
{
   get 
      { 
        return location;
      }

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

并在MainWindowConstructor设置像这样的值

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

在此方法中默认情况下,它将LocationSelection的第一项作为Location。 您需要使用System.Linq NameSpace FirstOrDefault()

在设置Location之前设置Binding值。