2015-08-25 21 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: –