簡體   English   中英

wpf中的數據綁定組合框

[英]data binding combobox in wpf

后台代碼:

public class TestVariableClass
{
    List<string> States;

    public TestVariableClass()
    {
    States = new List<string> { "MP", "CG"};           
    }
}

XAML:

<Window x:Class="TestApplication.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:TestApplication"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <local:TestVariableClass x:Key="StatesInIt"/>
</Window.Resources>
<Grid>
    <StackPanel Orientation="Horizontal" VerticalAlignment="Top" DataContext="{StaticResource StatesInIt}">
        <Label>States</Label>
        <ComboBox Width="140" Margin="5" x:Name="comboBoxStates" ItemsSource="{Binding States}"></ComboBox>
    </StackPanel>
</Grid>

運行應用程序時,我根本看不到我的comboBox字段被填充。 我的數據綁定方式正確嗎? 我試圖將此comboBox與我的數據庫列連接。 但是在嘗試了幾件事之后,我意識到錯誤在於綁定本身。

您要在模型類中綁定的內容必須是該類的公共屬性:

private List<string> _states;

public List<string> States { get { return _states; } }

注意,您將無法修改狀態集合,也無法在視圖中自動更新它。 為此,您可以使用ObservableCollection而不是List。 但是,您可能不需要該功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM