2016-08-03 122 views
-1

我的組合框項目未顯示。它在Visual Studio 2015上運行良好。但是,當我在Visual Studio 2013中嘗試這個功能時,它什麼也沒有顯示。我在ComboBox_Loaded函數中設置了調試點,從中我看到最後3行被編譯器忽略。我如何解決它的Visual Studio 2013.在此先感謝。ComboBox項目未加載WPF

<Window x:Class="GraphicalUserInterface.ShowDataByObjectsWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="ShowDataByObjectsWindow" Height="300" Width="300"> 
<Grid Background="#FFE5E5E5"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 
    <StackPanel Grid.Row="0" Margin="10"> 
     <TextBlock FontWeight="Bold" Text="Object Options"/> 
     <ComboBox x:Name="dbObjects" Loaded="ComboBox_Loaded" SelectionChanged="ComboBox_SelectionChanged"/> 
    </StackPanel> 
</Grid> 

public partial class ShowDataByObjectsWindow : Window 
{ 
    public List<string> dataTableName = new List<string>(); 
    public static string comboItem; 

    public ShowDataByObjectsWindow() 
    { 
     InitializeComponent();   
    } 

    private void ComboBox_Loaded(object sender, RoutedEventArgs e) 
    { 
     dataTableName.Add("adasd"); 
     dataTableName.Add("adaasdsd"); 


     var comboBox = sender as ComboBox; 
     comboBox.ItemsSource = dataTableName; 
     comboBox.SelectedIndex = 0; 
    } 

    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     var comboBox = sender as ComboBox; 

     string value = comboBox.SelectedItem as string; 
     this.Title = "Selected: " + value; 
    } 
} 
+0

「我已經看到最後3行被編譯器跳過了」:嘗試清理並強制重建項目。看起來你運行的是WPF應用程序的老式版本。 –

+0

我已經清理並再次重建。它仍然是一樣的。沒有更新 – Aarav

回答

1

試試這個

public ObservableCollection<String> Items { get; set; } 

//public 
public MainWindow() 
{ 
    InitializeComponent(); 

    Items = new ObservableCollection<string>(); 
    Items.Add("test"); 

    DataContext = this; 
} 

,並更改您的視圖

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <ComboBox HorizontalAlignment="Left" ItemsSource="{Binding Path=Items}" Margin="155,56,0,0" VerticalAlignment="Top" Width="120"/> 

    </Grid> 
</Window> 

它的作品!

+0

嗨你的例子工作。但是,無論何時我嘗試在項目中添加數據庫表名稱,它都有一個異常,如「在System.Data.Entity.dll中發生類型'System.ArgumentException'的異常。 其他信息:指定的存儲提供程序不能在配置中找到或無效。「代碼示例如下: 代碼示例爲: 'var eData = new DBEntities(); Items = new ObservableCollection (); Items.Add(「test」); Items.Add(「adasd」); Items.Add(「adaasdsd」); Items.Add(Convert.ToString(eData.Table1));' – Aarav