2015-08-25 130 views
0

我的綁定有問題。除了所選組合框中顯示的初始值爲空白以外,一切正常。下拉菜單中有兩個值低於最初顯示的空白。任何幫助都會很棒。C#綁定列表到組合框

主類

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> 

回答

1
在構造函數

試試這個

LocationComboBox.SelectedIndex = 0; 
+0

我曾嘗試過,但我一直把它放在MainWindow.mainWindow.LocationComboBox.DisplayMemberPath =「Name」後面;我只是把它放在最後一行MainWindow.mainWindow.LocationComboBox.SetBinding(ComboBox.SelectedValueProperty,selectedItemBinding); }它的工作謝謝! –

+0

很高興我能幫上忙。 –

+0

這使索引始終是第一個。有沒有辦法設置它,以便根據設置的位置設置它? –

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()

設置Location之前的值設置Binding

+0

嘗試但現在得到以下錯誤:System.Windows.Data錯誤:23:無法轉換類型'字符串'的'名稱'以默認轉換爲'en-US'文化輸入LocationSite';考慮使用Binding的Converter屬性。 NotSupportedException異常:的「System.NotSupportedException: –