如何在 XAML 中将 List 作为 ItemSource 绑定到 ListView?

发布于 2024-09-05 05:47:56 字数 2202 浏览 2 评论 0原文

我正在学习 WPF,希望有一个类似于 LinkedList 的集合,以便我可以在其中添加和删除字符串。我想要一个 ListView 来通过数据绑定监听该集合。如何在 XAML 中将简单列表集合绑定到 ListView

我的想法(不起作用)是这样的:

<Window x:Class="TestApp.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <LinkedList x:Key="myList"></LinkedList> //Wrong
    <Window.Resources>
    <Grid>
    <ListView Height="100" HorizontalAlignment="Left" Margin="88,134,0,0" 
      Name="listView1" VerticalAlignment="Top" Width="120" 
      ItemsSource="{Binding Source={StaticResource myList}}"/> //Wrong
    </Grid>
</Window>

我的所有代码(更新版本,不起作用):

<Window x:Class="TestApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" 
          Name="textBox1" VerticalAlignment="Top" Width="120" />
        <Button Content="Button" Height="23" HorizontalAlignment="Right" 
          Margin="0,12,290,0" Name="button1" VerticalAlignment="Top" Width="75" 
          Click="button1_Click" />
        <ListView Height="100" HorizontalAlignment="Left" Margin="88,134,0,0" 
          Name="listView1" VerticalAlignment="Top" Width="120" 
          ItemsSource="{Binding myList}"/>
    </Grid>
</Window>

C#代码:

namespace TestApp
{
    public partial class MainWindow : Window
    {
        ObservableCollection<string> myList = new ObservableCollection<string>();
        public MainWindow()
        {
            InitializeComponent();
            myList.Add("first string");
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            myList.Add(textBox1.Text);
            textBox1.Text = myList.Count+"st";
        }
    }
}

I'm learning WPF and would like to have a collection similar to a LinkedList, to where I can add and remove strings. And I want to have a ListView that listen to that collection with databinding. How can I do bind a simple list collection to a ListView in XAML?

My idea (not working) is something like this:

<Window x:Class="TestApp.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <LinkedList x:Key="myList"></LinkedList> //Wrong
    <Window.Resources>
    <Grid>
    <ListView Height="100" HorizontalAlignment="Left" Margin="88,134,0,0" 
      Name="listView1" VerticalAlignment="Top" Width="120" 
      ItemsSource="{Binding Source={StaticResource myList}}"/> //Wrong
    </Grid>
</Window>

All my code (updated version, not working):

<Window x:Class="TestApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" 
          Name="textBox1" VerticalAlignment="Top" Width="120" />
        <Button Content="Button" Height="23" HorizontalAlignment="Right" 
          Margin="0,12,290,0" Name="button1" VerticalAlignment="Top" Width="75" 
          Click="button1_Click" />
        <ListView Height="100" HorizontalAlignment="Left" Margin="88,134,0,0" 
          Name="listView1" VerticalAlignment="Top" Width="120" 
          ItemsSource="{Binding myList}"/>
    </Grid>
</Window>

C#-code:

namespace TestApp
{
    public partial class MainWindow : Window
    {
        ObservableCollection<string> myList = new ObservableCollection<string>();
        public MainWindow()
        {
            InitializeComponent();
            myList.Add("first string");
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            myList.Add(textBox1.Text);
            textBox1.Text = myList.Count+"st";
        }
    }
}

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

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

发布评论

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

评论(2

心的位置 2024-09-12 05:47:56

选择作为答案的方法效果很好...但我不太喜欢在 XAML 中设置其他所有内容时必须以编程方式指定 DataContext,我不觉得它是“正确的”(也许这只是我)。因此,对于下一个人,或者任何像我一样思考并从搜索引擎中找到此内容的人(就像我一样),以下是在 XAML 中执行此操作的方法:

C#

public sealed partial class MainPage : Page
{
    public ObservableCollection<string> Messages { get; set; }

    public MainPage()
    {
        this.Messages = new ObservableCollection<string>();
        this.InitializeComponent();
    }
}

XAML

<Window
    ....
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    ...>

    <ListView ItemsSource="{Binding Messages}" ... />

</Window>

说实话,我认为 {BindingrelativeSource={RelativeSource Self}} 应该是任何顶级元素的默认值(PageWindow等等...) DataConext 因为这只是很多人期望它工作的方式,我知道这就是我假设的工作方式。老实说,我觉得 {BindingrelativeSource={RelativeSource Self}} 有点冗长,对于较短的语法来说几乎很长。

The approach selected as an answer works fine... but I don't specifically like having to specify the DataContext programmatically while setting everything else in XAML, I don't feel like it's "proper" (maybe this is just me). So for the next person, or anyone else who thinks like me and finds this off a search engine (like i did), here is the way to do it in XAML:

C#

public sealed partial class MainPage : Page
{
    public ObservableCollection<string> Messages { get; set; }

    public MainPage()
    {
        this.Messages = new ObservableCollection<string>();
        this.InitializeComponent();
    }
}

XAML

<Window
    ....
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    ...>

    <ListView ItemsSource="{Binding Messages}" ... />

</Window>

To be honest I think {Binding RelativeSource={RelativeSource Self}} should be the default value any top level element's (Page, Window, etc...) DataConext because it is simply how a lot of people expect it to work, I know it's how I assume it would work. Honestly, I feel like {Binding RelativeSource={RelativeSource Self}} is a bit verbose and almost long for a shorter syntax.

少女情怀诗 2024-09-12 05:47:56

您只能对公共属性进行数据绑定,并且需要设置 DataContext。

public partial class MainWindow : Window
{
    public ObservableCollection<string> myList { get; private set; }

    public MainWindow()
    {
        InitializeComponent();

        myList = new ObservableCollection<string>();
        myList.Add("first string");
        DataContext = this;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        myList.Add(textBox1.Text);
        textBox1.Text = myList.Count + "st";
    }
}

You can only databind to public properties and you need to set the DataContext.

public partial class MainWindow : Window
{
    public ObservableCollection<string> myList { get; private set; }

    public MainWindow()
    {
        InitializeComponent();

        myList = new ObservableCollection<string>();
        myList.Add("first string");
        DataContext = this;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        myList.Add(textBox1.Text);
        textBox1.Text = myList.Count + "st";
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文