如何在 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; }
}
}
}
App.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
。
我无法绑定我的词典以在 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; }
}
}
}
App.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
。