c# - ItemsControl 显示类名

标签 c# uwp uwp-xaml

此应用显示集合的类名而不是所需的文本框。我已经阅读了其他问题,但无法弄清楚我遗漏了什么。我有一个数据上下文,我作为项目源绑定(bind)到集合,并且我添加了一个项目。我想要的只是将我的 View 模型“DrawBoxViewModel”中的集合“Boxes”绑定(bind)到项目源,并将单个项目显示为文本框。感谢所有帮助。

首先是我的 XAML:

<Page
x:Class="BoxMaker2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:BoxMaker2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:BoxMaker2.ViewModels"
mc:Ignorable="d">

<Page.Resources>
    <vm:DrawBoxViewModel x:Key="DrawBoxViewModel"/>
</Page.Resources>
<Canvas DataContext="{Binding Source={StaticResource DrawBoxViewModel}}">
    <ItemsControl ItemsSource="{Binding Boxes}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas Width="350" Height="600" Background="AliceBlue"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.Resources>
            <DataTemplate x:DataType="vm:Box" x:Key="test">
                <VariableSizedWrapGrid>
                    <TextBox Background="White" 
                             Text="{x:Bind Data}"
                             Width="100"
                             Height="100"/>
                    <VariableSizedWrapGrid.RenderTransform>
                        <TranslateTransform X="{Binding LeftCanvas}" Y="{Binding TopCanvas}"/>
                    </VariableSizedWrapGrid.RenderTransform>
                </VariableSizedWrapGrid>
            </DataTemplate>
        </ItemsControl.Resources>
    </ItemsControl>
</Canvas>

现在我的 View 模型:

namespace BoxMaker2.ViewModels
{
public class DrawBoxViewModel
{
    #region fields

    private ObservableCollection<Box> _boxes;

    #endregion

    #region properties

    public ObservableCollection<Box> Boxes { get { return this._boxes; } }

    #endregion

    #region constructors

    public DrawBoxViewModel()
    {
        this._boxes = new ObservableCollection<Box>();
        _boxes.Add(new Box() { Data = "hello!", LeftCanvas = 200, TopCanvas = 200 });
    }

    #endregion

}

public class Box : INotifyPropertyChanged
{
    private int _generation;
    public int Generation
    {
        get { return _generation; }
        set { _generation = value; OnPropertyChanged("Generation"); }
    }

    private int _childNo;
    public int ChildNo
    {
        get { return _childNo; }
        set { _childNo = value; OnPropertyChanged("ChildNo"); }
    }

    private Box _parentBox;
    public Box ParentBox
    {
        get { return _parentBox; }
        set { _parentBox = value; OnPropertyChanged("ParentBox"); }
    }

    private List<Box> _childrenBox;
    public List<Box> ChildrenBox
    {
        get { return _childrenBox; }
        set { _childrenBox = value; OnPropertyChanged("ChildrenBox"); }
    }

    private string _data;
    public string Data
    {
        get { return _data; }
        set
        {
            _data = value;
            OnPropertyChanged("Data");
        }
    }

    private double _topCanvas;
    public double TopCanvas
    {
        get { return _topCanvas; }
        set
        {
            _topCanvas = value;
            OnPropertyChanged("TopCanvas");
        }
    }

    private double _leftCanvas;
    public double LeftCanvas
    {
        get { return _leftCanvas; }
        set
        {
            _leftCanvas = value;
            OnPropertyChanged("LeftCanvas");
        }
    }

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

最佳答案

我不太确定您要实现的目标,但我在您的代码中发现了一些问题。

  1. 您应该将您的 VM 分配给 DataContextPage 直接。

    <Page.DataContext>
        <vm:DrawBoxViewModel />
    </Page.DataContext>
    

    这样做之后,您现在可以删除 DataContext="{Binding Source={StaticResource DrawBoxViewModel}}"来自你的 Canvas .

  2. 替换<ItemsControl.Resource><ItemsControl.ItemTemplate>并删除 x:Key="test" ,假设你想显示多个 TextBox用户界面上的es。 DataTemplateResource 内你 defined 在您通过它的键引用它之前不会做任何事情。我不 认为你真的想要这里。
  3. 你应该使用 x:Bind为你的 X & Y绑定(bind)

    <TranslateTransform X="{x:Bind LeftCanvas}"
                        Y="{x:Bind TopCanvas}" />
    
  4. 你的 Boxes集合可以简化如下

    #region properties
    
    public ObservableCollection<Box> Boxes { get; } = new ObservableCollection<Box>();
    
    #endregion
    
    #region constructors
    
    public DrawBoxViewModel()
    {
        Boxes.Add(new Box() { Data = "hello!", LeftCanvas = 0, TopCanvas = 200 });
    }
    
    #endregion
    

希望这对您有所帮助!

关于c# - ItemsControl 显示类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45889448/

相关文章:

c# - "Grouping"字典值

c# - EFCore 2.2.6 与 5.0.8 无跟踪

c# - UWP 有没有在 Canvas 中实现调整大小和移动文本框的控件?

uwp - 当我尝试转换我的应用程序时,DesktopAppConverter (DAC) 表示扩展的图像无效

c# - 无法将 <element> 添加到 ObservableCollection。找不到元素错误

c# - 如何显示 Assets 文件夹中的 XAML 格式图像?

c# - 键为 List 的字典如何运行?

android - 你如何管理你的 Assets ?

uwp-xaml - C++ WinRT UWP XAML 数据绑定(bind) ItemsSource 和 SelectedIndex 到 IObservableVector<hstring> 不起作用

c# - 在 WPF 中验证多个链接的数据绑定(bind) TextBox 值