gpt4 book ai didi

c# - 类的名称属性,用于查看示例数据

转载 作者:行者123 更新时间:2023-11-30 17:01:16 26 4
gpt4 key购买 nike

我创建了一个具有这个属性的类

public class PromoViewModel
{
string name;
// setter getter here...
string description;
// setter getter here...
string img;
// setter getter here...
}

问题是,当我制作这样的样本数据时

<vm:MainViewModel
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:Dmall.ViewModels"
SampleProperty="Sample Text Property Value">

<vm:MainViewModel.Promos>
<vm:PromoViewModel Name="design one" Description="Maecenas praesent accumsan bibendum" Img="Maecenas praesent accumsan bibendum dictumst eleifend facilisi faucibus habitant inceptos interdum lobortis nascetur"/>
<vm:PromoViewModel Name="design two" Description="Dictumst eleifend facilisi faucibus" Img="Pharetra placerat pulvinar sagittis senectus sociosqu suscipit torquent ultrices vehicula volutpat maecenas praesent"/>
<vm:PromoViewModel Name="design three" Description="Habitant inceptos interdum lobortis" Img="Accumsan bibendum dictumst eleifend facilisi faucibus habitant inceptos interdum lobortis nascetur pharetra placerat"/>
</vm:MainViewModel.Promos>

</vm:MainViewModel>

错误来了,因为“Name”是XAML的特殊关键字,x:Name都不行。
XAML Design Viewer 上的这个结果错误,因为我尝试这样做

{Binding Name}
// this would never exist because in the sample data, name is special attributes keyword not a property of PromoViewModel class and return NullReferenceException

我可以轻松地将 PromoViewModel 类中的 name 属性更改为“Title”并且它有效。
但效率不高。在类上使用属性“名称”并将其用于示例数据的正确方法是什么?

最佳答案

尝试使用 {Binding Path=Name} 以便您明确设置 Path 属性。 Path 只是绑定(bind)的默认属性,因此在这种情况下,您要确保绑定(bind)知道您在谈论 Path 而不是 Name

以下对我有用:

XAML:

<TextBlock Text="{Binding Path=Name}" />

View 模型:

public class ViewModel
{
public string Name { get; set; }
}

代码隐藏

ViewModel vm = new ViewModel();
vm.Name = "Test";

this.DataContext = vm;

关于c# - 类的名称属性,用于查看示例数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21196101/

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