Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
When does the bound data get updated?
Message
De
20/02/2013 04:15:19
 
 
À
19/02/2013 12:55:30
Information générale
Forum:
ASP.NET
Catégorie:
Windows Presentation Foundation (WPF)
Divers
Thread ID:
01566380
Message ID:
01566442
Vues:
47
>>>I have a textbox which is bound to a field on an entity. I need to run some code whenever the entity's field changes. I've tried calling my code in the textbox's Text_Changed event, but referring to the entity's field at this point in time does not reflect the value entered in the textbox.
>>>
>>>At what point in time would the entity's field get updated with the value entered in the textbox?
>>>
>>>Or is there another way to trap/raise an event when the entity's filed changes?
>>
>>Usually, the UI field is bound to a property that implements INotifyPropertyChanged. You can process your code changes there when the properties value changes. Or trap the PropertyChanged event in the UI and fire off your code there..
>
>Thanks John, but this is really all Greek to me at the moment.
>
>The form I am dealing with has:
>1. a field to enter the deposit amount
>2. A grid to enter the deposit details
>3. A textbox to show the total of the deposit details
>4. Another textbox to show the difference between the Deposit Amount and the total of the deposit details.
>
>I have this defined:
>
>
        private decimal depositTotal;
>        
>        public decimal DepositTotal
>        {
>            get
>            {
>                return this.depositTotal;
>            }
>
>            set
>            {
>                if (value != this.depositTotal)
>                {
>                    this.depositTotal = value;
>                    this.OnPropertyChanged("DepositTotal");
>                }
>            }
>        }
>
>        private decimal balanceOutstanding;
>
>        public decimal BalanceOutstanding
>        {
>            get
>            {
>                return this.balanceOutstanding;
>            }
>
>            set
>            {
>                if (value != this.balanceOutstanding)
>                {
>                    this.balanceOutstanding = value;
>                    this.OnPropertyChanged("BalanceOutstanding");
>                }
>            }
>        }
>
>I have the txtDepositTotal defined like this:
>
>
<TextBox Name="txtDepositTotal" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DepositTotal}"/>
>
>and the txtBalanceOutstanding like this:
>
>
<TextBox  Name="txtBalanceOutstanding"Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=BalanceOutstanding}" />
>
>and the DepositAmount:
>
>
<TextBox  Text="{Binding dep_amount, Mode=Default}" 
>                                  Name="txtDep_Amount" 
>                                  VerticalAlignment="Center" />
>
>Can you give me an example showing what I should do?

You could do this in XAML using a value converter:
  public class BalanceConverter : IMultiValueConverter
  {
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      var valuesToConvert = values.Cast<string>().ToArray();

      double val1;
      double val2;
      if (!Double.TryParse(valuesToConvert[0], out val1))
        val1 = 0;
      if (!Double.TryParse(valuesToConvert[1], out val2))
        val2 = 0;

      return String.Format("Balance: {0}", (val1 - val2).ToString("C"));
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
      throw new NotImplementedException();
    }
  }
<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:local="clr-namespace:WpfApplication3">
  <Window.Resources>
    <local:BalanceConverter x:Key="balance"/>
  </Window.Resources>
  <StackPanel>
    <TextBox Name="txtDepositTotal" />
    <TextBox   Name="txtDep_Amount" VerticalAlignment="Center" />
    <TextBox  Name="txtBalanceOutstanding" >
      <MultiBinding Converter="{StaticResource balance}">
        <Binding ElementName="txtDepositTotal" Path="Text"></Binding>
        <Binding ElementName="txtDep_Amount" Path="Text"></Binding>
      </MultiBinding>
    </TextBox>
  </StackPanel>
</Window>
(Converter logic could be tidier :-} )
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform