簡體   English   中英

如何在xaml c#中正確綁定字典?

[英]How to properly bind Dictionary in xaml c#?

我無法綁定我的字典以在 xaml 中顯示。當我將 Months 綁定到 ItemsSource 時,它說No DataContext found for Binding "Months" 我需要 DataContext 嗎?我需要在哪里添加它?

這是我的示例代碼:

namespace DictionaryTest
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            Dictionary<string, Month> m = new Dictionary<string, Month>();
            m.Add("1", new Month("January", 0));
            m.Add("2", new Month("February", 1));
            m.Add("3", new Month("March", 2));
            m.Add("4", new Month("April", 3));

            Resources["Months"] = m;

            InitializeComponent();
        }

        public class Month
        {
            public Month(string name, int popularity)
            {
                Name = name;
                Popularity = popularity;
            }
            public string Name { get; set; }
            public int Popularity { get; set; }
        }
    }
}

應用程序.xaml.cs:

namespace DictionaryTest
{
    public partial class App : Application
    {

    }
}

和我的 MainWindow.xaml:

<Window x:Class="DictionaryTest.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:DictionaryTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <ListView 
            ItemsSource="{Binding Path=Months}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Key"
                            DisplayMemberBinding="{Binding Key}" />
                    <GridViewColumn Header="Name"
                            DisplayMemberBinding="{Binding Value.Name}" />
                    <GridViewColumn Header="Popularity"
                            DisplayMemberBinding="{Binding Value.Popularity}" />
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

您需要將DataContext直接設置為MainWindow (盡管您可能想進一步研究MVVM模式)。

為了實現這一點,您需要將字典聲明為屬性,並設置DataContext ,例如:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        Months = new Dictionary<string, Month>();
        Months.Add("1", new("January", 0));
        Months.Add("2", new("February", 1));
        Months.Add("3", new("March", 2));
        Months.Add("4", new("April", 3));

        InitializeComponent();

        // This line tells the Xaml where to find find the data, in this case it's MainWindow
        DataContext = this;
    }

    public Dictionary<string, Month> Months { get; }

    public class Month
    {
        public Month(string name, int popularity)
        {
            Name = name;
            Popularity = popularity;
        }

        public string Name { get; set; }
        public int Popularity { get; set; }
    }
}

不需要構造函數中的Resources

暫無
暫無

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

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