使用 MVVM 以编程方式使用用户控件的可观察集合填充堆栈面板

发布于 2024-11-19 18:10:12 字数 114 浏览 3 评论 0原文

我有一个框架元素类型的可观察集合,我想在堆栈面板或类似的东西中显示它。 observablecollection 中的每个项目都是我创建的用户控件。我对 WPF 还很陌生,我不知道如何做到这一点。一个例子将不胜感激

I have an observablecollection of type frameworkelement that I would like to display in a stackpanel or something similar. Every item in the observablecollection is a usercontrol that I have created. I'm pretty new to WPF and I don't have any idea how to do this. An example would be much appreciated

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

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

发布评论

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

评论(2

诺曦 2024-11-26 18:10:12

我在这里借用了 rhe1980 的答案,但重点是代码隐藏中的代码实际上位于视图模型中。

视图:

<Window x:Class="Sandbox.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"
Name="mainWindow">
<Grid>
<StackPanel>
    <ItemsControl ItemsSource="{Binding Path=MyCollection}"/>                           
</StackPanel>         
</Grid>

代码隐藏:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new MyViewModel();
    }
}

ViewModel:

    public class MyViewModel: INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        if (!string.IsNullOrEmpty(propertyName))
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        this.OnObjectChanged();
    } 

   private ObservableCollection<FrameworkElement> _myCollection;
   public ObservableCollection<FrameworkElement> MyCollection
    {
        get
        {
            return _myCollection;
        }
        set
        {
            _myCollection = value;
            OnPropertyChanged("MyCollection");
        }
    }     
}

I'm borrowing rhe1980's answer a bit here, but the point is that the code in the codebehind will actually be in a viewmodel.

View:

<Window x:Class="Sandbox.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"
Name="mainWindow">
<Grid>
<StackPanel>
    <ItemsControl ItemsSource="{Binding Path=MyCollection}"/>                           
</StackPanel>         
</Grid>

CodeBehind:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new MyViewModel();
    }
}

ViewModel:

    public class MyViewModel: INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        if (!string.IsNullOrEmpty(propertyName))
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        this.OnObjectChanged();
    } 

   private ObservableCollection<FrameworkElement> _myCollection;
   public ObservableCollection<FrameworkElement> MyCollection
    {
        get
        {
            return _myCollection;
        }
        set
        {
            _myCollection = value;
            OnPropertyChanged("MyCollection");
        }
    }     
}
偏爱自由 2024-11-26 18:10:12

使用 ItemsControl 绑定 StackPanel 中的 ObservableCollection:

View(xaml):

<Window x:Class="Sandbox.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"
    Name="mainWindow">
<Grid>
    <StackPanel>
        <ItemsControl ItemsSource="{Binding ElementName=mainWindow,Path=ObservableCollection}"/>                           
    </StackPanel>         
</Grid>

隐藏代码(xaml.cs):

public partial class MainWindow : Window
{
    public ObservableCollection<FrameworkElement> ObservableCollection { get; set; }

    public MainWindow()
    {
        InitializeObservableCollection();
        InitializeComponent();
    }

    private void InitializeObservableCollection()
    {
        ObservableCollection = new ObservableCollection<FrameworkElement>();
        for (var ii = 0; ii < 10; ii++)
        {
            ObservableCollection.Add(new Button {Content = ii.ToString()});
        }
    }
}

Use a ItemsControl for bind the ObservableCollection in the StackPanel:

View(xaml):

<Window x:Class="Sandbox.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"
    Name="mainWindow">
<Grid>
    <StackPanel>
        <ItemsControl ItemsSource="{Binding ElementName=mainWindow,Path=ObservableCollection}"/>                           
    </StackPanel>         
</Grid>

Codebehind(xaml.cs):

public partial class MainWindow : Window
{
    public ObservableCollection<FrameworkElement> ObservableCollection { get; set; }

    public MainWindow()
    {
        InitializeObservableCollection();
        InitializeComponent();
    }

    private void InitializeObservableCollection()
    {
        ObservableCollection = new ObservableCollection<FrameworkElement>();
        for (var ii = 0; ii < 10; ii++)
        {
            ObservableCollection.Add(new Button {Content = ii.ToString()});
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文