Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding to a TextBox within a ListView?
    primarykey
    data
    text
    <p>I have been searching for an answer for this for almost a week now, and I can't seem to find a way to do this. I would think it is a relatively simple thing...</p> <p>I have a ListView, and one GridViewColumn contains TextBox items. I want to put a Label right underneath the ListView, and fill it with the sum of the items in all the TextBoxes. Whenever someone changes a value in any of the TextBoxes, the sum should change, too.</p> <p>I know I need to use a Converter to get the sum... but I don't know how to get the binding right...</p> <p>Any help would be greatly appreciated!</p> <p><strong>EDIT: I ended up solving this in a slightly different way. I will post my solution below.</strong></p> <pre><code>&lt;Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:converters="clr-namespace:WpfApplication2" Title="MainWindow" Height="350" Width="525"&gt; &lt;Window.Resources&gt; &lt;XmlDataProvider x:Key="myParties" XPath="Parties" Source="XMLFile1.xml" /&gt; &lt;CollectionViewSource x:Key="myCollectionViewSource" Source="{StaticResource myParties}" /&gt; &lt;converters:SumConverter x:Key="mySumConverter" /&gt; &lt;/Window.Resources&gt; &lt;StackPanel&gt; &lt;Button x:Name="Breakpoint" Click="bpClick" Content="Breakpoint"/&gt; &lt;ListView x:Name="myListView" HorizontalAlignment="Stretch" ItemsSource="{Binding Source={StaticResource myCollectionViewSource},XPath='Party',Mode=TwoWay}"&gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridViewColumn Width="100" DisplayMemberBinding="{Binding XPath='@Contact'}" Header="Contact"/&gt; &lt;GridViewColumn DisplayMemberBinding="{Binding XPath='@Qty'}" Header="Q"/&gt; &lt;GridViewColumn DisplayMemberBinding="{Binding XPath='@Amount'}" Header="Amt"/&gt; &lt;GridViewColumn x:Name="tbTot" Header="Tot"&gt; &lt;GridViewColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;DockPanel&gt; &lt;TextBox Width="100" Text="{Binding XPath='@Tot'}" /&gt; &lt;/DockPanel&gt; &lt;/DataTemplate&gt; &lt;/GridViewColumn.CellTemplate&gt; &lt;/GridViewColumn&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; &lt;Label Height="22"&gt; &lt;Label.Content&gt; &lt;MultiBinding Converter="{StaticResource mySumConverter}"&gt; &lt;Binding ElementName="myListView" Path="Items"/&gt; &lt;Binding ElementName="myListView" Path="Items.Count"/&gt; &lt;/MultiBinding&gt; &lt;/Label.Content&gt; &lt;/Label&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p>XML file:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;Parties&gt; &lt;Party Contact="Jim Shmekel" Qty="1" Amount="55.00" Tot="55.00"/&gt; &lt;Party Contact="Shmi Skywalker" Qty="1" Amount="20.00" Tot="20.00"/&gt; &lt;Party Contact="Jon Ronson" Qty="1" Amount="23.00" Tot="23.00"/&gt; &lt;/Parties&gt; </code></pre> <p>SumConverter:</p> <pre><code>Imports System.Collections.ObjectModel Imports System.Reflection &lt;ValueConversion(GetType(Object()), GetType(String))&gt; Public Class SumConverter : Implements System.Windows.Data.IMultiValueConverter Public Function Convert(ByVal values() As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IMultiValueConverter.Convert Static lvItems As IList Static lvItem As Xml.XmlElement Dim nVal As Double Convert = 0 lvItems = values(0) If lvItems Is Nothing Then Exit Function For Each lvItem In lvItems 'Debug.Print(lvItem.GetAttribute("Tot")) If Double.TryParse(lvItem.GetAttribute("Tot"), nVal) Then Convert = Convert + nVal End If Next End Function Public Function ConvertBack(ByVal value As Object, ByVal targetTypes() As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object() Implements System.Windows.Data.IMultiValueConverter.ConvertBack ConvertBack = Nothing End Function End Class </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload