gpt4 book ai didi

c# - ItemsControl 显示类名

转载 作者:太空宇宙 更新时间:2023-11-03 22:54:49 24 4
gpt4 key购买 nike

此应用显示集合的类名而不是所需的文本框。我已经阅读了其他问题,但无法弄清楚我遗漏了什么。我有一个数据上下文,我作为项目源绑定(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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com