Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 绑定到ListView中的文本框?_.net_Wpf_Vb.net_Xaml_Ivalueconverter - Fatal编程技术网

.net 绑定到ListView中的文本框?

.net 绑定到ListView中的文本框?,.net,wpf,vb.net,xaml,ivalueconverter,.net,Wpf,Vb.net,Xaml,Ivalueconverter,我已经为这个问题寻找了将近一个星期的答案,但我似乎找不到一个方法来解决这个问题。我认为这是一件相对简单的事情 我有一个ListView,一个GridViewColumn包含TextBox项。我想在ListView的正下方放置一个标签,并用所有文本框中的项目总和填充它。每当有人更改任何文本框中的值时,总和也应该更改 我知道我需要用一个转换器来求和。。。但我不知道怎样才能把装订好 任何帮助都将不胜感激 编辑:我最终以一种稍微不同的方式解决了这个问题。我将在下面发布我的解决方案。 <Window

我已经为这个问题寻找了将近一个星期的答案,但我似乎找不到一个方法来解决这个问题。我认为这是一件相对简单的事情

我有一个ListView,一个GridViewColumn包含TextBox项。我想在ListView的正下方放置一个标签,并用所有文本框中的项目总和填充它。每当有人更改任何文本框中的值时,总和也应该更改

我知道我需要用一个转换器来求和。。。但我不知道怎样才能把装订好

任何帮助都将不胜感激

编辑:我最终以一种稍微不同的方式解决了这个问题。我将在下面发布我的解决方案。

<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">

    <Window.Resources>
        <XmlDataProvider x:Key="myParties" XPath="Parties" Source="XMLFile1.xml" />
        <CollectionViewSource x:Key="myCollectionViewSource" Source="{StaticResource myParties}" />
        <converters:SumConverter x:Key="mySumConverter" />
    </Window.Resources>

    <StackPanel>
        <Button x:Name="Breakpoint" Click="bpClick" Content="Breakpoint"/>
        <ListView x:Name="myListView" 
                  HorizontalAlignment="Stretch" 
                  ItemsSource="{Binding Source={StaticResource myCollectionViewSource},XPath='Party',Mode=TwoWay}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="100" DisplayMemberBinding="{Binding XPath='@Contact'}" Header="Contact"/>
                    <GridViewColumn DisplayMemberBinding="{Binding XPath='@Qty'}" Header="Q"/>
                    <GridViewColumn DisplayMemberBinding="{Binding XPath='@Amount'}" Header="Amt"/>
                    <GridViewColumn x:Name="tbTot" Header="Tot">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <DockPanel>
                                    <TextBox Width="100" Text="{Binding XPath='@Tot'}" />
                                </DockPanel>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>    
        </ListView>

        <Label Height="22">
            <Label.Content>
                <MultiBinding Converter="{StaticResource mySumConverter}">
                    <Binding ElementName="myListView" Path="Items"/>
                    <Binding ElementName="myListView" Path="Items.Count"/>
                </MultiBinding>
            </Label.Content>
        </Label>
    </StackPanel>
</Window>

XML文件:

<?xml version="1.0" encoding="utf-8" ?>
<Parties>
  <Party Contact="Jim Shmekel"
         Qty="1"
         Amount="55.00" 
         Tot="55.00"/>
  <Party Contact="Shmi Skywalker"
         Qty="1"
         Amount="20.00" 
         Tot="20.00"/>
  <Party Contact="Jon Ronson"
         Qty="1"
         Amount="23.00" 
         Tot="23.00"/>
</Parties>

SumConverter:

Imports System.Collections.ObjectModel
Imports System.Reflection

<ValueConversion(GetType(Object()), GetType(String))>
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
导入System.Collections.ObjectModel
输入系统。反射
公共类SumConverter:实现System.Windows.Data.IMultiValueConverter
公共函数Convert(ByVal values()作为对象,ByVal targetType作为系统.Type,ByVal参数作为对象,ByVal区域性作为系统.Globalization.CultureInfo)作为对象实现System.Windows.Data.IMultiValueConverter.Convert
作为IList的静态lvItems
静态lvItem为Xml.XmlElement
双色暗黄
转换为0
lvItems=值(0)
如果lvItems为Nothing,则退出函数
对于lvItems中的每个lvItem
'Debug.Print(lvItem.GetAttribute(“Tot”))
如果是Double.TryParse(lvItem.GetAttribute(“Tot”),nVal),那么
Convert=Convert+nVal
如果结束
下一个
端函数
公共函数ConvertBack(ByVal值作为对象,ByVal targetTypes()作为System.Type,ByVal参数作为对象,ByVal区域性作为System.Globalization.CultureInfo)作为对象()实现System.Windows.Data.IMultiValueConverter.ConvertBack
返回=无
端函数
末级
简单的解决方案(我相信不是最好的)是绑定

ElementName = myListView, Path = Items
在转换器中遍历项,并使用
VisualTreeHelper.GetChild()
查找项的底层TextBlock,然后
int.TryParse(TextBlock.Text,out currentItemValue)

简单的解决方案(我肯定不是最好的)是绑定

ElementName = myListView, Path = Items

在转换器中遍历项,并使用
VisualTreeHelper.GetChild()
查找项的底层TextBlock,然后
int.TryParse(TextBlock.Text,out currentItemValue)

我有点失望,因为我真的希望按照sllev的建议来处理这个问题。对我来说,这似乎是最合乎逻辑的做法。也许还有办法,但我还没找到

下面是我如何解决这个问题的。基本上,我所做的是将“Tot”列添加到我的数据源中(它不在我最初的解决方案中,尽管它在我上面的问题中)。我将文本框绑定到该列,然后在ListView中的文本框的LostFocus事件上刷新ListView。Refresh()导致再次调用SumConverter

希望这能帮助其他人摆脱困境——这似乎是一个简单的请求,但我在这上面浪费了很多时间

MainWindow.xaml:

<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">

    <Window.Resources>
        <XmlDataProvider x:Key="myParties" XPath="Parties" Source="XMLFile1.xml" />
        <CollectionViewSource x:Key="myCollectionViewSource" Source="{StaticResource myParties}" />
        <converters:SumConverter x:Key="mySumConverter" />
    </Window.Resources>

    <StackPanel>
        <ListView x:Name="myListView" 
                  HorizontalAlignment="Stretch" 
                  ItemsSource="{Binding Source={StaticResource myCollectionViewSource},XPath='Party',Mode=TwoWay}">

            <ListView.View>
                <GridView>
                    <GridViewColumn Width="100" DisplayMemberBinding="{Binding XPath='@Contact'}" Header="Contact"/>
                    <GridViewColumn DisplayMemberBinding="{Binding XPath='@Qty'}" Header="Q"/>
                    <GridViewColumn DisplayMemberBinding="{Binding XPath='@Amount'}" Header="Amt"/>
                    <GridViewColumn x:Name="tbTot" Header="Tot">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <DockPanel>
                                    <TextBox LostFocus="TextBox_LostFocus" Width="100" Text="{Binding XPath='@Tot'}" />
                                </DockPanel>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>    
        </ListView>

        <Label VerticalAlignment="Stretch">
            <Label.Content>
                <MultiBinding Converter="{StaticResource mySumConverter}">
                    <Binding ElementName="myListView" Path="Items"/>
                    <Binding ElementName="myListView" Path="Items.Count"/>
                </MultiBinding>
            </Label.Content>
        </Label>
    </StackPanel>
</Window>
Class MainWindow 

        Public Sub New()
            ' This call is required by the designer.
            InitializeComponent()
        End Sub

        Private Sub TextBox_LostFocus(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
            myListView.Items.Refresh()
        End Sub
    End Class
SumConverter.vb:

<ValueConversion(GetType(Object()), GetType(String))>
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 ItemCollection
        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

公共类SumConverter:实现System.Windows.Data.IMultiValueConverter
公共函数Convert(ByVal values()作为对象,ByVal targetType作为系统.Type,ByVal参数作为对象,ByVal区域性作为系统.Globalization.CultureInfo)作为对象实现System.Windows.Data.IMultiValueConverter.Convert
静态lvItems作为ItemCollection
静态lvItem为Xml.XmlElement
双色暗黄
转换为0
lvItems=值(0)
如果lvItems为Nothing,则退出函数
对于lvItems中的每个lvItem
'Debug.Print(lvItem.GetAttribute(“Tot”))
如果是Double.TryParse(lvItem.GetAttribute(“Tot”),nVal),那么
Convert=Convert+nVal
如果结束
下一个
端函数
公共函数ConvertBack(ByVal值作为对象,ByVal targetTypes()作为System.Type,ByVal参数作为对象,ByVal区域性作为System.Globalization.CultureInfo)作为对象()实现System.Windows.Data.IMultiValueConverter.ConvertBack
返回=无
端函数
末级

我有点失望,因为我真的希望按照sllev的建议来处理这个问题。对我来说,这似乎是最合乎逻辑的做法。也许还有办法,但我还没找到

下面是我如何解决这个问题的。基本上,我所做的是将“Tot”列添加到我的数据源中(它不在我最初的解决方案中,尽管它在我上面的问题中)。我将文本框绑定到该列,然后在ListView中的文本框的LostFocus事件上刷新ListView。Refresh()导致再次调用SumConverter

希望这能帮助其他人摆脱困境——这似乎是一个简单的请求,但我在这上面浪费了很多时间

MainWindow.xaml:

<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">

    <Window.Resources>
        <XmlDataProvider x:Key="myParties" XPath="Parties" Source="XMLFile1.xml" />
        <CollectionViewSource x:Key="myCollectionViewSource" Source="{StaticResource myParties}" />
        <converters:SumConverter x:Key="mySumConverter" />
    </Window.Resources>

    <StackPanel>
        <ListView x:Name="myListView" 
                  HorizontalAlignment="Stretch" 
                  ItemsSource="{Binding Source={StaticResource myCollectionViewSource},XPath='Party',Mode=TwoWay}">

            <ListView.View>
                <GridView>
                    <GridViewColumn Width="100" DisplayMemberBinding="{Binding XPath='@Contact'}" Header="Contact"/>
                    <GridViewColumn DisplayMemberBinding="{Binding XPath='@Qty'}" Header="Q"/>
                    <GridViewColumn DisplayMemberBinding="{Binding XPath='@Amount'}" Header="Amt"/>
                    <GridViewColumn x:Name="tbTot" Header="Tot">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <DockPanel>
                                    <TextBox LostFocus="TextBox_LostFocus" Width="100" Text="{Binding XPath='@Tot'}" />
                                </DockPanel>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>    
        </ListView>

        <Label VerticalAlignment="Stretch">
            <Label.Content>
                <MultiBinding Converter="{StaticResource mySumConverter}">
                    <Binding ElementName="myListView" Path="Items"/>
                    <Binding ElementName="myListView" Path="Items.Count"/>
                </MultiBinding>
            </Label.Content>
        </Label>
    </StackPanel>
</Window>
Class MainWindow 

        Public Sub New()
            ' This call is required by the designer.
            InitializeComponent()
        End Sub

        Private Sub TextBox_LostFocus(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
            myListView.Items.Refresh()
        End Sub
    End Class
SumConverter.vb:

<ValueConversion(GetType(Object()), GetType(String))>
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 ItemCollection
        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

公共类SumConverter:实现System.Windows.Data.IMultiValueConverter
公共函数Convert(ByVal values()作为对象,ByVal targetType作为系统.Type,ByVal参数作为对象,ByVal区域性作为系统.Globalization.CultureInfo)作为对象实现System.Windows.Data.IMult