遇见一个问题

如果用一个结构体struct。再用一个ListView,然后使用绑定。

<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"> <Window.Resources>
<DataTemplate x:Key="ItemProjectTemplate">
<StackPanel>
<Image Source="img/1.jpg" Width="50" Height="50" />
<TextBlock FontSize="22" Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</Window.Resources> <ListView HorizontalAlignment="Left" Height="400"
VerticalAlignment="Top" Width="423"
x:Name="Projects"
ItemTemplate="{StaticResource ItemProjectTemplate}"/>
</Window>
using System.Windows;

namespace WpfApp1
{
public struct Project
{
public string Name;
}
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Projects.ItemsSource = InitProject();
} private Project[] InitProject()
{
Project[] x = new Project[]; x[].Name = "";
x[].Name = "";
x[].Name = ""; return x;
}
}
}

看一看效果

会发现图片加载出来了,可是绑定的文本数据不见了~

https://stackoverflow.com/questions/13101449/xaml-binding-code-not-working有这样的答复

DataBinding works for Properties and not on Fields. Replace Struct with class and make your fields as properties-

因为结构体里面是字段不是属性,所以就没有作用了。需要改写成

public struct Project
{
public string Name { get; set; }
}

这样就显示正常了

或者有更加高端的做法,用一个转化器

using System;
using System.Globalization;
using System.Windows.Data; namespace WpfApp1
{
public class Name : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Project project)
{
var name = project.Name;
return name;
} return null;
} public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
xmlns:convert="clr-namespace:WpfApp1"> <Window.Resources> <convert:Name x:Key="Name"/> <DataTemplate x:Key="ItemProjectTemplate">
<StackPanel>
<Image Source="img/1.jpg" Width="50" Height="50" />
<TextBlock FontSize="22" Text="{Binding Converter={StaticResource Name}}" />
</StackPanel>
</DataTemplate>
</Window.Resources> <ListView HorizontalAlignment="Left" Height="400"
VerticalAlignment="Top" Width="423"
x:Name="Projects"
ItemTemplate="{StaticResource ItemProjectTemplate}"/>
</Window>

XAML 绑定和结构体不得不说的问题的更多相关文章

  1. WPF中ItemsControl绑定到Google ProtocolBuffer的结构体时的性能问题

    背景: 最近遇到一个DataGrid的性能问题:里面大概有4000个数据, 绑定的ItemSource的类也只有一层数据,即简单的List(里面每个是Protocol Buffer自动产生的一个类,1 ...

  2. gin中绑定表单数据至自定义结构体

    package main import "github.com/gin-gonic/gin" type StructA struct { FieldA string `form:& ...

  3. Go结构体实现类似成员函数机制

    Go语言结构体成员能否是函数,从而实现类似类的成员函数的机制呢?答案是肯定的. package main import "fmt" type stru struct { testf ...

  4. go语言结构体

    定义: 是一种聚合的数据类型,是由零个或多个任意类型的值聚合成的实体. 成员: 每个值称为结构体的成员. 示例: 用结构体的经典案例处理公司的员工信息,每个员工信息包含一个唯一的员工编号.员工的名字. ...

  5. 在WPF中使用变通方法实现枚举类型的XAML绑定

    问题缘起 WPF的分层结构为编程带来了极大便利,XAML绑定是其最主要的特征.在使用绑定的过程中,大家都普遍的发现枚举成员的绑定是个问题.一般来说,枚举绑定多出现于与ComboBox配合的情况,此时我 ...

  6. swift 的枚举、结构体、类

    一.Swift的枚举 枚举是一系相关联的值定义的一个公共的组类型,同时能够让你在编程的时候在类型安全的情况下去使用这些值.Swift中的枚举比OC中的枚举强大得多, 因为Swift中的枚举是一等类型, ...

  7. 5.Swift枚举|结构体|类|属性|方法|下标脚本|继承

    1. 枚举: ->在Swift中依然适用整数来标示枚举值,需搭配case关键字 enum  Celebrity{  case DongXie,XiDu,Nandi,BeiGai }  // 从左 ...

  8. C语言中的结构体,结构体数组

    C语言中的结构体是一个小难点,下面我们详细来讲一下:至于什么是结构体,结构体为什么会产生,我就不说了,原因很简单,但是要注意到是结构体也是连续存储的,但要注意的是结构体里面类型各异,所以必然会产生内存 ...

  9. C# Socket 入门4 UPD 发送结构体(转)

    今天我们来学 socket  发送结构体 1. 先看要发送的结构体 using System; using System.Collections.Generic; using System.Text; ...

随机推荐

  1. 696. Count Binary Substrings统计配对的01个数

    [抄题]: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...

  2. opennebule 创建cdrom数据发送

    {","csrftoken":"b9b5026f1a92180b789971ed8e21d28b"}

  3. sfidsk创建可启动分区问题

    前言 由于工作上需要经常要为嵌入式设备制作启动SD卡,因此本人使用sfdisk编写了自动分区.格式化和安装文件的脚本.(不选择fdisk是因为它是为用户交互设计的,在脚本上使用不够方便) 实际使用过程 ...

  4. python 添加日期

    import pandas as pd applydata['apply_time'] = pd.to_datetime(applydata.apply_time)# applydata.apply_ ...

  5. 权限管理RBAC

    四张表: 1.module:id/name //模块 2.action:id /module_id/name //权限 3.user:id/name //用户表 4.group:id/user_id ...

  6. 利用AdaBoost方法构建多个弱分类器进行分类

    1.AdaBoost 思想 补充:这里的若分类器之间有比较强的依赖关系;对于若依赖关系的分类器一般使用Bagging的方法 弱分类器是指分类效果要比随机猜测效果略好的分类器,我们可以通过构建多个弱分类 ...

  7. 验证码测试-demo

    <!DOCTYPE html><html><head><meta charset="UTF-8"><title>Inse ...

  8. CentOS7下源码包方式安装rabbitmq

    1.先安装erlang http://www.cnblogs.com/justphp/p/6093880.html 2.下载rabbitmq rpm包: wget http://www.rabbitm ...

  9. [GO]将随机生成的四位数字拆分后放到一个切片里

    package main import ( "math/rand" "time" "fmt" ) func InitData(p *int) ...

  10. PC/APP/H5三端测试的相同与不同

    随着手机应用的不断状态,同一款产品的移动端应用市场占相较PC端也越来越大,那么app与PC端针对这些产品的测试有什么相同与不同之处呢?总结如下: 首先谈一谈相同之处: 一,针对同一个系统功能的测试,三 ...