【问题标题】:How to change a custom type to System type (string) in WPF XAML? [duplicate]如何在 WPF XAML 中将自定义类型更改为系统类型(字符串)? [复制]
【发布时间】:2013-01-10 23:07:49
【问题描述】:

可能重复:
How to use nested class in WPF XAML?

我正在重构示例中的代码:

after excluding Skills class,有相应的变化
MainWindow.xaml

<local:Team>
  <local:Employee Name="Larry" Age="21">
    <local:Employee.Skills>
      <!--  local:Skills -->
       <local:Skills>

我在 MainWindow1.xaml.cs

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
namespace WpfApplication
{
  public class Skill
  {//I'd like to remove class Skill having moved property string Description into class Employee 
    public string Description { get; set; }
  }

   public class Employee 
   {
    public string Name { get  ; set; }
    public int Age  { get; set; }
//I'd like to change in the next line Skill --> String               
    public List<Skill> Skills { get; set; }
    //public List<String> Skills { get; set; }

     public Employee()
     {
//I'd like to change in the next line Skill --> String       
       Skills=new List<Skill>();
       //Skills=new List<string>();
     }
  }

  public class Team : ObservableCollection<Employee> { }

  public class Company
  {
    public string CompanyName { get  ; set; }
    public Team Members { get  ; set; }
  }

  public class Companies : ObservableCollection<Company> { }

  public partial class Window1 : Window
    {
      public Window1()
    {
        InitializeComponent();
    }
  }
}

如果要更改,我应该如何更改 Window1.XAML:

  • List&lt;Skill&gt;List&lt;String&gt;

在 Window1.xaml.cs 中?

基于相同代码的相关问题:

更新:
只是要注意我尝试过的错误:

  • “A”而不是 A(不带引号);
  • 在 XAML 中应该是 String(但不是 string)(而在 C# 中是 stringString
  • 试图将 A 作为属性,根据现有代码,在一个标签 &lt;sys:String /&gt; 中 而不是:&lt;sys:String&gt;A&lt;/sys:String&gt;

【问题讨论】:

    标签: c# wpf xaml data-binding


    【解决方案1】:

    您的 Employee 类采用了您希望它成为的新方法 -

    public class Employee
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public List<String> Skills { get; set; }
    
        public Employee()
        {
            Skills=new List<string>();
        }
    }
    

    您可以像这样在 xaml 文件中创建实例 -

            <local:Team x:Key="teams">
                <local:Employee Name="Larry" Age="21">
                    <local:Employee.Skills>
                        <sys:String>A</sys:String>
                        <sys:String>B</sys:String>
                        <sys:String>C</sys:String>
                    </local:Employee.Skills>
                </local:Employee>
            </local:Team>
    

    你需要在你的 xaml 中定义 sys 命名空间来在 xaml 中声明一个字符串的实例 -

    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    

    我用ListBox 测试了上面的代码及其工作 -

    <ListBox ItemsSource="{Binding Skills, Source={StaticResource teams}}"/>
    

    【讨论】:

    • 哇,非常感谢。问题是我以前从未手动编写过 XML - 只有编辑器或 Intellisense 协助。而且 WPF 编辑器在这方面不是很友好
    • 能否附上所有 XAML 脚本?虽然很清楚如何在 XAML 中使用 String 类型,但我急于接受您的答案,但没有成功在主详细信息的上下文中运行它
    猜你喜欢
    • 1970-01-01
    • 2020-03-17
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    • 1970-01-01
    • 2020-09-17
    相关资源
    最近更新 更多