簡體   English   中英

C#綁定列表到ComboBox

[英]C# Binding List to 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>

在您的構造函數中嘗試這個

LocationComboBox.SelectedIndex = 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 FirstOrDefault()空間用於FirstOrDefault()

在設置Binding之前設置Location值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM