WPF绑定问题

发布于 2024-10-28 11:33:06 字数 960 浏览 4 评论 0原文

例如,我有:

MainWindows.cs

public partial class MainWindow : Window
        {
            public List<Player> List;
            public MainWindow()
            {
                InitializeComponent();
                List = new List<Player>()
                           {
                               new Player() {Id = 1, Name = "Tom"},
                               new Player() {Id = 2, Name = "Bob"},
                               new Player() {Id = 3, Name = "Any"},
                           };
                comboBox1.DataContext = List;

            }

            public class Player
            {
                public string Name { get; set; }
                public int Id { get; set; }
            }
    }

XAML:

我如何(需要)将 List 设置为 DataContext XAML? (并从代码隐藏中删除“comboBox1.DataContext = List”)

For example, I have:

MainWindows.cs

public partial class MainWindow : Window
        {
            public List<Player> List;
            public MainWindow()
            {
                InitializeComponent();
                List = new List<Player>()
                           {
                               new Player() {Id = 1, Name = "Tom"},
                               new Player() {Id = 2, Name = "Bob"},
                               new Player() {Id = 3, Name = "Any"},
                           };
                comboBox1.DataContext = List;

            }

            public class Player
            {
                public string Name { get; set; }
                public int Id { get; set; }
            }
    }

XAML: <ComboBox ItemsSource="{Binding}" DisplayMemberPath="Name"/>

How I can (need to) set List as a DataContext from the XAML? (and delete "comboBox1.DataContext = List" from the code-behind)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲欢浪云 2024-11-04 11:33:06

除非您使用 MVVM,否则您不需要这样做,但在任何情况下,使用都可以将 List 创建为窗口的属性,如下所示

public List<Player> List {get;set;}

,然后在 XAML 中您可以使用relativeSource 进行绑定到窗口:

<ComboBox ItemsSource="{Binding Path=List, RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}}" DisplayMemberPath="Name"/>

或者,您可以为窗口命名:

<Window .... x:Name="MyWindow" ..>

然后在绑定中使用 ElementName,如下所示:

<ComboBox ItemsSource="{Binding Path=List, ElementName=MyWindow}" DisplayMemberPath="Name"/>

unless you're using MVVM u don't need to do that, but in any case, use can create the List as a property of the window like so

public List<Player> List {get;set;}

and then in XAML u can use RelativeSource to bind to the window:

<ComboBox ItemsSource="{Binding Path=List, RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}}" DisplayMemberPath="Name"/>

alternatively, u can give a name to your window:

<Window .... x:Name="MyWindow" ..>

and then use ElementName in the binding, like so:

<ComboBox ItemsSource="{Binding Path=List, ElementName=MyWindow}" DisplayMemberPath="Name"/>
天煞孤星 2024-11-04 11:33:06

快速修复方法是直接在代码隐藏中设置 ComboBox 的 ItemsSource(而不是 DataContext),但为了能够使用正确的绑定,您需要一个 ViewModel 或至少一个 XAML DataContext。

此外,您还应该为列表选择一些比 List 更独特的名称,例如 Players - 使用列表中对象类型的复数形式是一个很好的做法。

Quick fix is setting your ComboBox's ItemsSource directly in code-behind (instead of DataContext), but in order to be able to use proper bindings you'll need a ViewModel or at least a XAML DataContext.

Also you should pick some more unique name than List for your List, like for example Players – it's good practice to use the plural form of the type of Objects in the List.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文