C# WPF和绑定控件
成功地将控件绑定到对象后,如何收集所有具有值的控件 这是XAML:C# WPF和绑定控件,c#,wpf,data-binding,C#,Wpf,Data Binding,成功地将控件绑定到对象后,如何收集所有具有值的控件 这是XAML: <Window 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="h
<Window
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"
xmlns:Model="clr-namespace:MyApp.DTO;assembly=MyApp.DTO" mc:Ignorable="d" x:Class="MyApp.Forms.Reference.CustomerTypeForm"
Title="Customer Type" Height="346" Width="459" WindowStartupLocation="CenterOwner" ShowInTaskbar="False" ScrollViewer.VerticalScrollBarVisibility="Disabled" ResizeMode="NoResize" Loaded="Window_Loaded">
<DockPanel LastChildFill="True">
<Grid>
<Canvas>
<Label Content="Number:" HorizontalAlignment="Left" Margin="87,33,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtNumber" HorizontalAlignment="Left" Height="24" Margin="185,36,0,211" Text="{Binding Number, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="76" KeyDown="txtNumber_KeyDown"/>
<Label Content="Description:" HorizontalAlignment="Left" VerticalAlignment="Top" Canvas.Left="87" Canvas.Top="62"/>
<TextBox x:Name="txtDescription" HorizontalAlignment="Left" Height="24" Margin="185,64,0,183" Text="{Binding Description, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="236"/>
<Label Content="Abbr:" HorizontalAlignment="Left" VerticalAlignment="Top" Canvas.Left="87" Canvas.Top="91"/>
<TextBox x:Name="txtAbbr" HorizontalAlignment="Left" Height="24" Margin="185,93,0,154" Text="{Binding Abbr, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="75"/>
</Canvas>
</Grid>
</DockPanel>
</Window>
WPF以数据为中心。。。这意味着我们使用的是数据,而不是UI控件。一旦您将一个对象的数据绑定到一个或多个UI控件,那么您就可以忘记UI控件,从那时起使用数据
那么您想知道UI中的值吗?只需查看绑定到UI控件的对象的属性,而不是UI控件的值。您想实现什么?我想将具有值的控件放在字典中,其中键是属性名称,值是控件的值。@Ivan MarkDebono:我的意思是,为什么?为什么要收集一些控件,而不是查找绑定的数据属性?另外,请定义“have values”。有没有一种方法可以遍历数据而不是检查每个属性?这取决于您使用的对象。。。如果您使用的是字典
,那么是的,您可以轻松地迭代每对键值对。您也可以在几乎任何类上使用反射
来获取属性信息的集合并读取它们的值,尽管这并不是那么简单。对象是DTO类。我最初的感觉是不应该在UI中显示DTO对象。然而,您在代码中做什么取决于您自己。如果对象是.NET对象,则可以使用反射
迭代其属性。你可以在帖子中找到一个例子,我应该用什么来代替DTO?我已经在将我的模型映射到dto。