0

Having successfully bound the controls to an object, how is it possible to collect all those controls that have a value?

This is the 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="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>
Ivan-Mark Debono
  • 15,500
  • 29
  • 132
  • 263
  • What are you trying to achieve? – Dennis Nov 20 '13 at 10:09
  • I would like to place the controls that have values in Dictionary where key is the property name and value is the control's value. – Ivan-Mark Debono Nov 20 '13 at 10:10
  • @Ivan-MarkDebono: I mean, what for? Why are you trying to collect some controls, instead of looking up to the bound data properties? Also, please, define "have values". – Dennis Nov 20 '13 at 10:11

1 Answers1

0

WPF is data-centric... that means that we work with data rather than UI controls. Once you have data bound an object to one or more UI controls, then you can just forget about the UI controls and work with the data from then on.

So you want to know the values from the UI? Just look at the properties of the object that you data bound to the UI controls rather than the values of the UI controls.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • Is there a way to iterate through the data instead of checking each individual property? – Ivan-Mark Debono Nov 20 '13 at 10:40
  • That would depend on what object you are using... if you were using a `Dictionary`, then yes, you could iterate through each pair of key value pairs easily. You could also do it on pretty much any class using `Reflection` to get a collection of `PropertyInfo`s and read their values, although this would not be quite as simple. – Sheridan Nov 20 '13 at 10:44
  • Objects are DTO classes. – Ivan-Mark Debono Nov 20 '13 at 10:51
  • My initial feeling is that you shouldn't be displaying DTO objects in the UI. However, what you do in your code is up to you. If the object is a .NET object, then you can use `Reflection` to iterate its properties. You can find an example on how to do this in the [How to loop through all the properties of a class?](http://stackoverflow.com/questions/531384/how-to-loop-through-all-the-properties-of-a-class) post. – Sheridan Nov 20 '13 at 10:58
  • What should I use instead of DTO? I'm already mapping my model to dto. – Ivan-Mark Debono Nov 20 '13 at 10:59
  • It is customary to copy data from data access classes into business model classes before displaying them in the UI. This mainly preserves the 'separation of concerns' between the various layers or tiers of an application and only needs to be considered on large applications really. – Sheridan Nov 20 '13 at 11:01