我是WPF的新手...
我正在嘗試將串列系結到新視窗中的組合框。
該串列屬于以下類別:
public class TestClass
{
public List<string> ComboBoxItems = new List<string>
{
"Item 1", "Item2"
};
}
從 MainWindow 創建一個新的 TestWindow:
private void TestWindow_Click(object sender, RoutedEventArgs e)
{
var test = new TestClass();
var win = new TestWindow { DataContext = test };
win.Show();
}
在 XAML 中,我嘗試將 ComboBoxItems 系結到 ComboBox,例如:
<Window ...
Title="TestWindow" Height="200" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<ComboBox Name="combo" ItemsSource="{Binding ComboBoxItems}"/>
</Grid>
</Window>
我想當我將這個新視窗的 DataContext 設定為我的 TestClass 的一個實體時,我可以訪問它的成員(包括串列 ComboBoxItems)并可以系結它們。
但是 ComboBox 是空的:

你能告訴我我做錯了什么嗎?
順便說一句:在 XAML 編輯器中沒有 IntelliSense?

有什么建議嗎?
謝謝!
uj5u.com熱心網友回復:
ComboBoxItems需要是屬性而不是欄位才能成為 DataBinding 的來源
public List<string> ComboBoxItems { get; } = new List<string>
{
"Item 1", "Item2"
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/436186.html
