簡體   English   中英

使用WrapPanel的ItemsControl,不顯示任何內容

[英]ItemsControl using WrapPanel, not displaying anything

目標:我正在嘗試創建一個包裝面板,其中包含綁定到可觀察集合的子項。

當前的預期行為:我希望看到3個嵌套的包裝面板,它們具有一個橢圓,一個文本塊,一個標簽和一個復選框。

問題:我的包裝面板和內容在運行時不顯示。 (注意:items控件外部的“測試”和“測試2”標簽確實按預期顯示。)

我已經讀過這篇文章 ,似乎並不能解決我的問題。

背后的代碼

using MVVM_SandBox.Models;
using MVVM_SandBox.ViewModels;

namespace MVVM_SandBox
{
    public partial class MainWindow
    {     
        public MainViewModel VMMain = new MainViewModel();


        public MainWindow()
        {
            VMMain.SomeItemModelBlahs = new System.Collections.ObjectModel.ObservableCollection<ItemModelBlah>() { new ItemModelBlah() { Label = "blah0" }, new ItemModelBlah() { Label = "blah1", CoolStuff = true }, new ItemModelBlah() { Label = "blah2" } };
            InitializeComponent();


        }
    }
}

XAML

<Window x:Name="winMain" x:Class="MVVM_SandBox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

                     xmlns:viewmodels="clr-namespace:MVVM_SandBox.ViewModels"
                     Title="MVVM SandBox" Height="600" Width="800" AllowDrop="True">
    <Window.DataContext>
        <viewmodels:MainViewModel></viewmodels:MainViewModel>
    </Window.DataContext>
    <StackPanel>
        <Label Width="Auto" Height="Auto">Test</Label>
        <ItemsControl ItemsSource="{Binding SomeItemModelBlahs}" Margin="10,10,10,10">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel x:Name="WrapPanelOfModelItems" Margin="10,10,10,10" Width="400" Height="200" IsItemsHost="True" MinWidth="200" MinHeight="200">
                        </WrapPanel>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Width="100" Height="100" Margin="10">
                        <Ellipse Width="10" Height="10" Fill="Aqua"></Ellipse>
                        <TextBlock Margin="10" Text="{Binding Label}"></TextBlock>
                        <Label>kjasdkjalsdjklsad</Label>
                        <CheckBox IsChecked="{Binding CoolStuff}">
                            <Label>Hello</Label> 
                        </CheckBox>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            </ItemsControl>
        <Label Height="Auto" Width="Auto">Test 2</Label>
    </StackPanel>

</Window>

查看模型

using MVVM_SandBox.Models;
using System.Collections.ObjectModel;


namespace MVVM_SandBox.ViewModels
{
    //[ImplementPropertyChanged]
    public class MainViewModel
    {
        public ObservableCollection<ItemModelBlah> SomeItemModelBlahs { get; set; }

        public MainViewModel()
        {

        }
    }
}

模型

namespace MVVM_SandBox.Models
{
    public class ItemModelBlah
    {
        public string Label { get; set; }
        public bool CoolStuff { get; set; }
    }
}

該代碼正在創建MainViewModel兩個實例:一個在后面的代碼中,另一個在XAML中。 實例后面的代碼具有非null的ObservableCollection ,而XAML中的實例設置為DataContext

我建議從XAML中刪除viewmodels:MainViewModel ,並僅在代碼中創建它:

public partial class MainWindow
{     
    public MainViewModel VMMain = new MainViewModel();

    public MainWindow()
    {
        DataContext = VWMain;
        InitializeComponent();
    }
}

另外,從視圖模型本身內部設置ObservableCollection是一個更好的設計:

class MainViewModel
{
    public ObservableCollection<ItemModelBlah> SomeItemModelBlahs { get; private set; }

    public MainViewModel()
    {
        SomeItemModelBlahs = new ObservableCollection<ItemModelBlah>()
        {
            new ItemModelBlah() { Label = "blah0" },
            new ItemModelBlah() { Label = "blah1", CoolStuff = true },
            new ItemModelBlah() { Label = "blah2" }
        };
    }
}

暫無
暫無

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

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