我的綁定有問題。除了所選組合框中顯示的初始值爲空白以外,一切正常。下拉菜單中有兩個值低於最初顯示的空白。任何幫助都會很棒。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>
我曾嘗試過,但我一直把它放在MainWindow.mainWindow.LocationComboBox.DisplayMemberPath =「Name」後面;我只是把它放在最後一行MainWindow.mainWindow.LocationComboBox.SetBinding(ComboBox.SelectedValueProperty,selectedItemBinding); }它的工作謝謝! –
很高興我能幫上忙。 –
這使索引始終是第一個。有沒有辦法設置它,以便根據設置的位置設置它? –